]> git.ozlabs.org Git - ccan/commitdiff
tal/str and tal/stack: use _label interfaces.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 27 Jul 2018 04:23:19 +0000 (13:53 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 27 Jul 2018 04:23:19 +0000 (13:53 +0930)
In particular, tal/str now passes through the label from the caller,
so (in case of CCAN_TAL_DEBUG) you can actually see the file and line
where the caller was, not just inside ccan/str.

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

index 00ee0166c1692a4f526f2471f91fa9c04d99ccac..aab0fd16881b9362cd6d2245d8a990206ac204a5 100644 (file)
@@ -12,7 +12,7 @@ static void _free_frame(tal_t *o)
 
 tal_t *tal_newframe_(const char *label)
 {
 
 tal_t *tal_newframe_(const char *label)
 {
-       h = tal_alloc_(h, 0, false, false, label);
+       h = tal_label(h, char, label);
        assert(h != NULL);
        tal_add_destructor(h, _free_frame);
        return h;
        assert(h != NULL);
        tal_add_destructor(h, _free_frame);
        return h;
index 4b3b11aa603cf23733ec9aa24251e602c2c7723f..b00775ab70fc4d652e8739027dc99d813a28d2c9 100644 (file)
 #include <stdio.h>
 #include <ccan/str/str.h>
 
 #include <stdio.h>
 #include <ccan/str/str.h>
 
-char *tal_strdup(const tal_t *ctx, const char *p)
+char *tal_strdup_(const tal_t *ctx, const char *p, const char *label)
 {
        /* We have to let through NULL for take(). */
 {
        /* We have to let through NULL for take(). */
-       return tal_dup_(ctx, p, 1, p ? strlen(p) + 1: 1, 0, false,
-                       TAL_LABEL(char, "[]"));
+       return tal_dup_arr_label(ctx, char, p, p ? strlen(p) + 1: 1, 0, label);
 }
 
 }
 
-char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
+char *tal_strndup_(const tal_t *ctx, const char *p, size_t n, const char *label)
 {
        size_t len;
        char *ret;
 {
        size_t len;
        char *ret;
@@ -30,19 +29,19 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
        else
                len = n;
 
        else
                len = n;
 
-       ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]"));
+       ret = tal_dup_arr_label(ctx, char, p, len, 1, label);
        if (ret)
                ret[len] = '\0';
        return ret;
 }
 
        if (ret)
                ret[len] = '\0';
        return ret;
 }
 
-char *tal_fmt(const tal_t *ctx, const char *fmt, ...)
+char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt, ...)
 {
        va_list ap;
        char *ret;
 
        va_start(ap, fmt);
 {
        va_list ap;
        char *ret;
 
        va_start(ap, fmt);
-       ret = tal_vfmt(ctx, fmt, ap);
+       ret = tal_vfmt_(ctx, fmt, ap, label);
        va_end(ap);
 
        return ret;
        va_end(ap);
 
        return ret;
@@ -79,7 +78,7 @@ static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap)
        return ok;
 }
 
        return ok;
 }
 
-char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap)
+char *tal_vfmt_(const tal_t *ctx, const char *fmt, va_list ap, const char *label)
 {
        char *buf;
 
 {
        char *buf;
 
@@ -87,7 +86,7 @@ char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap)
                return NULL;
 
        /* A decent guess to start. */
                return NULL;
 
        /* A decent guess to start. */
-       buf = tal_arr(ctx, char, strlen(fmt) * 2);
+       buf = tal_arr_label(ctx, char, strlen(fmt) * 2, label);
        if (!do_vfmt(&buf, 0, fmt, ap))
                buf = tal_free(buf);
        return buf;
        if (!do_vfmt(&buf, 0, fmt, ap))
                buf = tal_free(buf);
        return buf;
@@ -113,7 +112,8 @@ bool tal_append_fmt(char **baseptr, const char *fmt, ...)
        return ret;
 }
 
        return ret;
 }
 
