Skip to content

Commit

Permalink
ccan: upgrade to new ccan/tal and ccan/tal/str.
Browse files Browse the repository at this point in the history
The visible changes are:
1. tal_len() is renamed to tal_bytelen() for clarity.
2. tal allocations *always* know their length.
3. tal/str routines always set the length to strlen() + 1.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell authored and cdecker committed Jul 30, 2018
1 parent 337075d commit 5179025
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 202 deletions.
2 changes: 1 addition & 1 deletion ccan/README
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-2435-g92be2eff
CCAN version: init-2440-g55d81423
19 changes: 19 additions & 0 deletions ccan/ccan/structeq/test/compile_fail-expect-any-padding.c
@@ -0,0 +1,19 @@
#include <ccan/structeq/structeq.h>

struct mydata {
int start, end;
};
#ifdef FAIL
#define PADDING -1
#else
#define PADDING 0
#endif

STRUCTEQ_DEF(mydata, PADDING, start, end);

int main(void)
{
struct mydata a = { 0, 100 };

return mydata_eq(&a, &a);
}
19 changes: 19 additions & 0 deletions ccan/ccan/structeq/test/compile_fail-expect-padding.c
@@ -0,0 +1,19 @@
#include <ccan/structeq/structeq.h>

struct mydata {
int start, end;
};
#ifdef FAIL
#define PADDING 1
#else
#define PADDING 0
#endif

STRUCTEQ_DEF(mydata, PADDING, start, end);

int main(void)
{
struct mydata a = { 0, 100 };

return mydata_eq(&a, &a);
}
20 changes: 20 additions & 0 deletions ccan/ccan/structeq/test/compile_fail-unexpceted-padding.c
@@ -0,0 +1,20 @@
#include <ccan/structeq/structeq.h>

struct mydata {
int start, end;
int pad;
};
#ifdef FAIL
#define PADDING 0
#else
#define PADDING sizeof(int)
#endif

STRUCTEQ_DEF(mydata, PADDING, start, end);

int main(void)
{
struct mydata a = { 0, 100 };

return mydata_eq(&a, &a);
}
32 changes: 32 additions & 0 deletions ccan/ccan/structeq/test/run-with-padding.c
@@ -0,0 +1,32 @@
#include <ccan/structeq/structeq.h>
#include <ccan/tap/tap.h>

/* In theory, this could be generated without padding, if alignof(int) were 0,
* and test would fail. Call me when that happens. */
struct mydata {
char start;
int end;
};

STRUCTEQ_DEF(mydata, sizeof(int) - sizeof(char), start, end);

int main(void)
{
struct mydata a, b;

/* This is how many tests you plan to run */
plan_tests(3);

a.start = 0;
a.end = 100;
ok1(mydata_eq(&a, &a));

b = a;
ok1(mydata_eq(&a, &b));

b.end++;
ok1(!mydata_eq(&a, &b));

/* This exits depending on whether all tests passed */
return exit_status();
}
11 changes: 2 additions & 9 deletions ccan/ccan/tal/benchmark/samba-allocs.c
Expand Up @@ -178,15 +178,8 @@ static void do_tals(struct node *node)
unsigned int i;
static int count;

/* Tal pays a penalty for arrays, but we can't tell which is an array
* and which isn't. Grepping samba source gives 1221 talloc_array of
* 33137 talloc occurrences, so conservatively assume 1 in 16 */
if (count++ % 16 == 0)
node->n = tal_arr(node->parent ? node->parent->n : NULL,
char, node->len);
else
node->n = tal_alloc_(node->parent ? node->parent->n : NULL,
node->len, false, false, TAL_LABEL(type, ""));
node->n = tal_arr(node->parent ? node->parent->n : NULL,
char, node->len);

if (node->destructor)
tal_add_destructor(node->n, unused_tal_destructor);
Expand Down
4 changes: 3 additions & 1 deletion ccan/ccan/tal/str/_info
Expand Up @@ -7,7 +7,9 @@
*
* This is a grab bag of functions for string operations, designed to enhance
* the standard string.h; these are separated from the non-tal-needing
* string utilities in "str.h".
* string utilities in "str.h". Each string created by this library
* will have tal_count() equal to strlen() + 1 (assuming you didn't create
* a string containing a NUL, such as using tal_fmt("%c", 0)).
*
* Example:
* #include <ccan/tal/str/str.h>
Expand Down
52 changes: 29 additions & 23 deletions ccan/ccan/tal/str/str.c
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -69,6 +68,8 @@ static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap)

if (ret < max) {
ok = true;
/* Make sure tal_count() is correct! */
tal_resize(buf, off + ret + 1);
break;
}
max *= 2;
Expand All @@ -79,15 +80,15 @@ 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;

if (!fmt && taken(fmt))
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;
Expand All @@ -113,7 +114,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;
Expand All @@ -127,9 +129,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);

Expand All @@ -138,8 +138,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;
Expand Down Expand Up @@ -190,8 +191,9 @@ char **tal_strsplit(const tal_t *ctx,
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;
Expand All @@ -204,7 +206,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;

Expand All @@ -222,6 +224,8 @@ char *tal_strjoin(const tal_t *ctx,
totlen += dlen;
}
ret[totlen] = '\0';
/* Make sure tal_count() is correct! */
tal_resize(&ret, totlen+1);
out:
if (taken(strings))
tal_free(strings);
Expand Down Expand Up @@ -255,7 +259,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];
Expand Down Expand Up @@ -285,10 +290,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,
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;
Expand Down

0 comments on commit 5179025

Please sign in to comment.