]> git.ozlabs.org Git - ccan/commitdiff
tal: rename tal_dup to tal_dup_arr, make tal_dup simpler.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 28 May 2015 10:25:42 +0000 (19:55 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 28 May 2015 10:36:58 +0000 (20:06 +0930)
It's a bit too powerful, if you just want to memdup one thing, it's
hard to remember what the extra args are for.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/invbloom/invbloom.c
ccan/tal/path/path.c
ccan/tal/str/test/run-string.c
ccan/tal/tal.h
ccan/tal/talloc/talloc.h
ccan/tal/talloc/test/run-overflow.c
ccan/tal/talloc/test/run-take.c
ccan/tal/test/run-overflow.c
ccan/tal/test/run-take.c
tools/ccanlint/tests/depends_accurate.c

index 28dabec67d8c07f0f809f033aaaa49b27fe8d4a7..d34ec1feeadded58b431be5e8aaa70bf9bc748ff 100644 (file)
@@ -145,7 +145,7 @@ static void *extract(const tal_t *ctx, struct invbloom *ib, int count)
                if (ib->count[i] != count)
                        continue;
 
-               id = tal_dup(ctx, u8, idsum_ptr(ib, i), ib->id_size, 0);
+               id = tal_dup_arr(ctx, u8, idsum_ptr(ib, i), ib->id_size, 0);
                return id;
        }
        return NULL;
index a0dbe291440e248fba0faddf6a70bf0062c0c009..93362c754f599981014916a71482b6997c98686d 100644 (file)
@@ -50,7 +50,7 @@ char *path_join(const tal_t *ctx, const char *base, const char *a)
                goto out;
 
        len = strlen(base);
-       ret = tal_dup(ctx, char, base, len, 1 + strlen(a) + 1);
+       ret = tal_dup_arr(ctx, char, base, len, 1 + strlen(a) + 1);
        if (!ret)
                goto out;
        if (ret[len-1] != PATH_SEP)
@@ -465,7 +465,7 @@ char *path_basename(const tal_t *ctx, const char *path)
 static char *fixed_string(const tal_t *ctx,
                          const char *str, const char *path)
 {
-       char *ret = tal_dup(ctx, char, path, 0, strlen(str)+1);
+       char *ret = tal_dup_arr(ctx, char, path, 0, strlen(str)+1);
        if (ret)
                strcpy(ret, str);
        return ret;
index 8d596e462e13a006a86ea46cfbf1a47af0a1d952..533ad01280ed593ee2594514c3bf07e566c9eb48 100644 (file)
@@ -27,14 +27,14 @@ int main(void)
 #else
        c = tal_typechk_(parent, char *);
 #endif
-       c = tal_dup(parent, char, "hello", 6, 0);
+       c = tal_dup_arr(parent, char, "hello", 6, 0);
        ok1(strcmp(c, "hello") == 0);
        ok1(strcmp(tal_name(c), "char[]") == 0);
        ok1(tal_parent(c) == parent);
        tal_free(c);
 
        /* Now with an extra byte. */
-       c = tal_dup(parent, char, "hello", 6, 1);
+       c = tal_dup_arr(parent, char, "hello", 6, 1);
        ok1(strcmp(c, "hello") == 0);
        ok1(strcmp(tal_name(c), "char[]") == 0);
        ok1(tal_parent(c) == parent);
index 3da1e4203d8edcb332e4701d876c05091977acbc..0cfea98446245ef3c0d9eb6882c4acf734e6cd83 100644 (file)
@@ -289,14 +289,25 @@ tal_t *tal_next(const tal_t *root, const tal_t *prev);
 tal_t *tal_parent(const tal_t *ctx);
 
 /**
- * tal_dup - duplicate an array.
+ * tal_dup - duplicate an object.
+ * @ctx: The tal allocated object to be parent of the result (may be NULL).
+ * @type: the type (should match type of @p!)
+ * @p: the object to copy (or reparented if take())
+ */
+#define tal_dup(ctx, type, p)                  \
+       ((type *)tal_dup_((ctx), tal_typechk_(p, type *),       \
+                         sizeof(type), 1, 0,                   \
+                         false, TAL_LABEL(type, "")))
+
+/**
+ * tal_dup_arr - duplicate an array.
  * @ctx: The tal allocated object to be parent of the result (may be NULL).
  * @type: the type (should match type of @p!)
  * @p: the array to copy (or resized & reparented if take())
  * @n: the number of sizeof(type) entries to copy.
  * @extra: the number of extra sizeof(type) entries to allocate.
  */
-#define tal_dup(ctx, type, p, n, extra)                                \
+#define tal_dup_arr(ctx, type, p, n, extra)                    \
        ((type *)tal_dup_((ctx), tal_typechk_(p, type *),       \
                          sizeof(type), (n), (extra),           \
                          true, TAL_LABEL(type, "[]")))
index 718f12c4095f7c26f4d4d50b40e67d13fe26350a..8dfb80e7dd27ea59af20a57cf94966e94a589dd9 100644 (file)
@@ -185,14 +185,25 @@ typedef TALLOC_CTX tal_t;
 #define tal_parent(ctx) talloc_parent(ctx)
 
 /**
- * tal_dup - duplicate an array.
+ * tal_dup - duplicate an object.
+ * @ctx: The tal allocated object to be parent of the result (may be NULL).
+ * @type: the type (should match type of @p!)
+ * @p: the object to copy (or reparented if take())
+ */
+#define tal_dup(ctx, type, p)                                          \
+       ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
+                                sizeof(type), 1, 0,                    \
+                                TAL_LABEL(type, "")))
+
+/**
+ * tal_dup_arr - duplicate an array.
  * @ctx: The tal allocated object to be parent of the result (may be NULL).
  * @type: the type (should match type of @p!)
  * @p: the array to copy (or resized & reparented if take())
  * @n: the number of sizeof(type) entries to copy.
  * @extra: the number of extra sizeof(type) entries to allocate.
  */