-char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2)
+char *tal_strcat_(const tal_t *ctx, const char *s1, const char *s2,
+                 const char *label)
 {
        size_t len1, len2;
        char *ret;
 {
        size_t len1, len2;
        char *ret;
@@ -127,9 +127,7 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2)
        len1 = s1 ? strlen(s1) : 0;
        len2 = strlen(s2);
 
        len1 = s1 ? strlen(s1) : 0;
        len2 = strlen(s2);
 
-       /* We use tal_dup_ here to avoid attaching a length property. */
-       ret = tal_dup_(ctx, s1, 1, len1, len2 + 1, false,
-                      TAL_LABEL(char, "[]"));
+       ret = tal_dup_arr_label(ctx, char, s1, len1, len2 + 1, label);
        if (likely(ret))
                memcpy(ret + len1, s2, len2 + 1);
 
        if (likely(ret))
                memcpy(ret + len1, s2, len2 + 1);
 
@@ -138,8 +136,9 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2)
        return ret;
 }
 
        return ret;
 }
 
-char **tal_strsplit(const tal_t *ctx,
-                   const char *string, const char *delims, enum strsplit flags)
+char **tal_strsplit_(const tal_t *ctx,
+                    const char *string, const char *delims, enum strsplit flags,
+                    const char *label)
 {
        char **parts, *str;
        size_t max = 64, num = 0;
 {
        char **parts, *str;
        size_t max = 64, num = 0;
@@ -190,8 +189,9 @@ fail:
        return NULL;
 }
 
        return NULL;
 }
 
