Commit 8cf5b620 authored by Rusty Russell's avatar Rusty Russell

tal: don't automatically register cleanup function.

It may interfere with other at_exit() calls, so let them call it manually.
Also, use memset to zero, which really does make valgrind notice any leaks.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent d21d629a
......@@ -76,7 +76,6 @@ static void *(*allocfn)(size_t size) = malloc;
static void *(*resizefn)(void *, size_t size) = realloc;
static void (*freefn)(void *) = free;
static void (*errorfn)(const char *msg) = (void *)abort;
static bool initialized = false;
/* Count on non-destrutor notifiers; often stays zero. */
static size_t notifiers = 0;
......@@ -101,23 +100,19 @@ static struct children *ignore_destroying_bit(struct children *parent_child)
}
/* This means valgrind can see leaks. */
static void tal_cleanup(void)
void tal_cleanup(void)
{
struct tal_hdr *i;
while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
list_del(&i->list);
memset(i, 0, sizeof(*i));
}
/* Cleanup any taken pointers. */
take_cleanup();
}
/* For allocation failures inside ccan/take */
static void take_alloc_failed(const void *p)
{
tal_free(p);
}
/* We carefully start all real properties with a zero byte. */
static bool is_literal(const struct prop_hdr *prop)
{
......@@ -344,11 +339,6 @@ static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
struct children *children = find_property(parent, CHILDREN);
if (!children) {
if (unlikely(!initialized)) {
atexit(tal_cleanup);
take_allocfail(take_alloc_failed);
initialized = true;
}
children = add_child_property(parent, child);
if (!children)
return false;
......
......@@ -327,6 +327,17 @@ void tal_set_backend(void *(*alloc_fn)(size_t size),
tal_expand_((void **)(a1p), (a2), sizeof**(a1p), \
(num2) + 0*sizeof(*(a1p) == (a2)))
/**
* tal_cleanup - remove pointers from NULL node
*
* Internally, tal keeps a list of nodes allocated from @ctx NULL; this
* prevents valgrind from noticing memory leaks. This re-initializes
* that list to empty.
*
* It also calls take_cleanup() for you.
*/
void tal_cleanup(void);
/**
* tal_check - set the allocation or error functions to use
......
......@@ -148,5 +148,6 @@ int main(void)
ok1(err_count == when_to_fail - 1);
tal_free(p);
tal_cleanup();
return exit_status();
}
......@@ -42,5 +42,6 @@ int main(void)
ok1(tal_first(parent) == NULL);
tal_free(parent);
tal_cleanup();
return exit_status();
}
......@@ -82,5 +82,7 @@ int main(void)
tal_free(p2);
tal_free(p1);
}
tal_cleanup();
return exit_status();
}
......@@ -63,5 +63,6 @@ int main(void)
tal_free(parent);
ok1(destroy_count == 4);
tal_cleanup();
return exit_status();
}
......@@ -28,5 +28,6 @@ int main(void)
tal_free(a);
tal_cleanup();
return exit_status();
}
......@@ -21,5 +21,6 @@ int main(void)
tal_free(p);
ok1(errno == EINVAL);
tal_cleanup();
return exit_status();
}
......@@ -42,5 +42,7 @@ int main(void)
/* We can expect some residue from having any child, but limited! */
ok1(num_allocated <= allocated_after_first);
tal_free(p);
tal_cleanup();
return exit_status();
}
......@@ -29,5 +29,6 @@ int main(void)
ok1(*p[i] == '1');
tal_free(p[i]);
}
tal_cleanup();
return exit_status();
}
......@@ -30,5 +30,6 @@ int main(void)
ok1(strcmp(tal_name(p), __FILE__ ":29:int[]") == 0);
tal_free(p);
tal_cleanup();
return exit_status();
}
......@@ -26,5 +26,6 @@ int main(void)
tal_free(p);
tal_cleanup();
return exit_status();
}
......@@ -29,5 +29,6 @@ int main(void)
ok1(strcmp(tal_name(p), "int[]") == 0);
tal_free(p);
tal_cleanup();
return exit_status();
}
......@@ -126,5 +126,6 @@ int main(void)
tal_del_notifier(new_ctx, resize_notifier);
tal_free(new_ctx);
tal_cleanup();
return exit_status();
}
......@@ -94,5 +94,6 @@ int main(void)
ok1(error_count == 3);
tal_free(origpi);
tal_cleanup();
return exit_status();
}
......@@ -36,5 +36,6 @@ int main(void)
ok1(tal_parent(p[4]) == p[0]);
tal_free(p[0]);
tal_cleanup();
return exit_status();
}
......@@ -52,5 +52,6 @@ int main(void)
ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL);
ok1(!taken_any());
tal_cleanup();
return exit_status();
}
......@@ -75,5 +75,6 @@ int main(void)
/* Finally, free the parent. */
tal_free(p);
tal_cleanup();
return exit_status();
}
......@@ -56,5 +56,6 @@ int main(void)
}
tal_free(parent);
tal_cleanup();
return exit_status();
}
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