Commit 1651e25e authored by Rusty Russell's avatar Rusty Russell

ccan/tal: always include a length field.

The current semantics of tal_count() / tal_bytelen() are to return 0
for anything not allocated using tal_arr*.  This is because we tried
to save a native-length word in the header, but produces an awkward
API.

(To make it worse, defining CCAN_TAL_DEBUG turns length to always on,
and we enable that for c-lightning developer mode, which hides bugs!).

However, for c-lightning, just over half of allocations want a length:
these use 3 words each, so we're actually worse off overall.

The answer is to always have a length field in the header.  This also
simplfies the tal code.

samba-allocs stats before:
Tal time: 1237102-1305755(1.251e+06+/-2.1e+04)ns
Tal_free time: 1346871-1514514(1.37844e+06+/-5.2e+04)ns

After:
Tal time: 1115180-1180633(1.1351e+06+/-2.1e+04)ns
Tal_free time: 1334381-1465933(1.39148e+06+/-4.7e+04)ns
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 38ec541c
...@@ -178,15 +178,8 @@ static void do_tals(struct node *node) ...@@ -178,15 +178,8 @@ static void do_tals(struct node *node)
unsigned int i; unsigned int i;
static int count; 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, node->n = tal_arr(node->parent ? node->parent->n : NULL,
char, node->len); char, node->len);
else
node->n = tal_alloc_(node->parent ? node->parent->n : NULL,
node->len, false, false, TAL_LABEL(type, ""));
if (node->destructor) if (node->destructor)
tal_add_destructor(node->n, unused_tal_destructor); tal_add_destructor(node->n, unused_tal_destructor);
......
...@@ -24,7 +24,6 @@ enum prop_type { ...@@ -24,7 +24,6 @@ enum prop_type {
CHILDREN = 0x00c1d500, CHILDREN = 0x00c1d500,
NAME = 0x00111100, NAME = 0x00111100,
NOTIFIER = 0x00071f00, NOTIFIER = 0x00071f00,
LENGTH = 0x00515300
}; };
struct tal_hdr { struct tal_hdr {
...@@ -32,6 +31,7 @@ struct tal_hdr { ...@@ -32,6 +31,7 @@ struct tal_hdr {
struct prop_hdr *prop; struct prop_hdr *prop;
/* XOR with TAL_PTR_OBFUSTICATOR */ /* XOR with TAL_PTR_OBFUSTICATOR */
intptr_t parent_child; intptr_t parent_child;
size_t bytelen;
}; };
struct prop_hdr { struct prop_hdr {
...@@ -50,11 +50,6 @@ struct name { ...@@ -50,11 +50,6 @@ struct name {
char name[]; char name[];
}; };
struct length {
struct prop_hdr hdr; /* LENGTH */
size_t len;
};
struct notifier { struct notifier {
struct prop_hdr hdr; /* NOTIFIER */ struct prop_hdr hdr; /* NOTIFIER */
enum tal_notify_type types; enum tal_notify_type types;
...@@ -77,7 +72,7 @@ static struct { ...@@ -77,7 +72,7 @@ static struct {
struct tal_hdr hdr; struct tal_hdr hdr;
struct children c; struct children c;
} null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list }, } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
&null_parent.c.hdr, TAL_PTR_OBFUSTICATOR }, &null_parent.c.hdr, TAL_PTR_OBFUSTICATOR, 0 },
{ { CHILDREN, NULL }, { { CHILDREN, NULL },
&null_parent.hdr, &null_parent.hdr,
{ { &null_parent.c.children.n, { { &null_parent.c.children.n,
...@@ -414,50 +409,23 @@ static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno) ...@@ -414,50 +409,23 @@ static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
/* Finally free our properties. */ /* Finally free our properties. */
for (p = t->prop; p && !is_literal(p); p = next) { for (p = t->prop; p && !is_literal(p); p = next) {
next = p->next; next = p->next;
/* LENGTH is appended, so don't free separately! */
if (p->type != LENGTH)
freefn(p); freefn(p);
} }
freefn(t); freefn(t);
} }
static size_t extra_for_length(size_t size) void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
{
size_t extra;
const size_t align = ALIGNOF(struct length);
/* Round up size, and add tailer. */
extra = ((size + align-1) & ~(align-1)) - size;
extra += sizeof(struct length);
return extra;
}
void *tal_alloc_(const tal_t *ctx, size_t size,
bool clear, bool add_length, const char *label)
{ {
size_t req_size = size;
struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx)); struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
#ifdef CCAN_TAL_DEBUG
/* Always record length if debugging. */
add_length = true;
#endif
if (add_length)
size += extra_for_length(size);
child = allocate(sizeof(struct tal_hdr) + size); child = allocate(sizeof(struct tal_hdr) + size);
if (!child) if (!child)
return NULL; return NULL;
if (clear) if (clear)
memset(from_tal_hdr(child), 0, req_size); memset(from_tal_hdr(child), 0, size);
child->prop = (void *)label; child->prop = (void *)label;
child->bytelen = size;
if (add_length) {
struct length *lprop;
lprop = (struct length *)((char *)(child+1) + size) - 1;
init_property(&lprop->hdr, child, LENGTH);
lprop->len = req_size;
}
if (!add_child(parent, child)) { if (!add_child(parent, child)) {
freefn(child); freefn(child);
return NULL; return NULL;
...@@ -470,7 +438,7 @@ void *tal_alloc_(const tal_t *ctx, size_t size, ...@@ -470,7 +438,7 @@ void *tal_alloc_(const tal_t *ctx, size_t size,
static bool adjust_size(size_t *size, size_t count) static bool adjust_size(size_t *size, size_t count)
{ {
const size_t extra = sizeof(struct tal_hdr) + sizeof(struct length)*2; const size_t extra = sizeof(struct tal_hdr);
/* Multiplication wrap */ /* Multiplication wrap */
if (count && unlikely(*size * count / *size != count)) if (count && unlikely(*size * count / *size != count))
...@@ -478,7 +446,7 @@ static bool adjust_size(size_t *size, size_t count) ...@@ -478,7 +446,7 @@ static bool adjust_size(size_t *size, size_t count)
*size *= count; *size *= count;
/* Make sure we don't wrap adding header/tailer. */ /* Make sure we don't wrap adding header. */
if (*size + extra < extra) if (*size + extra < extra)
goto overflow; goto overflow;
return true; return true;
...@@ -488,12 +456,12 @@ overflow: ...@@ -488,12 +456,12 @@ overflow:
} }
void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear, void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
bool add_length, const char *label) const char *label)
{ {
if (!adjust_size(&size, count)) if (!adjust_size(&size, count))
return NULL; return NULL;
return tal_alloc_(ctx, size, clear, add_length, label); return tal_alloc_(ctx, size, clear, label);
} }
void *tal_free(const tal_t *ctx) void *tal_free(const tal_t *ctx)
...@@ -658,15 +626,10 @@ const char *tal_name(const tal_t *t) ...@@ -658,15 +626,10 @@ const char *tal_name(const tal_t *t)
size_t tal_bytelen(const tal_t *ptr) size_t tal_bytelen(const tal_t *ptr)
{ {
struct length *l; /* NULL -> null_parent which has bytelen 0 */
struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ptr));
if (!ptr)
return 0;
l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH); return t->bytelen;
if (!l)
return 0;
return l->len;
} }
/* Start one past first child: make stopping natural in circ. list. */ /* Start one past first child: make stopping natural in circ. list. */
...@@ -720,52 +683,27 @@ bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear) ...@@ -720,52 +683,27 @@ bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
{ {
struct tal_hdr *old_t, *t; struct tal_hdr *old_t, *t;
struct children *child; struct children *child;
struct prop_hdr **lenp;
struct length len;
size_t extra = 0;
old_t = debug_tal(to_tal_hdr(*ctxp)); old_t = debug_tal(to_tal_hdr(*ctxp));
if (!adjust_size(&size, count)) if (!adjust_size(&size, count))
return false; return false;
lenp = find_property_ptr(old_t, LENGTH); t = resizefn(old_t, sizeof(struct tal_hdr) + size);
if (lenp) {
/* Copy here, in case we're shrinking! */
len = *(struct length *)*lenp;
extra = extra_for_length(size);
} else /* If we don't have an old length, we can't clear! */
assert(!clear);
t = resizefn(old_t, sizeof(struct tal_hdr) + size + extra);
if (!t) { if (!t) {
call_error("Reallocation failure"); call_error("Reallocation failure");
return false; return false;
} }
/* Copy length to end. */
if (lenp) {
struct length *new_len;
/* Clear between old end and new end. */ /* Clear between old end and new end. */
if (clear && size > len.len) { if (clear && size > t->bytelen) {
char *old_end = (char *)(t + 1) + len.len; char *old_end = (char *)(t + 1) + t->bytelen;
memset(old_end, 0, size - len.len); memset(old_end, 0, size - t->bytelen);
}
new_len = (struct length *)((char *)(t + 1) + size
+ extra - sizeof(len));
len.len = size;
*new_len = len;
/* Be careful replacing next ptr; could be old hdr. */
if (lenp == &old_t->prop)
t->prop = &new_len->hdr;
else
*lenp = &new_len->hdr;
} }
update_bounds(t, sizeof(struct tal_hdr) + size + extra); /* Update length. */
t->bytelen = size;
update_bounds(t, sizeof(struct tal_hdr) + size);
/* If it didn't move, we're done! */ /* If it didn't move, we're done! */
if (t != old_t) { if (t != old_t) {
...@@ -790,12 +728,10 @@ bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear) ...@@ -790,12 +728,10 @@ bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count) bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
{ {
struct length *l;
size_t old_len; size_t old_len;
bool ret = false; bool ret = false;
l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH); old_len = debug_tal(to_tal_hdr(*ctxp))->bytelen;
old_len = l->len;
/* Check for additive overflow */ /* Check for additive overflow */
if (old_len + count * size < old_len) { if (old_len + count * size < old_len) {
...@@ -820,8 +756,7 @@ out: ...@@ -820,8 +756,7 @@ out:
} }
void *tal_dup_(const tal_t *ctx, const void *p, size_t size, void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
size_t n, size_t extra, bool add_length, size_t n, size_t extra, const char *label)
const char *label)
{ {
void *ret; void *ret;
size_t nbytes = size; size_t nbytes = size;
...@@ -850,7 +785,7 @@ void *tal_dup_(const tal_t *ctx, const void *p, size_t size, ...@@ -850,7 +785,7 @@ void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
return (void *)p; return (void *)p;
} }
ret = tal_alloc_arr_(ctx, size, n + extra, false, add_length, label); ret = tal_alloc_arr_(ctx, size, n + extra, false, label);
if (ret) if (ret)
memcpy(ret, p, nbytes); memcpy(ret, p, nbytes);
return ret; return ret;
...@@ -879,12 +814,11 @@ static void dump_node(unsigned int indent, const struct tal_hdr *t) ...@@ -879,12 +814,11 @@ static void dump_node(unsigned int indent, const struct tal_hdr *t)
for (i = 0; i < indent; i++) for (i = 0; i < indent; i++)
printf(" "); printf(" ");
printf("%p", t); printf("%p len=%zu", t, t->bytelen);
for (p = t->prop; p; p = p->next) { for (p = t->prop; p; p = p->next) {
struct children *c; struct children *c;
struct name *n; struct name *n;
struct notifier *no; struct notifier *no;
struct length *l;
if (is_literal(p)) { if (is_literal(p)) {
printf(" \"%s\"", (const char *)p); printf(" \"%s\"", (const char *)p);
break; break;
...@@ -904,10 +838,6 @@ static void dump_node(unsigned int indent, const struct tal_hdr *t) ...@@ -904,10 +838,6 @@ static void dump_node(unsigned int indent, const struct tal_hdr *t)
no = (struct notifier *)p; no = (struct notifier *)p;
printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn); printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
break; break;
case LENGTH:
l = (struct length *)p;
printf(" LENGTH(%p):len=%zu", p, l->len);
break;
default: default:
printf(" **UNKNOWN(%p):%i**", p, p->type); printf(" **UNKNOWN(%p):%i**", p, p->type);
} }
...@@ -955,7 +885,6 @@ static bool check_node(struct children *parent_child, ...@@ -955,7 +885,6 @@ static bool check_node(struct children *parent_child,
struct prop_hdr *p; struct prop_hdr *p;
struct name *name = NULL; struct name *name = NULL;
struct children *children = NULL; struct children *children = NULL;
struct length *length = NULL;
if (!in_bounds(t)) if (!in_bounds(t))
return check_err(t, errorstr, "invalid pointer"); return check_err(t, errorstr, "invalid pointer");
...@@ -981,12 +910,6 @@ static bool check_node(struct children *parent_child, ...@@ -981,12 +910,6 @@ static bool check_node(struct children *parent_child,
"has two child nodes"); "has two child nodes");
children = (struct children *)p; children = (struct children *)p;
break; break;
case LENGTH:
if (length)
return check_err(t, errorstr,
"has two lengths");
length = (struct length *)p;
break;
case NOTIFIER: case NOTIFIER:
break; break;
case NAME: case NAME:
......
...@@ -28,6 +28,8 @@ typedef void tal_t; ...@@ -28,6 +28,8 @@ typedef void tal_t;
* of the object is a string of the type, but if CCAN_TAL_DEBUG is * of the object is a string of the type, but if CCAN_TAL_DEBUG is
* defined it also contains the file and line which allocated it. * defined it also contains the file and line which allocated it.
* *
* tal_count() of the return will be 1.
*
* Example: * Example:
* int *p = tal(NULL, int); * int *p = tal(NULL, int);
* *p = 1; * *p = 1;
...@@ -71,8 +73,7 @@ void *tal_free(const tal_t *p); ...@@ -71,8 +73,7 @@ void *tal_free(const tal_t *p);
* @type: the type to allocate. * @type: the type to allocate.
* @count: the number to allocate. * @count: the number to allocate.
* *
* Note that an object allocated with tal_arr() has a length property; * tal_count() of the returned pointer will be @count.
* see tal_count().
* *
* Example: * Example:
* p = tal_arr(NULL, int, 2); * p = tal_arr(NULL, int, 2);
...@@ -88,8 +89,7 @@ void *tal_free(const tal_t *p); ...@@ -88,8 +89,7 @@ void *tal_free(const tal_t *p);
* @type: the type to allocate. * @type: the type to allocate.
* @count: the number to allocate. * @count: the number to allocate.
* *
* Note that an object allocated with tal_arrz() has a length property; * Equivalent to tal_arr() followed by memset() to zero.
* see tal_count().
* *
* Example: * Example:
* p = tal_arrz(NULL, int, 2); * p = tal_arrz(NULL, int, 2);
...@@ -99,12 +99,12 @@ void *tal_free(const tal_t *p); ...@@ -99,12 +99,12 @@ void *tal_free(const tal_t *p);
tal_arrz_label(ctx, type, count, TAL_LABEL(type, "[]")) tal_arrz_label(ctx, type, count, TAL_LABEL(type, "[]"))
/** /**
* tal_resize - enlarge or reduce a tal_arr[z]. * tal_resize - enlarge or reduce a tal object.
* @p: A pointer to the tal allocated array to resize. * @p: A pointer to the tal allocated array to resize.
* @count: the number to allocate. * @count: the number to allocate.
* *
* This returns true on success (and may move *@p), or false on failure. * This returns true on success (and may move *@p), or false on failure.
* If @p has a length property, it is updated on success. * On success, tal_count() of *@p will be @count.
* *
* Example: * Example:
* tal_resize(&p, 100); * tal_resize(&p, 100);
...@@ -113,13 +113,11 @@ void *tal_free(const tal_t *p); ...@@ -113,13 +113,11 @@ void *tal_free(const tal_t *p);
tal_resize_((void **)(p), sizeof**(p), (count), false) tal_resize_((void **)(p), sizeof**(p), (count), false)
/** /**
* tal_resizez - enlarge or reduce a tal_arr[z]; zero out extra. * tal_resizez - enlarge or reduce a tal object; zero out extra.
* @p: A pointer to the tal allocated array to resize. * @p: A pointer to the tal allocated array to resize.
* @count: the number to allocate. * @count: the number to allocate.
* *
* This returns true on success (and may move *@p), or false on failure. * This returns true on success (and may move *@p), or false on failure.
* If @p has a length property, it is updated on success.
* On expand, new elements are memset to 0 bytes.
* *
* Example: * Example:
* tal_resizez(&p, 200); * tal_resizez(&p, 200);
...@@ -302,20 +300,20 @@ enum tal_notify_type { ...@@ -302,20 +300,20 @@ enum tal_notify_type {
const char *tal_name(const tal_t *ptr); const char *tal_name(const tal_t *ptr);
/** /**
* tal_count - get the count of objects in a tal_arr. * tal_count - get the count of objects in a tal object.
* @ptr: The tal allocated object array (or NULL) * @ptr: The tal allocated object (or NULL)
* *
* Returns 0 if @ptr has no length property or is NULL, but be aware * Returns 0 if @ptr is NULL. Note that if the allocation was done as a
* that that is also a valid size! * different type to @ptr, the result may not match the @count argument
* (or implied 1) of that allocation!
*/ */
#define tal_count(p) (tal_bytelen(p) / sizeof(*p)) #define tal_count(p) (tal_bytelen(p) / sizeof(*p))
/** /**
* tal_bytelen - get the count of bytes in a tal_arr. * tal_bytelen - get the count of bytes in a tal object.
* @ptr: The tal allocated object array (or NULL) * @ptr: The tal allocated object (or NULL)
* *
* Returns 0 if @ptr has no length property or NULL, but be aware that that is * Returns 0 if @ptr is NULL.
* also a valid size!
*/ */
size_t tal_bytelen(const tal_t *ptr); size_t tal_bytelen(const tal_t *ptr);
...@@ -368,21 +366,21 @@ tal_t *tal_parent(const tal_t *ctx); ...@@ -368,21 +366,21 @@ tal_t *tal_parent(const tal_t *ctx);
/* Lower-level interfaces, where you want to supply your own label string. */ /* Lower-level interfaces, where you want to supply your own label string. */
#define tal_label(ctx, type, label) \ #define tal_label(ctx, type, label) \
((type *)tal_alloc_((ctx), sizeof(type), false, false, label)) ((type *)tal_alloc_((ctx), sizeof(type), false, label))
#define talz_label(ctx, type, label) \ #define talz_label(ctx, type, label) \
((type *)tal_alloc_((ctx), sizeof(type), true, false, label)) ((type *)tal_alloc_((ctx), sizeof(type), true, label))
#define tal_arr_label(ctx, type, count, label) \ #define tal_arr_label(ctx, type, count, label) \
((type *)tal_alloc_arr_((ctx), sizeof(type), (count), false, true, label)) ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), false, label))
#define tal_arrz_label(ctx, type, count, label) \ #define tal_arrz_label(ctx, type, count, label) \
((type *)tal_alloc_arr_((ctx), sizeof(type), (count), true, true, label)) ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), true, label))
#define tal_dup_label(ctx, type, p, label) \ #define tal_dup_label(ctx, type, p, label) \
((type *)tal_dup_((ctx), tal_typechk_(p, type *), \ ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \
sizeof(type), 1, 0, \ sizeof(type), 1, 0, \
false, label)) label))
#define tal_dup_arr_label(ctx, type, p, n, extra, label) \ #define tal_dup_arr_label(ctx, type, p, n, extra, label) \
((type *)tal_dup_((ctx), tal_typechk_(p, type *), \ ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \
sizeof(type), (n), (extra), \ sizeof(type), (n), (extra), \
true, label)) label))
/** /**
* tal_set_backend - set the allocation or error functions to use * tal_set_backend - set the allocation or error functions to use
...@@ -496,14 +494,12 @@ bool tal_set_name_(tal_t *ctx, const char *name, bool literal); ...@@ -496,14 +494,12 @@ bool tal_set_name_(tal_t *ctx, const char *name, bool literal);
#define tal_typechk_(ptr, ptype) (ptr) #define tal_typechk_(ptr, ptype) (ptr)
#endif #endif
void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label);
bool add_length, const char *label);
void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear, void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
bool add_length, const char *label); const char *label);
void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size, void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size,
size_t n, size_t extra, bool add_length, size_t n, size_t extra, const char *label);
const char *label);
tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t); tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t);
......
...@@ -54,7 +54,7 @@ int main(void) ...@@ -54,7 +54,7 @@ int main(void)
p1 = tal(NULL, char); p1 = tal(NULL, char);
ok1(p1); ok1(p1);
ok1(tal_count(p1) == 0); ok1(tal_count(p1) == 1);
p2 = tal_arr(p1, char, 1); p2 = tal_arr(p1, char, 1);
ok1(p2); ok1(p2);
......
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