From: Rusty Russell Date: Thu, 28 May 2015 10:25:42 +0000 (+0930) Subject: tal: rename tal_dup to tal_dup_arr, make tal_dup simpler. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=f0308b852cda745a74f5135971ad6c0747886df1 tal: rename tal_dup to tal_dup_arr, make tal_dup simpler. 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 --- diff --git a/ccan/invbloom/invbloom.c b/ccan/invbloom/invbloom.c index 28dabec6..d34ec1fe 100644 --- a/ccan/invbloom/invbloom.c +++ b/ccan/invbloom/invbloom.c @@ -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; diff --git a/ccan/tal/path/path.c b/ccan/tal/path/path.c index a0dbe291..93362c75 100644 --- a/ccan/tal/path/path.c +++ b/ccan/tal/path/path.c @@ -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; diff --git a/ccan/tal/str/test/run-string.c b/ccan/tal/str/test/run-string.c index 8d596e46..533ad012 100644 --- a/ccan/tal/str/test/run-string.c +++ b/ccan/tal/str/test/run-string.c @@ -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); diff --git a/ccan/tal/tal.h b/ccan/tal/tal.h index 3da1e420..0cfea984 100644 --- a/ccan/tal/tal.h +++ b/ccan/tal/tal.h @@ -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, "[]"))) diff --git a/ccan/tal/talloc/talloc.h b/ccan/tal/talloc/talloc.h index 718f12c4..8dfb80e7 100644 --- a/ccan/tal/talloc/talloc.h +++ b/ccan/tal/talloc/talloc.h @@ -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, "[]"))) diff --git a/ccan/tal/talloc/test/run-overflow.c b/ccan/tal/talloc/test/run-overflow.c index c7f9c18f..e555b359 100644 --- a/ccan/tal/talloc/test/run-overflow.c +++ b/ccan/tal/talloc/test/run-overflow.c @@ -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); diff --git a/ccan/tal/talloc/test/run-take.c b/ccan/tal/talloc/test/run-take.c index 7283c16e..4939b8af 100644 --- a/ccan/tal/talloc/test/run-take.c +++ b/ccan/tal/talloc/test/run-take.c @@ -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(); diff --git a/ccan/tal/test/run-overflow.c b/ccan/tal/test/run-overflow.c index 473ba70a..e68c0471 100644 --- a/ccan/tal/test/run-overflow.c +++ b/ccan/tal/test/run-overflow.c @@ -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); diff --git a/ccan/tal/test/run-take.c b/ccan/tal/test/run-take.c index 94b65817..d93304e2 100644 --- a/ccan/tal/test/run-take.c +++ b/ccan/tal/test/run-take.c @@ -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(); diff --git a/tools/ccanlint/tests/depends_accurate.c b/tools/ccanlint/tests/depends_accurate.c index a0fe6f32..08760333 100644 --- a/tools/ccanlint/tests/depends_accurate.c +++ b/tools/ccanlint/tests/depends_accurate.c @@ -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";