]> git.ozlabs.org Git - ccan/commitdiff
tal/str: use tal/talloc backend #ifdef TAL_USE_TALLOC.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 3 Jan 2013 01:06:55 +0000 (11:36 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 3 Jan 2013 01:06:55 +0000 (11:36 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal/str/_info
ccan/tal/str/str.c
ccan/tal/str/str.h
ccan/tal/str/test/helper.h [new file with mode: 0644]
ccan/tal/str/test/run-string.c
ccan/tal/str/test/run-strreg.c
ccan/tal/str/test/run-take.c
ccan/tal/str/test/run.c

index f95fc10eb8d35426ab7ebb384759413a01340544..381dd98b1d1aa6d01a8f02ef2e54382081c4fb5f 100644 (file)
@@ -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;
        }
index 15aa0c046883633d956eb8abbf50c48e9e362c5e..237652f738fc2217b5f0e9eeb117f299b2a484b0 100644 (file)
@@ -11,7 +11,6 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <ccan/str/str.h>
-#include <ccan/tal/tal.h>
 #include <ccan/take/take.h>
 
 char *tal_strdup(const tal_t *ctx, const char *p)
index f2a694896c6aa89deed462278a70c7aa41307e32..0fe542cf235eeb9c08119f0de14ced6f6050a0ea 100644 (file)
@@ -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 <ccan/tal/talloc/talloc.h>
+#else
 #include <ccan/tal/tal.h>
-#include <ccan/tal/tal.h>
+#endif
 #include <string.h>
 #include <stdbool.h>
 
diff --git a/ccan/tal/str/test/helper.h b/ccan/tal/str/test/helper.h
new file mode 100644 (file)
index 0000000..c1bc9cc
--- /dev/null
@@ -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
index 7bf31a6cf6bce7057423e9dff1d9c4e7941f4cc3..8d596e462e13a006a86ea46cfbf1a47af0a1d952 100644 (file)
@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
+#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");
index 47671efd9270ec4a65d2ec7253fb65db5f31a257..6d126f3970a13acdbed21cbb63e0115e1aa9d477 100644 (file)
@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
+#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();
index 521e592b3820e4e49d3943b7aa05edd0cd1d6d7a..edf173f81462e346f4c759c5b7ce732de6295a42 100644 (file)
@@ -1,6 +1,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
+#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());
index cb7cc199647397f4c5c74fa1174f003976fce84d..4b9cf1fb5581e2a1596733f098f1bbc33e3540b0 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <ccan/tal/str/str.c>
 #include <ccan/tap/tap.h>
+#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();