X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Ftal%2Ftal.c;h=ffe2d71334dce4473d57243bca0827aaa5fee565;hb=7ca46909f86e8bbad4e4bb19cac1a75d4ebf3df6;hp=e575130568ca26913ad97ed00fa2feb6f4cf7740;hpb=0e34459a02e2615f50bac2767c7dce6632470946;p=ccan diff --git a/ccan/tal/tal.c b/ccan/tal/tal.c index e5751305..ffe2d713 100644 --- a/ccan/tal/tal.c +++ b/ccan/tal/tal.c @@ -9,6 +9,7 @@ #include #include #include +#include //#define TAL_DEBUG 1 @@ -294,6 +295,7 @@ static struct children *add_child_property(struct tal_hdr *parent, init_group_property(&prop->group, prop, child); list_head_init(&prop->group.list); + update_bounds(&prop->group); } return prop; } @@ -316,8 +318,7 @@ static bool add_child(struct tal_hdr *parent, struct tal_hdr *child) children = add_child_property(parent, child); if (!children) return false; - children->group.list.n.next = children->group.list.n.prev - = &children->group.list.n; + list_head_init(&children->group.list); /* Child links to itself. */ child->next = child; @@ -417,7 +418,7 @@ static void del_tree(struct tal_hdr *t) freefn(t); } -void *tal_alloc_(const tal_t *ctx, size_t size, bool clear) +void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label) { struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx)); @@ -426,7 +427,7 @@ void *tal_alloc_(const tal_t *ctx, size_t size, bool clear) return NULL; if (clear) memset(from_tal_hdr(child), 0, size); - child->prop = NULL; + child->prop = (void *)label; if (!add_child(parent, child)) { freefn(child); return NULL; @@ -455,6 +456,7 @@ static struct tal_hdr *remove_node(struct tal_hdr *t) /* Are we the only one? */ if (prev == t) { + struct prop_hdr *next = (*prop)->next; struct children *c = group->parent_child; /* Is this the group embedded in the child property? */ if (group == &c->group) { @@ -462,9 +464,9 @@ static struct tal_hdr *remove_node(struct tal_hdr *t) } else { /* Empty group, so free it. */ list_del_from(&c->group.list, &group->list.n); - *prop = group->hdr.next; freefn(group); } + *prop = next; return c->parent; } else { /* Move property to next node. */ @@ -481,6 +483,7 @@ static struct tal_hdr *remove_node(struct tal_hdr *t) void tal_free(const tal_t *ctx) { struct tal_hdr *t; + int saved_errno = errno; if (!ctx) return; @@ -488,6 +491,7 @@ void tal_free(const tal_t *ctx) t = debug_tal(to_tal_hdr(ctx)); remove_node(t); del_tree(t); + errno = saved_errno; } void *tal_steal_(const tal_t *new_parent, const tal_t *ctx) @@ -638,7 +642,12 @@ tal_t *tal_next(const tal_t *root, const tal_t *prev) tal_t *tal_parent(const tal_t *ctx) { struct group *group; - struct tal_hdr *t = debug_tal(to_tal_hdr(ctx)); + struct tal_hdr *t; + + if (!ctx) + return NULL; + + t = debug_tal(to_tal_hdr(ctx)); while (!(group = find_property(t, GROUP))) t = t->next; @@ -648,22 +657,23 @@ tal_t *tal_parent(const tal_t *ctx) return from_tal_hdr(group->parent_child->parent); } -void *tal_realloc_(tal_t *ctx, size_t size) +bool tal_resize_(tal_t **ctxp, size_t size) { struct tal_hdr *old_t, *t, **prev; struct group *group; struct children *child; - old_t = debug_tal(to_tal_hdr(ctx)); + old_t = debug_tal(to_tal_hdr(*ctxp)); t = resizefn(old_t, size + sizeof(struct tal_hdr)); if (!t) { call_error("Reallocation failure"); - tal_free(old_t); - return NULL; + return false; } + + /* If it didn't move, we're done! */ if (t == old_t) - return ctx; + return true; update_bounds(t); /* Fix up linked list pointer. */ @@ -683,8 +693,8 @@ void *tal_realloc_(tal_t *ctx, size_t size) assert(child->parent == old_t); child->parent = t; } - - return from_tal_hdr(debug_tal(t)); + *ctxp = from_tal_hdr(debug_tal(t)); + return true; } char *tal_strdup(const tal_t *ctx, const char *p) @@ -706,7 +716,12 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n) void *tal_memdup(const tal_t *ctx, const void *p, size_t n) { - void *ret = tal_arr(ctx, char, n); + void *ret; + + if (ctx == TAL_TAKE) + return (void *)p; + + ret = tal_arr(ctx, char, n); if (ret) memcpy(ret, p, n); return ret; @@ -727,9 +742,14 @@ char *tal_asprintf(const tal_t *ctx, const char *fmt, ...) char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap) { size_t max = strlen(fmt) * 2; - char *buf = tal_arr(ctx, char, max); + char *buf; int ret; + if (ctx == TAL_TAKE) + buf = tal_arr(tal_parent(fmt), char, max); + else + buf = tal_arr(ctx, char, max); + while (buf) { va_list ap2; @@ -739,8 +759,13 @@ char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap) if (ret < max) break; - buf = tal_resize(buf, max *= 2); + if (!tal_resize(&buf, max *= 2)) { + tal_free(buf); + buf = NULL; + } } + if (ctx == TAL_TAKE) + tal_free(fmt); return buf; } @@ -875,7 +900,8 @@ static bool check_node(struct group *group, name = (struct name *)p; break; } - if (p != &null_parent.c.hdr && !in_bounds(p)) + if (p != &null_parent.c.hdr && p != &null_parent.c.group.hdr + && !in_bounds(p)) return check_err(t, errorstr, "has bad property pointer");