Commit 38ec541c authored by Rusty Russell's avatar Rusty Russell

tal/str and tal/stack: use _label interfaces.

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: default avatarRusty Russell <rusty@rustcorp.com.au>
parent ed39cbbd
...@@ -12,7 +12,7 @@ static void _free_frame(tal_t *o) ...@@ -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); assert(h != NULL);
tal_add_destructor(h, _free_frame); tal_add_destructor(h, _free_frame);
return h; return h;
......
...@@ -12,14 +12,13 @@ ...@@ -12,14 +12,13 @@
#include <stdio.h> #include <stdio.h>
#include <ccan/str/str.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, return tal_dup_arr_label(ctx, char, p, p ? strlen(p) + 1: 1, 0, label);
TAL_LABEL(char, "[]"));
} }
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; size_t len;
char *ret; char *ret;
...@@ -30,19 +29,19 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n) ...@@ -30,19 +29,19 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
else else
len = n; 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) if (ret)
ret[len] = '\0'; ret[len] = '\0';
return ret; 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; va_list ap;
char *ret; char *ret;
va_start(ap, fmt); va_start(ap, fmt);
ret = tal_vfmt(ctx, fmt, ap); ret = tal_vfmt_(ctx, fmt, ap, label);
va_end(ap); va_end(ap);
return ret; return ret;
...@@ -79,7 +78,7 @@ static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap) ...@@ -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) ...@@ -87,7 +86,7 @@ char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap)
return NULL; return NULL;
/* A decent guess to start. */ /* 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)) if (!do_vfmt(&buf, 0, fmt, ap))
buf = tal_free(buf); buf = tal_free(buf);
return buf; return buf;
...@@ -113,7 +112,8 @@ bool tal_append_fmt(char **baseptr, const char *fmt, ...) ...@@ -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; size_t len1, len2;
char *ret; char *ret;
...@@ -127,9 +127,7 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2) ...@@ -127,9 +127,7 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2)
len1 = s1 ? strlen(s1) : 0; len1 = s1 ? strlen(s1) : 0;
len2 = strlen(s2); len2 = strlen(s2);
/* We use tal_dup_ here to avoid attaching a length property. */ ret = tal_dup_arr_label(ctx, char, s1, len1, len2 + 1, label);
ret = tal_dup_(ctx, s1, 1, len1, len2 + 1, false,
TAL_LABEL(char, "[]"));
if (likely(ret)) if (likely(ret))
memcpy(ret + len1, s2, len2 + 1); memcpy(ret + len1, s2, len2 + 1);
...@@ -138,8 +136,9 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2) ...@@ -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, char **tal_strsplit_(const tal_t *ctx,
const char *string, const char *delims, enum strsplit flags) const char *string, const char *delims, enum strsplit flags,
const char *label)
{ {
char **parts, *str; char **parts, *str;
size_t max = 64, num = 0; size_t max = 64, num = 0;
...@@ -190,8 +189,9 @@ fail: ...@@ -190,8 +189,9 @@ fail:
return NULL; return NULL;
} }
char *tal_strjoin(const tal_t *ctx, char *tal_strjoin_(const tal_t *ctx,
char *strings[], const char *delim, enum strjoin flags) char *strings[], const char *delim, enum strjoin flags,
const char *label)
{ {
unsigned int i; unsigned int i;
char *ret = NULL; char *ret = NULL;
...@@ -204,7 +204,7 @@ char *tal_strjoin(const tal_t *ctx, ...@@ -204,7 +204,7 @@ char *tal_strjoin(const tal_t *ctx,
goto fail; goto fail;
dlen = strlen(delim); dlen = strlen(delim);
ret = tal_arr(ctx, char, dlen*2+1); ret = tal_arr_label(ctx, char, dlen*2+1, label);
if (!ret) if (!ret)
goto fail; goto fail;
...@@ -255,7 +255,8 @@ static size_t count_open_braces(const char *string) ...@@ -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); size_t nmatch = 1 + count_open_braces(regex);
regmatch_t matches[nmatch]; regmatch_t matches[nmatch];
...@@ -285,10 +286,11 @@ bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...) ...@@ -285,10 +286,11 @@ bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...)
if (matches[i].rm_so == -1) if (matches[i].rm_so == -1)
*arg = NULL; *arg = NULL;
else { else {
*arg = tal_strndup(ctx, *arg = tal_strndup_(ctx,
string + matches[i].rm_so, string + matches[i].rm_so,
matches[i].rm_eo matches[i].rm_eo
- matches[i].rm_so); - matches[i].rm_so,
label);
/* FIXME: If we fail, we set some and leak! */ /* FIXME: If we fail, we set some and leak! */
if (!*arg) { if (!*arg) {
ret = false; ret = false;
......
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @p: the string to copy (can be take()). * @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); ...@@ -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 * tal_fmt - allocate a formatted string
* @ctx: NULL, or tal allocated object to be parent. * @ctx: NULL, or tal allocated object to be parent.
* @fmt: the printf-style format (can be take()). * @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); ...@@ -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()). * @fmt: the printf-style format (can be take()).
* @va: the va_list containing the format args. * @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); ...@@ -67,7 +76,9 @@ bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
* @s1: the first string (can be take()). * @s1: the first string (can be take()).
* @s2: the second 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 { enum strsplit {
STR_EMPTY_OK, STR_EMPTY_OK,
...@@ -109,10 +120,13 @@ enum strsplit { ...@@ -109,10 +120,13 @@ enum strsplit {
* return long_lines; * return long_lines;
* } * }
*/ */
char **tal_strsplit(const tal_t *ctx, #define tal_strsplit(ctx, string, delims, flag) \
const char *string TAKES, tal_strsplit_(ctx, string, delims, flag, TAL_LABEL(char *, "[]"))
const char *delims TAKES, char **tal_strsplit_(const tal_t *ctx,
enum strsplit flag); const char *string TAKES,
const char *delims TAKES,
enum strsplit flag,
const char *label);
enum strjoin { enum strjoin {
STR_TRAIL, STR_TRAIL,
...@@ -142,10 +156,13 @@ enum strjoin { ...@@ -142,10 +156,13 @@ enum strjoin {
* return ret; * return ret;
* } * }
*/ */
char *tal_strjoin(const void *ctx, #define tal_strjoin(ctx, strings, delim, flags) \
char *strings[] TAKES, tal_strjoin_(ctx, strings, delim, flags, TAL_LABEL(char, "[]"))
const char *delim TAKES, char *tal_strjoin_(const void *ctx,
enum strjoin flags); 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, ...@@ -187,6 +204,8 @@ char *tal_strjoin(const void *ctx,
* return 0; * return 0;
* } * }
*/ */
bool tal_strreg(const void *ctx, const char *string TAKES, #define tal_strreg(ctx, string, ...) \
const char *regex TAKES, ...); 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 */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment