Browse Source

tal/str: use tal/talloc backend #ifdef TAL_USE_TALLOC.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 13 years ago
parent
commit
97d99004be

+ 4 - 0
ccan/tal/str/_info

@@ -44,7 +44,11 @@ int main(int argc, char *argv[])
 
 
 	if (strcmp(argv[1], "depends") == 0) {
 	if (strcmp(argv[1], "depends") == 0) {
 		printf("ccan/str\n");
 		printf("ccan/str\n");
+#ifdef TAL_USE_TALLOC
+		printf("ccan/tal/talloc\n");
+#else
 		printf("ccan/tal\n");
 		printf("ccan/tal\n");
+#endif
 		printf("ccan/take\n");
 		printf("ccan/take\n");
 		return 0;
 		return 0;
 	}
 	}

+ 0 - 1
ccan/tal/str/str.c

@@ -11,7 +11,6 @@
 #include <unistd.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdio.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
-#include <ccan/tal/tal.h>
 #include <ccan/take/take.h>
 #include <ccan/take/take.h>
 
 
 char *tal_strdup(const tal_t *ctx, const char *p)
 char *tal_strdup(const tal_t *ctx, const char *p)

+ 4 - 1
ccan/tal/str/str.h

@@ -1,8 +1,11 @@
 /* Licensed under BSD-MIT - see LICENSE file for details */
 /* Licensed under BSD-MIT - see LICENSE file for details */
 #ifndef CCAN_STR_TAL_H
 #ifndef CCAN_STR_TAL_H
 #define CCAN_STR_TAL_H
 #define CCAN_STR_TAL_H
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
 #include <ccan/tal/tal.h>
 #include <ccan/tal/tal.h>
-#include <ccan/tal/tal.h>
+#endif
 #include <string.h>
 #include <string.h>
 #include <stdbool.h>
 #include <stdbool.h>
 
 

+ 22 - 0
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

+ 10 - 5
ccan/tal/str/test/run-string.c

@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
 #include <ccan/tap/tap.h>
+#include "helper.h"
 
 
 int main(void)
 int main(void)
 {
 {
@@ -21,7 +22,11 @@ int main(void)
 	ok1(tal_parent(c) == parent);
 	ok1(tal_parent(c) == parent);
 	tal_free(c);
 	tal_free(c);
 
 
+#ifdef TAL_USE_TALLOC
+	c = tal_talloc_typechk_(parent, char *);
+#else
 	c = tal_typechk_(parent, char *);
 	c = tal_typechk_(parent, char *);
+#endif
 	c = tal_dup(parent, char, "hello", 6, 0);
 	c = tal_dup(parent, char, "hello", 6, 0);
 	ok1(strcmp(c, "hello") == 0);
 	ok1(strcmp(c, "hello") == 0);
 	ok1(strcmp(tal_name(c), "char[]") == 0);
 	ok1(strcmp(tal_name(c), "char[]") == 0);
@@ -49,26 +54,26 @@ int main(void)
 	c = tal_strcat(parent, take(c), " again");
 	c = tal_strcat(parent, take(c), " again");
 	ok1(strcmp(c, "hello there again") == 0);
 	ok1(strcmp(c, "hello there again") == 0);
 	ok1(tal_parent(c) == parent);
 	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));
 	c = tal_strcat(parent, "And ", take(c));
 	ok1(strcmp(c, "And hello there again") == 0);
 	ok1(strcmp(c, "And hello there again") == 0);
 	ok1(tal_parent(c) == parent);
 	ok1(tal_parent(c) == parent);
-	ok1(tal_first(parent) == c && !tal_next(parent, c));
+	ok1(single_child(parent, c));
 
 
 	/* NULL pass through works... */
 	/* NULL pass through works... */
 	c = tal_strcat(parent, take(NULL), take(c));
 	c = tal_strcat(parent, take(NULL), take(c));
 	ok1(!c);
 	ok1(!c);
-	ok1(!tal_first(parent));
+	ok1(no_children(parent));
 
 
 	c = tal_strcat(parent, take(tal_strdup(parent, "hi")),
 	c = tal_strcat(parent, take(tal_strdup(parent, "hi")),
 		       take(NULL));
 		       take(NULL));
 	ok1(!c);
 	ok1(!c);
-	ok1(!tal_first(parent));
+	ok1(no_children(parent));
 
 
 	c = tal_strcat(parent, take(NULL), take(NULL));
 	c = tal_strcat(parent, take(NULL), take(NULL));
 	ok1(!c);
 	ok1(!c);
-	ok1(!tal_first(parent));
+	ok1(no_children(parent));
 
 
 	/* Appending formatted strings. */
 	/* Appending formatted strings. */
 	c = tal_strdup(parent, "hi");
 	c = tal_strdup(parent, "hi");

+ 6 - 5
ccan/tal/str/test/run-strreg.c

@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
 #include <ccan/tap/tap.h>
