Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitmap tal support #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ccan/bitmap/_info
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int main(int argc, char *argv[])

if (strcmp(argv[1], "depends") == 0) {
printf("ccan/endian\n");
printf("ccan/tal\n");
return 0;
}

Expand Down
51 changes: 0 additions & 51 deletions ccan/bitmap/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,55 +189,4 @@ static inline bool bitmap_empty(const bitmap *bitmap, unsigned long nbits)

unsigned long bitmap_ffs(const bitmap *bitmap,
unsigned long n, unsigned long m);

/*
* Allocation functions
*/
static inline bitmap *bitmap_alloc(unsigned long nbits)
{
return malloc(bitmap_sizeof(nbits));
}

static inline bitmap *bitmap_alloc0(unsigned long nbits)
{
bitmap *bitmap;

bitmap = bitmap_alloc(nbits);
if (bitmap)
bitmap_zero(bitmap, nbits);
return bitmap;
}

static inline bitmap *bitmap_alloc1(unsigned long nbits)
{
bitmap *bitmap;

bitmap = bitmap_alloc(nbits);
if (bitmap)
bitmap_fill(bitmap, nbits);
return bitmap;
}

static inline bitmap *bitmap_realloc0(bitmap *bitmap,
unsigned long obits, unsigned long nbits)
{
bitmap = realloc(bitmap, bitmap_sizeof(nbits));

if ((nbits > obits) && bitmap)
bitmap_zero_range(bitmap, obits, nbits);

return bitmap;
}

static inline bitmap *bitmap_realloc1(bitmap *bitmap,
unsigned long obits, unsigned long nbits)
{
bitmap = realloc(bitmap, bitmap_sizeof(nbits));

if ((nbits > obits) && bitmap)
bitmap_fill_range(bitmap, obits, nbits);

return bitmap;
}

#endif /* CCAN_BITMAP_H_ */
1 change: 1 addition & 0 deletions ccan/bitmap/tal/LICENSE
53 changes: 53 additions & 0 deletions ccan/bitmap/tal/_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
* bitmap/tal - tal routines for bitmaps.
*
* This code contains extra bitmap routines which use the tal allocator.
*
* License: LGPL (v2.1 or any later version)
* Author: Ian Zimmerman
*
* Example:
* #include <ccan/bitmap/tal/tal.h>
* #include <stdio.h>
*
* // Takes a commandline of bit numbers, prints the number set.
* int main(int argc, char *argv[])
* {
* int i, bits = 0, total = 0;
* bitmap *bmap = bitmap_talz(NULL, bits);
*
* for (i = 1; i < argc; i++) {
* int bit = atoi(argv[i]);
* if (bit >= bits) {
* bitmap_tal_resizez(&bmap, bits, bit+1);
* bits = bit+1;
* }
* bitmap_set_bit(bmap, bit);
* }
* for (i = 0; i < bits; i++)
* total += bitmap_test_bit(bmap, i);
* printf("Total bits: %i\n", total);
* return 0;
* }
* // Given "1" outputs Total bits: 1
* // Given "1 1 1" outputs Total bits: 1
* // Given "1 20" outputs Total bits: 2
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;

if (strcmp(argv[1], "depends") == 0) {
printf("ccan/bitmap\n");
printf("ccan/tal\n");
return 0;
}

return 1;
}
51 changes: 51 additions & 0 deletions ccan/bitmap/tal/tal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* GNU LGPL version 2 (or later) - see LICENSE file for details */
#ifndef CCAN_BITMAP_TAL_H
#define CCAN_BITMAP_TAL_H
#include <ccan/tal/tal.h>
#include <ccan/bitmap/bitmap.h>

static inline bitmap *bitmap_tal(const tal_t* ctx, unsigned long nbits)
{
return tal_arr(ctx, bitmap, BITMAP_NWORDS(nbits));
}

static inline bitmap *bitmap_talz(const tal_t* ctx, unsigned long nbits)
{
return tal_arrz(ctx, bitmap, BITMAP_NWORDS(nbits));
}

static inline bitmap *bitmap_tal_fill(const tal_t* ctx, unsigned long nbits)
{
bitmap *nbitmap = tal_arr(ctx, bitmap, BITMAP_NWORDS(nbits));

if (nbitmap)
bitmap_fill(nbitmap, nbits);
return nbitmap;
}

static inline bool bitmap_tal_resizez(bitmap **bitmap,
unsigned long obits,
unsigned long nbits)
{
if (!tal_resize(bitmap, BITMAP_NWORDS(nbits)))
return false;

if (nbits > obits)
bitmap_zero_range(*bitmap, obits, nbits);

return true;
}

static inline bool bitmap_tal_resize_fill(bitmap **bitmap,
unsigned long obits,
unsigned long nbits)
{
if (!tal_resize(bitmap, BITMAP_NWORDS(nbits)))
return false;

if (nbits > obits)
bitmap_fill_range(*bitmap, obits, nbits);

return true;
}
#endif /* CCAN_BITMAP_TAL_H */
45 changes: 45 additions & 0 deletions ccan/bitmap/tal/test/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <ccan/bitmap/tal/tal.h>
#include <ccan/tap/tap.h>

static size_t bitmap_popcount(const bitmap *b, size_t nbits)
{
size_t i, n = 0;
for (i = 0; i < nbits; i++)
n += bitmap_test_bit(b, i);
return n;
}

int main(void)
{
tal_t *ctx = tal(NULL, char);
bitmap *b;

/* This is how many tests you plan to run */
plan_tests(10);
b = bitmap_talz(ctx, 99);
ok1(bitmap_empty(b, 99));

b = bitmap_tal_fill(ctx, 99);
ok1(bitmap_full(b, 99));

/* Resize shrink (with zero pad). */
ok1(bitmap_tal_resizez(&b, 99, 50));
ok1(bitmap_full(b, 50));

/* Resize shrink (with one pad). */
ok1(bitmap_tal_resize_fill(&b, 50, 29));
ok1(bitmap_full(b, 29));

/* Enlarge (with zero pad) */
ok1(bitmap_tal_resizez(&b, 29, 50));
ok1(bitmap_popcount(b, 50) == 29);

/* Enlarge (with one pad) */
ok1(bitmap_tal_resize_fill(&b, 50, 99));
ok1(bitmap_popcount(b, 99) == 29 + 49);

tal_free(ctx);

/* This exits depending on whether all tests passed */
return exit_status();
}