setup: make all htables use tal.

This makes them easier to clean up.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-01-12 11:43:14 +10:30
parent 0d93841cc7
commit f07e37018d
2 changed files with 21 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
#include "config.h"
#include <assert.h>
#include <ccan/ccan/err/err.h>
#include <ccan/ccan/htable/htable.h>
#include <common/autodata.h>
#include <common/setup.h>
#include <common/utils.h>
@@ -24,6 +25,15 @@ static struct wally_operations wally_tal_ops = {
.free_fn = wally_free,
};
static void *htable_tal(struct htable *ht, size_t len)
{
return tal_arrz(ht, u8, len);
}
static void htable_tal_free(struct htable *ht, void *p)
{
tal_free(p);
}
void common_setup(const char *argv0)
{
@@ -47,6 +57,9 @@ void common_setup(const char *argv0)
errx(1, "Error setting libwally operations: %i", wally_ret);
secp256k1_ctx = wally_get_secp_context();
/* Make htable* use tal for the tables themselves. */
htable_set_allocator(htable_tal, htable_tal_free);
setup_tmpctx();
}

View File

@@ -172,9 +172,14 @@ int main(int argc, char *argv[])
assert(htable_count(f->cur) == 0);
assert(htable_count(f->old) == 0);
/* They should have no children, and f should only have 2. */
assert(!tal_first(f->cur));
assert(!tal_first(f->old));
/* They should have no children (except htable contents for one!), and
* f should only have 2. */
if (tal_first(f->cur) == NULL)
assert(tal_first(f->old) == f->old->table);
else {
assert(tal_first(f->cur) == f->cur->table);
assert(tal_first(f->old) == NULL);
}
assert((tal_first(f) == f->cur
&& tal_next(f->cur) == f->old