+#include "helper.h"
 
 
 static bool find_parent(tal_t *child, tal_t *parent)
 static bool find_parent(tal_t *child, tal_t *parent)
 {
 {
@@ -77,7 +78,7 @@ int main(int argc, char *argv[])
 	tal_free(a);
 	tal_free(a);
 
 
 	/* No leaks! */
 	/* No leaks! */
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 
 
 	/* NULL arg with take means always fail. */
 	/* NULL arg with take means always fail. */
 	ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
 	ok1(tal_strreg(ctx, take(NULL), "((hello|goodbye) world)",
@@ -89,7 +90,7 @@ int main(int argc, char *argv[])
 	ok1(streq(b, "hello"));
 	ok1(streq(b, "hello"));
 	ok1(tal_parent(b) == ctx);
 	ok1(tal_parent(b) == ctx);
 	tal_free(b);
 	tal_free(b);
-	ok1(tal_first(ctx) == NULL);
+	ok1(no_children(ctx));
 
 
 	/* Take regex. */
 	/* Take regex. */
 	a = tal_strdup(ctx, "([a-z]+)");
 	a = tal_strdup(ctx, "([a-z]+)");
@@ -97,7 +98,7 @@ int main(int argc, char *argv[])
 	ok1(streq(b, "hello"));
 	ok1(streq(b, "hello"));
 	ok1(tal_parent(b) == ctx);
 	ok1(tal_parent(b) == ctx);
 	tal_free(b);
 	tal_free(b);
-	ok1(tal_first(ctx) == NULL);
+	ok1(no_children(ctx));
 
 
 	/* Take both. */
 	/* Take both. */
 	a = tal_strdup(ctx, "([a-z]+)");
 	a = tal_strdup(ctx, "([a-z]+)");
@@ -106,13 +107,13 @@ int main(int argc, char *argv[])
 	ok1(streq(b, "hello"));
 	ok1(streq(b, "hello"));
 	ok1(tal_parent(b) == ctx);
 	ok1(tal_parent(b) == ctx);
 	tal_free(b);
 	tal_free(b);
-	ok1(tal_first(ctx) == NULL);
+	ok1(no_children(ctx));
 
 
 	/* ... even if we fail to match. */
 	/* ... even if we fail to match. */
 	a = tal_strdup(ctx, "([a-z]+)");
 	a = tal_strdup(ctx, "([a-z]+)");
 	ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
 	ok1(tal_strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
 		       take(a), &b, invalid) == false);
 		       take(a), &b, invalid) == false);
-	ok1(tal_first(ctx) == NULL);
+	ok1(no_children(ctx));
 	tal_free(ctx);
 	tal_free(ctx);
 
 
 	return exit_status();
 	return exit_status();

+ 2 - 1
ccan/tal/str/test/run-take.c

@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
 #include <ccan/tap/tap.h>
+#include "helper.h"
 
 
 int main(void)
 int main(void)
 {
 {
@@ -32,7 +33,7 @@ int main(void)
 	ok1(tal_parent(c) == parent);
 	ok1(tal_parent(c) == parent);
 	/* No leftover allocations. */
 	/* No leftover allocations. */
 	tal_free(c);
 	tal_free(c);
-	ok1(tal_first(parent) == NULL);
+	ok1(no_children(parent));
 
 
 	tal_free(parent);
 	tal_free(parent);
 	ok1(!taken_any());
 	ok1(!taken_any());

+ 10 - 9
ccan/tal/str/test/run.c

@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdio.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
 #include <ccan/tap/tap.h>
+#include "helper.h"
 
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
 
 
@@ -85,7 +86,7 @@ int main(int argc, char *argv[])
 	ok1(tal_check(ctx, NULL));
 	ok1(tal_check(ctx, NULL));
 	tal_free(split);
 	tal_free(split);
 	/* Previous free should get rid of str */
 	/* Previous free should get rid of str */
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 
 
 	/* tal_strsplit take delims */
 	/* tal_strsplit take delims */
 	str = tal_strdup(ctx, " ");
 	str = tal_strdup(ctx, " ");
@@ -98,7 +99,7 @@ int main(int argc, char *argv[])
 	ok1(tal_check(ctx, NULL));
 	ok1(tal_check(ctx, NULL));
 	tal_free(split);
 	tal_free(split);
 	/* str is gone... */
 	/* str is gone... */
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 
 
 	/* tal_strsplit takes both. */
 	/* tal_strsplit takes both. */
 	split = tal_strsplit(ctx, take(tal_strdup(NULL, "hello world")),
 	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));
 	ok1(tal_check(ctx, NULL));
 	tal_free(split);
 	tal_free(split);
 	/* temp allocs are gone... */
 	/* temp allocs are gone... */
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 
 
 	/* tal_strjoin passthrough taken NULLs OK. */
 	/* tal_strjoin passthrough taken NULLs OK. */
 	ok1(tal_strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL);
 	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(!strcmp(str, "hello there world"));
 	ok1(tal_parent(str) == ctx);
 	ok1(tal_parent(str) == ctx);
 	/* split is gone... */
 	/* split is gone... */
-	ok1(tal_first(ctx) == str && !tal_next(ctx, str));
+	ok1(single_child(ctx, str));
 	tal_free(str);
 	tal_free(str);
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 
 
 	/* tal_strjoin take delim */
 	/* tal_strjoin take delim */
 	split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
 	split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
@@ -137,9 +138,9 @@ int main(int argc, char *argv[])
 	ok1(tal_parent(str) == ctx);
 	ok1(tal_parent(str) == ctx);
 	tal_free(split);
 	tal_free(split);
 	/* tmp alloc is gone, str is only remainder. */
 	/* 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);
 	tal_free(str);
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 
 
 	/* tal_strjoin take both. */
 	/* tal_strjoin take both. */
 	str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
 	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(!strcmp(str, "hello there world"));
 	ok1(tal_parent(str) == ctx);
 	ok1(tal_parent(str) == ctx);
 	/* tmp allocs are gone, str is only remainder. */
 	/* 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);
 	tal_free(str);
-	ok1(!tal_first(ctx));
+	ok1(no_children(ctx));
 	tal_free(ctx);
 	tal_free(ctx);
 
 
 	return exit_status();
 	return exit_status();