]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/tal.c
tal: add tal_expand().
[ccan] / ccan / tal / tal.c
index c78185be01aa12715a0499088974ab8f561ccee6..5c0aec07990dcbafc44b5e7bf075ac8c68ba5641 100644 (file)
@@ -5,7 +5,6 @@
 #include <ccan/take/take.h>
 #include <assert.h>
 #include <stdio.h>
-#include <stdarg.h>
 #include <stddef.h>
 #include <string.h>
 #include <limits.h>
@@ -713,29 +712,34 @@ bool tal_resize_(tal_t **ctxp, size_t size, size_t count)
        return true;
 }
 
-char *tal_strdup(const tal_t *ctx, const char *p)
+bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
 {
-       /* We have to let through NULL for take(). */
-       return tal_dup_(ctx, p, 1, p ? strlen(p) + 1: 1, 0, false,
-                       TAL_LABEL(char, "[]"));
-}
+       struct length *l;
+       bool ret = false;
 
-char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
-{
-       size_t len;
-       char *ret;
+       l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
 
-       /* We have to let through NULL for take(). */
-       if (likely(p)) {
-               len = strlen(p);
-               if (len > n)
-                       len = n;
-       } else
-               len = n;
+       /* Check for additive overflow */
+       if (l->count + count < count) {
+               call_error("dup size overflow");
+               goto out;
+       }
 
-       ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]"));
-       if (ret)
-               ret[len] = '\0';
+       /* Don't point src inside thing we're expanding! */
+       assert(src < *ctxp
+              || (char *)src >= (char *)(*ctxp) + (size * l->count));
+
+       /* Note: updates l->count. */
+       if (!tal_resize_(ctxp, size, l->count + count))
+               goto out;
+
+       memcpy((char *)*ctxp + size * (l->count - count),
+              src, count * size);
+       ret = true;
+
+out:
+       if (taken(src))
+               tal_free(src);
        return ret;
 }
 
@@ -776,47 +780,6 @@ void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
        return ret;
 }
 
-char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
-{
-       va_list ap;
-       char *ret;
-
-       va_start(ap, fmt);
-       ret = tal_vasprintf(ctx, fmt, ap);
-       va_end(ap);
-
-       return ret;
-}
-
-char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
-{
-       size_t max;
-       char *buf;
-       int ret;
-
-       if (!fmt && taken(fmt))
-               return NULL;
-
-       /* A decent guess to start. */
-       max = strlen(fmt) * 2;
-       buf = tal_arr(ctx, char, max);
-       while (buf) {
-               va_list ap2;
-
-               va_copy(ap2, ap);
-               ret = vsnprintf(buf, max, fmt, ap2);
-               va_end(ap2);
-
-               if (ret < max)
-                       break;
-               if (!tal_resize(&buf, max *= 2))
-                       buf = tal_free(buf);
-       }
-       if (taken(fmt))
-               tal_free(fmt);
-       return buf;
-}
-
 void tal_set_backend(void *(*alloc_fn)(size_t size),
                     void *(*resize_fn)(void *, size_t size),
                     void (*free_fn)(void *),