diff options
author | David Robillard <d@drobilla.net> | 2022-08-18 16:43:52 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-08-18 16:43:52 -0400 |
commit | 3b62fc2b6a6beedde5d717238209f769cd322509 (patch) | |
tree | 8fa13c781122bb282cd2700a3e1b3371e4b4db7d /src | |
parent | f65912600fc301cbdbb613f1df0dc29820f1da35 (diff) | |
download | zix-3b62fc2b6a6beedde5d717238209f769cd322509.tar.gz zix-3b62fc2b6a6beedde5d717238209f769cd322509.tar.bz2 zix-3b62fc2b6a6beedde5d717238209f769cd322509.zip |
Handle trees with no destroy callback more gracefully
Diffstat (limited to 'src')
-rw-r--r-- | src/tree.c | 21 |
1 files changed, 11 insertions, 10 deletions
@@ -53,6 +53,13 @@ struct ZixTreeNodeImpl { # define DEBUG_PRINTF(fmt, ...) #endif +static void +zix_tree_noop_destroy(void* const ptr, const void* const user_data) +{ + (void)ptr; + (void)user_data; +} + ZixTree* zix_tree_new(ZixAllocator* const allocator, bool allow_duplicates, @@ -66,7 +73,7 @@ zix_tree_new(ZixAllocator* const allocator, if (t) { t->allocator = allocator; t->root = NULL; - t->destroy = destroy; + t->destroy = destroy ? destroy : zix_tree_noop_destroy; t->destroy_user_data = destroy_user_data; t->cmp = cmp; t->cmp_data = cmp_data; @@ -83,9 +90,7 @@ zix_tree_free_rec(ZixTree* t, ZixTreeNode* n) if (n) { zix_tree_free_rec(t, n->left); zix_tree_free_rec(t, n->right); - if (t->destroy) { - t->destroy(n->data, t->destroy_user_data); - } + t->destroy(n->data, t->destroy_user_data); zix_free(t->allocator, n); } @@ -447,9 +452,7 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti) if ((n == t->root) && !n->left && !n->right) { t->root = NULL; - if (t->destroy) { - t->destroy(n->data, t->destroy_user_data); - } + t->destroy(n->data, t->destroy_user_data); zix_free(t->allocator, n); --t->size; assert(t->size == 0); @@ -579,9 +582,7 @@ zix_tree_remove(ZixTree* t, ZixTreeIter* ti) DUMP(t); - if (t->destroy) { - t->destroy(n->data, t->destroy_user_data); - } + t->destroy(n->data, t->destroy_user_data); zix_free(t->allocator, n); --t->size; |