1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

data-struct/radix-tree: pass the value dtr into create.

Rather than having to pass it into every method that removes items.
This commit is contained in:
Joe Thornber 2018-05-29 11:03:10 +01:00
parent 28c8e95d19
commit 033df741e2
3 changed files with 12 additions and 9 deletions

View File

@ -74,17 +74,21 @@ struct node256 {
struct radix_tree { struct radix_tree {
unsigned nr_entries; unsigned nr_entries;
struct value root; struct value root;
radix_value_dtr dtr;
void *dtr_context;
}; };
//---------------------------------------------------------------- //----------------------------------------------------------------
struct radix_tree *radix_tree_create(void) struct radix_tree *radix_tree_create(radix_value_dtr dtr, void *dtr_context)
{ {
struct radix_tree *rt = malloc(sizeof(*rt)); struct radix_tree *rt = malloc(sizeof(*rt));
if (rt) { if (rt) {
rt->nr_entries = 0; rt->nr_entries = 0;
rt->root.type = UNSET; rt->root.type = UNSET;
rt->dtr = dtr;
rt->dtr_context = dtr_context;
} }
return rt; return rt;
@ -153,9 +157,9 @@ static void _free_node(struct value v, radix_value_dtr dtr, void *context)
} }
} }
void radix_tree_destroy(struct radix_tree *rt, radix_value_dtr dtr, void *context) void radix_tree_destroy(struct radix_tree *rt)
{ {
_free_node(rt->root, dtr, context); _free_node(rt->root, rt->dtr, rt->dtr_context);
free(rt); free(rt);
} }

View File

@ -25,12 +25,11 @@ union radix_value {
uint64_t n; uint64_t n;
}; };
struct radix_tree *radix_tree_create(void);
typedef void (*radix_value_dtr)(void *context, union radix_value v); typedef void (*radix_value_dtr)(void *context, union radix_value v);
// dtr may be NULL // dtr will be called on any deleted entries. dtr may be NULL.
void radix_tree_destroy(struct radix_tree *rt, radix_value_dtr dtr, void *context); struct radix_tree *radix_tree_create(radix_value_dtr dtr, void *dtr_context);
void radix_tree_destroy(struct radix_tree *rt);
unsigned radix_tree_size(struct radix_tree *rt); unsigned radix_tree_size(struct radix_tree *rt);
bool radix_tree_insert(struct radix_tree *rt, uint8_t *kb, uint8_t *ke, union radix_value v); bool radix_tree_insert(struct radix_tree *rt, uint8_t *kb, uint8_t *ke, union radix_value v);

View File

@ -21,14 +21,14 @@
static void *rt_init(void) static void *rt_init(void)
{ {
struct radix_tree *rt = radix_tree_create(); struct radix_tree *rt = radix_tree_create(NULL, NULL);
T_ASSERT(rt); T_ASSERT(rt);
return rt; return rt;
} }
static void rt_exit(void *fixture) static void rt_exit(void *fixture)
{ {
radix_tree_destroy(fixture, NULL, NULL); radix_tree_destroy(fixture);
} }
static void test_create_destroy(void *fixture) static void test_create_destroy(void *fixture)