diff --git a/ccan/bitmap/_info b/ccan/bitmap/_info index b8d297d16..37deb0a1d 100644 --- a/ccan/bitmap/_info +++ b/ccan/bitmap/_info @@ -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; } diff --git a/ccan/bitmap/bitmap.h b/ccan/bitmap/bitmap.h index 9e6c2bbc5..7ce48dbba 100644 --- a/ccan/bitmap/bitmap.h +++ b/ccan/bitmap/bitmap.h @@ -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_ */ diff --git a/ccan/bitmap/tal/LICENSE b/ccan/bitmap/tal/LICENSE new file mode 120000 index 000000000..4d0b239e7 --- /dev/null +++ b/ccan/bitmap/tal/LICENSE @@ -0,0 +1 @@ +../../../licenses/LGPL-2.1 \ No newline at end of file diff --git a/ccan/bitmap/tal/_info b/ccan/bitmap/tal/_info new file mode 100644 index 000000000..345c327d9 --- /dev/null +++ b/ccan/bitmap/tal/_info @@ -0,0 +1,53 @@ +#include "config.h" +#include +#include + +/** + * 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 + * #include + * + * // 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; +} diff --git a/ccan/bitmap/tal/tal.h b/ccan/bitmap/tal/tal.h new file mode 100644 index 000000000..7d1cd887d --- /dev/null +++ b/ccan/bitmap/tal/tal.h @@ -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 +#include + +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 */ diff --git a/ccan/bitmap/tal/test/run.c b/ccan/bitmap/tal/test/run.c new file mode 100644 index 000000000..a50593ac1 --- /dev/null +++ b/ccan/bitmap/tal/test/run.c @@ -0,0 +1,45 @@ +#include +#include + +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(); +}