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)
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;
......
......@@ -12,14 +12,13 @@
#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(). */
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;
......@@ -30,19 +29,19 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t 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;
}
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);
ret = tal_vfmt(ctx, fmt, ap);
ret = tal_vfmt_(ctx, fmt, ap, label);
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;
}
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;
......@@ -87,7 +86,7 @@ char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap)
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;
......@@ -113,7 +112,8 @@ bool tal_append_fmt(char **baseptr, const char *fmt, ...)
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;
......@@ -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);
/* 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);
......@@ -138,8 +136,9 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2)
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;
......@@ -190,8 +189,9 @@ fail:
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;
......@@ -204,7 +204,7 @@ char *tal_strjoin(const tal_t *ctx,
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;
......@@ -255,7 +255,8 @@ static size_t count_open_braces(const char *string)
#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];
......@@ -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 {
*arg = tal_strndup(ctx,
*arg = tal_strndup_(ctx,
string + matches[i].rm_so,
matches[i].rm_eo
- matches[i].rm_so);
- matches[i].rm_so,
label);
/* FIXME: If we fail, we set some and leak! */
if (!*arg) {
ret = false;
......
......@@ -14,7 +14,8 @@
* @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.
......@@ -24,14 +25,19 @@ char *tal_strdup(const tal_t *ctx, const char *p TAKES);
*
* 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()).
*/
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)
......@@ -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.
*/
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);
/**
......@@ -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()).
*/
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,
......@@ -109,10 +120,13 @@ enum strsplit {
* return long_lines;
* }
*/
char **tal_strsplit(const tal_t *ctx,
#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);
enum strsplit flag,
const char *label);
enum strjoin {
STR_TRAIL,
......@@ -142,10 +156,13 @@ enum strjoin {
* return ret;
* }
*/
char *tal_strjoin(const void *ctx,
#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);
enum strjoin flags,
const char *label);
/**
* tal_strreg - match/extract from a string via (extended) regular expressions.
......@@ -187,6 +204,8 @@ char *tal_strjoin(const void *ctx,
* 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 */
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