Browse Source

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 <rusty@rustcorp.com.au>
Rusty Russell 10 years ago
parent
commit
f0308b852c

+ 1 - 1
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;

+ 2 - 2
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;

+ 2 - 2
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);

+ 13 - 2
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, "[]")))

+ 13 - 2
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, "[]")))

+ 8 - 8
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);

+ 4 - 4
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();

+ 8 - 8
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);

+ 4 - 4
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();

+ 1 - 1
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";