tal: make tal_free() return NULL
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 21 Nov 2012 23:38:50 +0000 (10:08 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 21 Nov 2012 23:38:50 +0000 (10:08 +1030)
This makes it convenient to do:

ptr = tal_free(ptr);

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal/tal.c
ccan/tal/tal.h
ccan/tal/test/run.c

index dce9d233a076733f636f42fbc2e02f0956cd364a..5ab6cb682cd5349b060b8731e99d9f18c45bb15e 100644 (file)
@@ -493,18 +493,17 @@ static struct tal_hdr *remove_node(struct tal_hdr *t)
        return NULL;
 }
 
-void tal_free(const tal_t *ctx)
+void *tal_free(const tal_t *ctx)
 {
-        struct tal_hdr *t;
-       int saved_errno = errno;
-
-        if (!ctx)
-                return;
-
-        t = debug_tal(to_tal_hdr(ctx));
-        remove_node(t);
-        del_tree(t);
-       errno = saved_errno;
+        if (ctx) {
+               struct tal_hdr *t;
+               int saved_errno = errno;
+               t = debug_tal(to_tal_hdr(ctx));
+               remove_node(t);
+               del_tree(t);
+               errno = saved_errno;
+       }
+       return NULL;
 }
 
 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
@@ -757,14 +756,10 @@ void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
        if (taken(p)) {
                if (unlikely(!p))
                        return NULL;
-               if (unlikely(!tal_resize_((void **)&p, n + extra))) {
-                       tal_free(p);
-                       return NULL;
-               }
-               if (unlikely(!tal_steal(ctx, p))) {
-                       tal_free(p);
-                       return NULL;
-               }
+               if (unlikely(!tal_resize_((void **)&p, n + extra)))
+                       return tal_free(p);
+               if (unlikely(!tal_steal(ctx, p)))
+                       return tal_free(p);
                return (void *)p;
        }
        ret = tal_alloc_(ctx, n + extra, false, label);
@@ -806,10 +801,8 @@ char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
 
                if (ret < max)
                        break;
-               if (!tal_resize(&buf, max *= 2)) {
-                       tal_free(buf);
-                       buf = NULL;
-               }
+               if (!tal_resize(&buf, max *= 2))
+                       buf = tal_free(buf);
        }
        if (taken(fmt))
                tal_free(fmt);
index 1ed15c75f46fd664b6f57b10c65bcd8eb9790406..42245f68660277c990561502f50b00f94297bcb5 100644 (file)
@@ -53,14 +53,15 @@ typedef void tal_t;
  * @p: NULL, or tal allocated object to free.
  *
  * This calls the destructors for p (if any), then does the same for all its
- * children (recursively) before finally freeing the memory.
+ * children (recursively) before finally freeing the memory.  It returns
+ * NULL, for convenience.
  *
  * Note: errno is preserved by this call.
  *
  * Example:
- *     tal_free(p);
+ *     p = tal_free(p);
  */
-void tal_free(const tal_t *p);
+void *tal_free(const tal_t *p);
 
 /**
  * tal_arr - allocate an array of objects.
index 3d3799c55492b989a59720d98423b7ccfbee1f9e..ab4341f2c93f443bfdc6b61f84ce57c4fa45be2b 100644 (file)
@@ -7,7 +7,10 @@ int main(void)
        char *parent, *c[4], *p;
        int i, j;
 
-       plan_tests(12);
+       plan_tests(14);
+
+       /* tal_free(NULL) works. */
+       ok1(tal_free(NULL) == NULL);
 
        parent = tal(NULL, char);
        ok1(parent);
@@ -33,7 +36,7 @@ int main(void)
        ok1(*c[3] == '1');
 
        /* Free parent. */
-       tal_free(parent);
+       ok1(tal_free(parent) == NULL);
 
        parent = tal(NULL, char);