From 97d99004bec31012400b6c1d9bad045ae1c9b075 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 3 Jan 2013 11:36:55 +1030 Subject: [PATCH 1/1] tal/str: use tal/talloc backend #ifdef TAL_USE_TALLOC. Signed-off-by: Rusty Russell --- ccan/tal/str/_info | 4 ++++ ccan/tal/str/str.c | 1 - ccan/tal/str/str.h | 5 ++++- ccan/tal/str/test/helper.h | 22 ++++++++++++++++++++++ ccan/tal/str/test/run-string.c | 15 ++++++++++----- ccan/tal/str/test/run-strreg.c | 11 ++++++----- ccan/tal/str/test/run-take.c | 3 ++- ccan/tal/str/test/run.c | 19 ++++++++++--------- 8 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 ccan/tal/str/test/helper.h diff --git a/ccan/tal/str/_info b/ccan/tal/str/_info index f95fc10e..381dd98b 100644 --- a/ccan/tal/str/_info +++ b/ccan/tal/str/_info @@ -44,7 +44,11 @@ int main(int argc, char *argv[]) if (strcmp(argv[1], "depends") == 0) { printf("ccan/str\n"); +#ifdef TAL_USE_TALLOC + printf("ccan/tal/talloc\n"); +#else printf("ccan/tal\n"); +#endif printf("ccan/take\n"); return 0; } diff --git a/ccan/tal/str/str.c b/ccan/tal/str/str.c index 15aa0c04..237652f7 100644 --- a/ccan/tal/str/str.c +++ b/ccan/tal/str/str.c @@ -11,7 +11,6 @@ #include #include #include -#include #include char *tal_strdup(const tal_t *ctx, const char *p) diff --git a/ccan/tal/str/str.h b/ccan/tal/str/str.h index f2a69489..0fe542cf 100644 --- a/ccan/tal/str/str.h +++ b/ccan/tal/str/str.h @@ -1,8 +1,11 @@ /* Licensed under BSD-MIT - see LICENSE file for details */ #ifndef CCAN_STR_TAL_H #define CCAN_STR_TAL_H +#ifdef TAL_USE_TALLOC +#include +#else #include -#include +#endif #include #include diff --git a/ccan/tal/str/test/helper.h b/ccan/tal/str/test/helper.h new file mode 100644 index 00000000..c1bc9ccf --- /dev/null +++ b/ccan/tal/str/test/helper.h @@ -0,0 +1,22 @@ +/* tal/talloc can't implement tal_first/tal_next. */ +#ifdef TAL_USE_TALLOC +static inline bool no_children(const void *ctx) +{ + return talloc_total_blocks(ctx) == 1; +} + +static inline bool single_child(const void *ctx, const void *child) +{ + return talloc_total_blocks(ctx) == 2 && tal_parent(child) == ctx; +} +#else +static inline bool no_children(const void *ctx) +{ + return !tal_first(ctx); +} + +static inline bool single_child(const void *ctx, const void *child) +{ + return tal_first(ctx) == child && !tal_next(ctx, child); +} +#endif diff --git a/ccan/tal/str/test/run-string.c b/ccan/tal/str/test/run-string.c index 7bf31a6c..8d596e46 100644 --- a/ccan/tal/str/test/run-string.c +++ b/ccan/tal/str/test/run-string.c @@ -1,6 +1,7 @@ #include #include #include +#include "helper.h" int main(void) { @@ -21,7 +22,11 @@ int main(void) ok1(tal_parent(c) == parent); tal_free(c); +#ifdef TAL_USE_TALLOC + c = tal_talloc_typechk_(parent, char *); +#else c = tal_typechk_(parent, char *); +#endif c = tal_dup(parent, char, "hello", 6, 0); ok1(strcmp(c, "hello") == 0); ok1(strcmp(tal_name(c), "char[]") == 0); @@ -49,26 +54,26 @@ int main(void) c = tal_strcat(parent, take(c), " again"); ok1(strcmp(c, "hello there again") == 0); ok1(tal_parent(c) == parent); - ok1(tal_first(parent) == c && !tal_next(parent, c)); + ok1(single_child(parent, c)); c = tal_strcat(parent, "And ", take(c)); ok1(strcmp(c, "And hello there again") == 0); ok1(tal_parent(c) == parent); - ok1(tal_first(parent) == c && !tal_next(parent, c)); + ok1(single_child(parent, c)); /* NULL pass through works... */ c = tal_strcat(parent, take(NULL), take(c)); ok1(!c); - ok1(!tal_first(parent)); + ok1(no_children(parent)); c = tal_strcat(parent, take(tal_strdup(parent, "hi")), take(NULL)); ok1(!c); - ok1(!tal_first(parent)); + ok1(no_children(parent)); c = tal_strcat(parent, take(NULL), take(NULL)); ok1(!c); - ok1(!tal_first(parent)); + ok1(no_children(parent)); /* Appending formatted strings. */ c = tal_strdup(parent, "hi"); diff --git a/ccan/tal/str/test/run-strreg.c b/ccan/tal/str/test/run-strreg.c index 47671efd..6d126f39 100644 --- a/ccan/tal/str/test/run-strreg.c +++ b/ccan/tal/str/test/run-strreg.c @@ -1,6 +1,7 @@ #include #include #include +#include "helper.h" static bool find_parent(tal_t *child, tal_t *parent) { @@ -77,7 +78,7 @@ int main(int argc, char *argv[]) tal_free(a); /* No leaks! */ - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); /* NULL arg with take means always fail. */ ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)", @@ -89,7 +90,7 @@ int main(int argc, char *argv[]) ok1(streq(b, "hello")); ok1(tal_parent(b) == ctx); tal_free(b); - ok1(tal_first(ctx) == NULL); + ok1(no_children(ctx)); /* Take regex. */ a = tal_strdup(ctx, "([a-z]+)"); @@ -97,7 +98,7 @@ int main(int argc, char *argv[]) ok1(streq(b, "hello")); ok1(tal_parent(b) == ctx); tal_free(b); - ok1(tal_first(ctx) == NULL); + ok1(no_children(ctx)); /* Take both. */ a = tal_strdup(ctx, "([a-z]+)"); @@ -106,13 +107,13 @@ int main(int argc, char *argv[]) ok1(streq(b, "hello")); ok1(tal_parent(b) == ctx); tal_free(b); - ok1(tal_first(ctx) == NULL); + ok1(no_children(ctx)); /* ... even if we fail to match. */ a = tal_strdup(ctx, "([a-z]+)"); ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")), take(a), &b, invalid) == false); - ok1(tal_first(ctx) == NULL); + ok1(no_children(ctx)); tal_free(ctx); return exit_status(); diff --git a/ccan/tal/str/test/run-take.c b/ccan/tal/str/test/run-take.c index 521e592b..edf173f8 100644 --- a/ccan/tal/str/test/run-take.c +++ b/ccan/tal/str/test/run-take.c @@ -1,6 +1,7 @@ #include #include #include +#include "helper.h" int main(void) { @@ -32,7 +33,7 @@ int main(void) ok1(tal_parent(c) == parent); /* No leftover allocations. */ tal_free(c); - ok1(tal_first(parent) == NULL); + ok1(no_children(parent)); tal_free(parent); ok1(!taken_any()); diff --git a/ccan/tal/str/test/run.c b/ccan/tal/str/test/run.c index cb7cc199..4b9cf1fb 100644 --- a/ccan/tal/str/test/run.c +++ b/ccan/tal/str/test/run.c @@ -3,6 +3,7 @@ #include #include #include +#include "helper.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) @@ -85,7 +86,7 @@ int main(int argc, char *argv[]) ok1(tal_check(ctx, NULL)); tal_free(split); /* Previous free should get rid of str */ - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); /* tal_strsplit take delims */ str = tal_strdup(ctx, " "); @@ -98,7 +99,7 @@ int main(int argc, char *argv[]) ok1(tal_check(ctx, NULL)); tal_free(split); /* str is gone... */ - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); /* tal_strsplit takes both. */ split = tal_strsplit(ctx, take(tal_strdup(NULL, "hello world")), @@ -111,7 +112,7 @@ int main(int argc, char *argv[]) ok1(tal_check(ctx, NULL)); tal_free(split); /* temp allocs are gone... */ - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); /* tal_strjoin passthrough taken NULLs OK. */ ok1(tal_strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL); @@ -125,9 +126,9 @@ int main(int argc, char *argv[]) ok1(!strcmp(str, "hello there world")); ok1(tal_parent(str) == ctx); /* split is gone... */ - ok1(tal_first(ctx) == str && !tal_next(ctx, str)); + ok1(single_child(ctx, str)); tal_free(str); - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); /* tal_strjoin take delim */ split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK); @@ -137,9 +138,9 @@ int main(int argc, char *argv[]) ok1(tal_parent(str) == ctx); tal_free(split); /* tmp alloc is gone, str is only remainder. */ - ok1(tal_first(ctx) == str && !tal_next(ctx, str)); + ok1(single_child(ctx, str)); tal_free(str); - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); /* tal_strjoin take both. */ str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ", @@ -148,9 +149,9 @@ int main(int argc, char *argv[]) ok1(!strcmp(str, "hello there world")); ok1(tal_parent(str) == ctx); /* tmp allocs are gone, str is only remainder. */ - ok1(tal_first(ctx) == str && !tal_next(ctx, str)); + ok1(single_child(ctx, str)); tal_free(str); - ok1(!tal_first(ctx)); + ok1(no_children(ctx)); tal_free(ctx); return exit_status(); -- 2.39.2