-#define tal_dup(ctx, type, p, n, extra)                                        \
+#define tal_dup_arr(ctx, type, p, n, extra)                                    \
        ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
                                 sizeof(type), (n), (extra),            \
                                 TAL_LABEL(type, "[]")))
index c7f9c18f8ea06c8e7f8366b93802f6cbda7457df..e555b3599f76bd293ef7323d2cb267077d19e4b2 100644 (file)
@@ -33,19 +33,19 @@ int main(void)
        origpi = tal_arr(NULL, int, 100);
        ok1(origpi);
        ok1(error_count == 0);
-       pi = tal_dup(NULL, int, origpi, (size_t)-1, 0);
+       pi = tal_dup_arr(NULL, int, origpi, (size_t)-1, 0);
        ok1(!pi);
        ok1(error_count == 1);
-       pi = tal_dup(NULL, int, origpi, 0, (size_t)-1);
+       pi = tal_dup_arr(NULL, int, origpi, 0, (size_t)-1);
        ok1(!pi);
        ok1(error_count == 2);
 
-       pi = tal_dup(NULL, int, origpi, (size_t)-1UL / sizeof(int),
+       pi = tal_dup_arr(NULL, int, origpi, (size_t)-1UL / sizeof(int),
                     (size_t)-1UL / sizeof(int));
        ok1(!pi);
        ok1(error_count == 3);
        /* This will still overflow when tal_hdr is added. */
-       pi = tal_dup(NULL, int, origpi, (size_t)-1UL / sizeof(int) / 2,
+       pi = tal_dup_arr(NULL, int, origpi, (size_t)-1UL / sizeof(int) / 2,
                     (size_t)-1UL / sizeof(int) / 2);
        ok1(!pi);
        ok1(error_count == 4);
@@ -55,20 +55,20 @@ int main(void)
        /* Now, check that with taltk() we free old one on failure. */
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
-       pi = tal_dup(NULL, int, take(origpi), (size_t)-1, 0);
+       pi = tal_dup_arr(NULL, int, take(origpi), (size_t)-1, 0);
        ok1(!pi);
        ok1(error_count == 1);
 
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
-       pi = tal_dup(NULL, int, take(origpi), 0, (size_t)-1);
+       pi = tal_dup_arr(NULL, int, take(origpi), 0, (size_t)-1);
        ok1(!pi);
        ok1(error_count == 1);
        ok1(talloc_total_blocks(NULL) == 1);
 
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
-       pi = tal_dup(NULL, int, take(origpi), (size_t)-1UL / sizeof(int),
+       pi = tal_dup_arr(NULL, int, take(origpi), (size_t)-1UL / sizeof(int),
                     (size_t)-1UL / sizeof(int));
        ok1(!pi);
        ok1(error_count == 1);
@@ -77,7 +77,7 @@ int main(void)
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
        /* This will still overflow when tal_hdr is added. */
-       pi = tal_dup(NULL, int, take(origpi), (size_t)-1UL / sizeof(int) / 2,
+       pi = tal_dup_arr(NULL, int, take(origpi), (size_t)-1UL / sizeof(int) / 2,
                     (size_t)-1UL / sizeof(int) / 2);
        ok1(!pi);
        ok1(error_count == 1);
index 7283c16e402a2cf14adfd652e609c068983c869e..4939b8af4a5127e8201d3a30da71360a43395352 100644 (file)
@@ -26,17 +26,17 @@ int main(void)
 
        c = tal(parent, char);
        *c = 'h';
-       c = tal_dup(parent, char, take(c), 1, 0);
+       c = tal_dup(parent, char, take(c));
        ok1(c[0] == 'h');
        ok1(tal_parent(c) == parent);
 
-       c = tal_dup(parent, char, take(c), 1, 2);
+       c = tal_dup_arr(parent, char, take(c), 1, 2);
        ok1(c[0] == 'h');
        strcpy(c, "hi");
        ok1(tal_parent(c) == parent);
 
        /* dup must reparent child. */
-       c = tal_dup(NULL, char, take(c), 1, 0);
+       c = tal_dup(NULL, char, take(c));
        ok1(c[0] == 'h');
        ok1(tal_parent(c) == NULL);
 
@@ -49,7 +49,7 @@ int main(void)
 
        /* NULL pass-through. */
        c = NULL;
-       ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL);
+       ok1(tal_dup_arr(NULL, char, take(c), 5, 5) == NULL);
        ok1(!taken_any());
 
        return exit_status();
index 473ba70a11b775160c88a3af8ee43291572955a9..e68c0471b9d7e4c49ee7568786be9dc726354586 100644 (file)
@@ -32,19 +32,19 @@ int main(void)
        origpi = tal_arr(NULL, int, 100);
        ok1(origpi);
        ok1(error_count == 0);
-       pi = tal_dup(NULL, int, origpi, (size_t)-1, 0);
+       pi = tal_dup_arr(NULL, int, origpi, (size_t)-1, 0);
        ok1(!pi);
        ok1(error_count == 1);
-       pi = tal_dup(NULL, int, origpi, 0, (size_t)-1);
+       pi = tal_dup_arr(NULL, int, origpi, 0, (size_t)-1);
        ok1(!pi);
        ok1(error_count == 2);
 
-       pi = tal_dup(NULL, int, origpi, (size_t)-1UL / sizeof(int),
+       pi = tal_dup_arr(NULL, int, origpi, (size_t)-1UL / sizeof(int),
                     (size_t)-1UL / sizeof(int));
        ok1(!pi);
        ok1(error_count == 3);
        /* This will still overflow when tal_hdr is added. */
-       pi = tal_dup(NULL, int, origpi, (size_t)-1UL / sizeof(int) / 2,
+       pi = tal_dup_arr(NULL, int, origpi, (size_t)-1UL / sizeof(int) / 2,
                     (size_t)-1UL / sizeof(int) / 2);
        ok1(!pi);
        ok1(error_count == 4);
@@ -54,20 +54,20 @@ int main(void)
        /* Now, check that with taltk() we free old one on failure. */
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
-       pi = tal_dup(NULL, int, take(origpi), (size_t)-1, 0);
+       pi = tal_dup_arr(NULL, int, take(origpi), (size_t)-1, 0);
        ok1(!pi);
        ok1(error_count == 1);
 
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
-       pi = tal_dup(NULL, int, take(origpi), 0, (size_t)-1);
+       pi = tal_dup_arr(NULL, int, take(origpi), 0, (size_t)-1);
        ok1(!pi);
        ok1(error_count == 1);
        ok1(!tal_first(NULL));
 
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
-       pi = tal_dup(NULL, int, take(origpi), (size_t)-1UL / sizeof(int),
+       pi = tal_dup_arr(NULL, int, take(origpi), (size_t)-1UL / sizeof(int),
                     (size_t)-1UL / sizeof(int));
        ok1(!pi);
        ok1(error_count == 1);
@@ -76,7 +76,7 @@ int main(void)
        origpi = tal_arr(NULL, int, 100);
        error_count = 0;
        /* This will still overflow when tal_hdr is added. */
-       pi = tal_dup(NULL, int, take(origpi), (size_t)-1UL / sizeof(int) / 2,
+       pi = tal_dup_arr(NULL, int, take(origpi), (size_t)-1UL / sizeof(int) / 2,
                     (size_t)-1UL / sizeof(int) / 2);
        ok1(!pi);
        ok1(error_count == 1);
index 94b6581749964b3331f13907d3eaaececade2754..d93304e206bdf4207cb33bc7bd5eaf12333eb982 100644 (file)
@@ -26,17 +26,17 @@ int main(void)
 
        c = tal(parent, char);
        *c = 'h';
-       c = tal_dup(parent, char, take(c), 1, 0);
+       c = tal_dup(parent, char, take(c));
        ok1(c[0] == 'h');
        ok1(tal_parent(c) == parent);
 
-       c = tal_dup(parent, char, take(c), 1, 2);
+       c = tal_dup_arr(parent, char, take(c), 1, 2);
        ok1(c[0] == 'h');
        strcpy(c, "hi");
        ok1(tal_parent(c) == parent);
 
        /* dup must reparent child. */
-       c = tal_dup(NULL, char, take(c), 1, 0);
+       c = tal_dup(NULL, char, take(c));
        ok1(c[0] == 'h');
        ok1(tal_parent(c) == NULL);
 
@@ -49,7 +49,7 @@ int main(void)
 
        /* NULL pass-through. */
        c = NULL;
-       ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL);
+       ok1(tal_dup_arr(NULL, char, take(c), 5, 5) == NULL);
        ok1(!taken_any());
 
        tal_cleanup();
index a0fe6f32a36976886ae80ef90f48bf80dfb9a887..08760333eafcb6c0d757fc6f5c2e234c0137633a 100644 (file)
@@ -104,7 +104,7 @@ static void check_depends_accurate(struct manifest *m,
        }
 
        /* Now remove NUL and append test dependencies to deps. */
-       deps = tal_dup(m, char *, take(deps), core_deps, test_deps + 2);
+       deps = tal_dup_arr(m, char *, take(deps), core_deps, test_deps + 2);
        memcpy(deps + core_deps, tdeps, sizeof(tdeps[0]) * test_deps);
        /* ccan/tap is given a free pass. */
        deps[core_deps + test_deps] = (char *)"ccan/tap";