-char *tal_strjoin(const tal_t *ctx,
-                 char *strings[], const char *delim, enum strjoin flags)
+char *tal_strjoin_(const tal_t *ctx,
+                  char *strings[], const char *delim, enum strjoin flags,
+                  const char *label)
 {
        unsigned int i;
        char *ret = NULL;
 {
        unsigned int i;
        char *ret = NULL;
@@ -204,7 +204,7 @@ char *tal_strjoin(const tal_t *ctx,
                goto fail;
 
        dlen = strlen(delim);
                goto fail;
 
        dlen = strlen(delim);
-       ret = tal_arr(ctx, char, dlen*2+1);
+       ret = tal_arr_label(ctx, char, dlen*2+1, label);
        if (!ret)
                goto fail;
 
        if (!ret)
                goto fail;
 
@@ -255,7 +255,8 @@ static size_t count_open_braces(const char *string)
 #endif
 }
 
 #endif
 }
 
-bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...)
+bool tal_strreg_(const tal_t *ctx, const char *string, const char *label,
+                const char *regex, ...)
 {
        size_t nmatch = 1 + count_open_braces(regex);
        regmatch_t matches[nmatch];
 {
        size_t nmatch = 1 + count_open_braces(regex);
        regmatch_t matches[nmatch];
@@ -285,10 +286,11 @@ bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...)
                        if (matches[i].rm_so == -1)
                                *arg = NULL;
                        else {
                        if (matches[i].rm_so == -1)
                                *arg = NULL;
                        else {
-                               *arg = tal_strndup(ctx,
-                                                  string + matches[i].rm_so,
-                                                  matches[i].rm_eo
-                                                  - matches[i].rm_so);
+                               *arg = tal_strndup_(ctx,
+                                                   string + matches[i].rm_so,
+                                                   matches[i].rm_eo
+                                                   - matches[i].rm_so,
+                                                   label);
                                /* FIXME: If we fail, we set some and leak! */
                                if (!*arg) {
                                        ret = false;
                                /* FIXME: If we fail, we set some and leak! */
                                if (!*arg) {
                                        ret = false;
index ec853defccf9804b42a02879070c12faa493d06b..bdcb27ea3f8e18b398e81ba2f876154d6118b092 100644 (file)
@@ -14,7 +14,8 @@
  * @ctx: NULL, or tal allocated object to be parent.
  * @p: the string to copy (can be take()).
  */
  * @ctx: NULL, or tal allocated object to be parent.
  * @p: the string to copy (can be take()).
  */
-char *tal_strdup(const tal_t *ctx, const char *p TAKES);
+#define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
+char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
 
 /**
  * tal_strndup - duplicate a limited amount of a string.
 
 /**
  * tal_strndup - duplicate a limited amount of a string.
@@ -24,14 +25,19 @@ char *tal_strdup(const tal_t *ctx, const char *p TAKES);
  *
  * Always gives a nul-terminated string, with strlen() <= @n.
  */
  *
  * Always gives a nul-terminated string, with strlen() <= @n.
  */
-char *tal_strndup(const tal_t *ctx, const char *p TAKES, size_t n);
+#define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
+char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
+                  const char *label);
 
 /**
  * tal_fmt - allocate a formatted string
  * @ctx: NULL, or tal allocated object to be parent.
  * @fmt: the printf-style format (can be take()).
  */
 
 /**
  * tal_fmt - allocate a formatted string
  * @ctx: NULL, or tal allocated object to be parent.
  * @fmt: the printf-style format (can be take()).
  */
-char *tal_fmt(const tal_t *ctx, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
+#define tal_fmt(ctx, ...)                               \
+       tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
+char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
+               ...) PRINTF_FMT(3,4);
 
 /**
  * tal_vfmt - allocate a formatted string (va_list version)
 
 /**
  * tal_vfmt - allocate a formatted string (va_list version)
@@ -39,7 +45,10 @@ char *tal_fmt(const tal_t *ctx, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
  * @fmt: the printf-style format (can be take()).
  * @va: the va_list containing the format args.
  */
  * @fmt: the printf-style format (can be take()).
  * @va: the va_list containing the format args.
  */
-char *tal_vfmt(const tal_t *ctx, const char *fmt TAKES, va_list ap)
+#define tal_vfmt(ctx, fmt, va)                         \
+       tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
+char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
+               const char *label)
        PRINTF_FMT(2,0);
 
 /**
        PRINTF_FMT(2,0);
 
 /**
@@ -67,7 +76,9 @@ bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
  * @s1: the first string (can be take()).
  * @s2: the second string (can be take()).
  */
  * @s1: the first string (can be take()).
  * @s2: the second string (can be take()).
  */
-char *tal_strcat(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES);
+#define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
+char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
+                 const char *label);
 
 enum strsplit {
        STR_EMPTY_OK,
 
 enum strsplit {
        STR_EMPTY_OK,
@@ -109,10 +120,13 @@ enum strsplit {
  *             return long_lines;
  *     }
  */
  *             return long_lines;
  *     }
  */
-char **tal_strsplit(const tal_t *ctx,
-                   const char *string TAKES,
-                   const char *delims TAKES,
-                   enum strsplit flag);
+#define tal_strsplit(ctx, string, delims, flag)        \
+       tal_strsplit_(ctx, string, delims, flag, TAL_LABEL(char *, "[]"))
+char **tal_strsplit_(const tal_t *ctx,
+                    const char *string TAKES,
+                    const char *delims TAKES,
+                    enum strsplit flag,
+                    const char *label);
 
 enum strjoin {
        STR_TRAIL,
 
 enum strjoin {
        STR_TRAIL,
@@ -142,10 +156,13 @@ enum strjoin {
  *             return ret;
  *     }
  */
  *             return ret;
  *     }
  */
-char *tal_strjoin(const void *ctx,
-                 char *strings[] TAKES,
-                 const char *delim TAKES,
-                 enum strjoin flags);
+#define tal_strjoin(ctx, strings, delim, flags)                                \
+       tal_strjoin_(ctx, strings, delim, flags, TAL_LABEL(char, "[]"))
+char *tal_strjoin_(const void *ctx,
+                  char *strings[] TAKES,
+                  const char *delim TAKES,
+                  enum strjoin flags,
+                  const char *label);
 
 /**
  * tal_strreg - match/extract from a string via (extended) regular expressions.
 
 /**
  * tal_strreg - match/extract from a string via (extended) regular expressions.
@@ -187,6 +204,8 @@ char *tal_strjoin(const void *ctx,
  *             return 0;
  *     }
  */
  *             return 0;
  *     }
  */
-bool tal_strreg(const void *ctx, const char *string TAKES,
-               const char *regex TAKES, ...);
+#define tal_strreg(ctx, string, ...)                                   \
+       tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__)
+bool tal_strreg_(const void *ctx, const char *string TAKES,
+                const char *label, const char *regex, ...);
 #endif /* CCAN_STR_TAL_H */
 #endif /* CCAN_STR_TAL_H */