]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/tal.c
tal: add TAL_TAKE.
[ccan] / ccan / tal / tal.c
index e575130568ca26913ad97ed00fa2feb6f4cf7740..d7b7b4b22e6ad34ad54e21f6b8bd2683d97c4337 100644 (file)
@@ -706,7 +706,12 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
 
 void *tal_memdup(const tal_t *ctx, const void *p, size_t n)
 {
 
 void *tal_memdup(const tal_t *ctx, const void *p, size_t n)
 {
-       void *ret = tal_arr(ctx, char, n);
+       void *ret;
+
+       if (ctx == TAL_TAKE)
+               return (void *)p;
+
+       ret = tal_arr(ctx, char, n);
        if (ret)
                memcpy(ret, p, n);
        return ret;
        if (ret)
                memcpy(ret, p, n);
        return ret;
@@ -727,9 +732,14 @@ char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
 {
        size_t max = strlen(fmt) * 2;
 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
 {
        size_t max = strlen(fmt) * 2;
-       char *buf = tal_arr(ctx, char, max);
+       char *buf;
        int ret;
 
        int ret;
 
+       if (ctx == TAL_TAKE)
+               buf = tal_arr(tal_parent(fmt), char, max);
+       else
+               buf = tal_arr(ctx, char, max);
+
        while (buf) {
                va_list ap2;
 
        while (buf) {
                va_list ap2;
 
@@ -741,6 +751,8 @@ char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
                        break;
                buf = tal_resize(buf, max *= 2);
        }
                        break;
                buf = tal_resize(buf, max *= 2);
        }
+       if (ctx == TAL_TAKE)
+               tal_free(fmt);
        return buf;
 }
 
        return buf;
 }