From 84917baf8fb1bbbe0aea5fe2bbbbb5d8679448a6 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Sun, 5 Feb 2017 14:29:45 +0100 Subject: [PATCH 1/3] hex: fix comment Signed-off-by: Martin Milata --- ccan/str/hex/hex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ccan/str/hex/hex.h b/ccan/str/hex/hex.h index 0a0d5c5aa..624a77b10 100644 --- a/ccan/str/hex/hex.h +++ b/ccan/str/hex/hex.h @@ -70,4 +70,4 @@ static inline size_t hex_data_size(size_t strlen) { return strlen / 2; } -#endif /* PETTYCOIN_HEX_H */ +#endif /* CCAN_HEX_H */ From e5c52830afce7eb03172446be9ce5198d3a2b973 Mon Sep 17 00:00:00 2001 From: Ian Zimmerman Date: Sun, 20 Aug 2017 20:42:57 -0700 Subject: [PATCH 2/3] Add tal allocation functions to bitmap module --- ccan/bitmap/_info | 1 + ccan/bitmap/bitmap.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) 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..85256ae83 100644 --- a/ccan/bitmap/bitmap.h +++ b/ccan/bitmap/bitmap.h @@ -8,6 +8,7 @@ #include #include +#include typedef unsigned long bitmap_word; @@ -240,4 +241,47 @@ static inline bitmap *bitmap_realloc1(bitmap *bitmap, return bitmap; } +static inline bitmap *bitmap_tal(const tal_t* ctx, unsigned long nbits) +{ + return tal_arr(ctx, bitmap, BITMAP_NWORDS(nbits)); +} + +static inline bitmap *bitmap_tal0(const tal_t* ctx, unsigned long nbits) +{ + return tal_arrz(ctx, bitmap, BITMAP_NWORDS(nbits)); +} + +static inline bitmap *bitmap_tal1(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 bitmap *bitmap_tal_resize0(bitmap* obitmap, unsigned long obits, + unsigned long nbits) +{ + bitmap *nbitmap = obitmap; + + tal_resize(&nbitmap, BITMAP_NWORDS(nbits)); + if ((nbits > obits) && nbitmap) + bitmap_zero_range(nbitmap, obits, nbits); + + return nbitmap; +} + +static inline bitmap *bitmap_tal_resize1(bitmap* obitmap, unsigned long obits, + unsigned long nbits) +{ + bitmap *nbitmap = obitmap; + + tal_resize(&nbitmap, BITMAP_NWORDS(nbits)); + if ((nbits > obits) && nbitmap) + bitmap_fill_range(nbitmap, obits, nbits); + + return nbitmap; +} + #endif /* CCAN_BITMAP_H_ */ From f74e36355d3fbf05d2bde36a3ee79d69f102521c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 22 Aug 2017 11:35:49 +0930 Subject: [PATCH 3/3] bitmap/tal: new module with tal-friendly routines. Signed-off-by: Rusty Russell --- ccan/bitmap/bitmap.h | 95 -------------------------------------- ccan/bitmap/tal/LICENSE | 1 + ccan/bitmap/tal/_info | 53 +++++++++++++++++++++ ccan/bitmap/tal/tal.h | 51 ++++++++++++++++++++ ccan/bitmap/tal/test/run.c | 45 ++++++++++++++++++ 5 files changed, 150 insertions(+), 95 deletions(-) create mode 120000 ccan/bitmap/tal/LICENSE create mode 100644 ccan/bitmap/tal/_info create mode 100644 ccan/bitmap/tal/tal.h create mode 100644 ccan/bitmap/tal/test/run.c diff --git a/ccan/bitmap/bitmap.h b/ccan/bitmap/bitmap.h index 85256ae83..7ce48dbba 100644 --- a/ccan/bitmap/bitmap.h +++ b/ccan/bitmap/bitmap.h @@ -8,7 +8,6 @@ #include #include -#include typedef unsigned long bitmap_word; @@ -190,98 +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; -} - -static inline bitmap *bitmap_tal(const tal_t* ctx, unsigned long nbits) -{ - return tal_arr(ctx, bitmap, BITMAP_NWORDS(nbits)); -} - -static inline bitmap *bitmap_tal0(const tal_t* ctx, unsigned long nbits) -{ - return tal_arrz(ctx, bitmap, BITMAP_NWORDS(nbits)); -} - -static inline bitmap *bitmap_tal1(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 bitmap *bitmap_tal_resize0(bitmap* obitmap, unsigned long obits, - unsigned long nbits) -{ - bitmap *nbitmap = obitmap; - - tal_resize(&nbitmap, BITMAP_NWORDS(nbits)); - if ((nbits > obits) && nbitmap) - bitmap_zero_range(nbitmap, obits, nbits); - - return nbitmap; -} - -static inline bitmap *bitmap_tal_resize1(bitmap* obitmap, unsigned long obits, - unsigned long nbits) -{ - bitmap *nbitmap = obitmap; - - tal_resize(&nbitmap, BITMAP_NWORDS(nbits)); - if ((nbits > obits) && nbitmap) - bitmap_fill_range(nbitmap, obits, nbits); - - return nbitmap; -} - #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(); +}