From 4480669e03219a5076b6722b63cc64d00f251422 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sat, 5 Aug 2023 20:46:35 +0800 Subject: [PATCH] uniq_skiplist support arg for free callback --- HISTORY | 3 ++- src/tests/test_sorted_array.c | 6 ++++++ src/tests/test_uniq_skiplist.c | 8 +++++--- src/uniq_skiplist.c | 10 ++++++---- src/uniq_skiplist.h | 15 +++++++++------ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/HISTORY b/HISTORY index 0750d4a..e05f184 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.69 2023-08-02 +Version 1.69 2023-08-05 * bugfixed: array_allocator_alloc MUST init the array + * uniq_skiplist support arg for free callback Version 1.68 2023-07-05 * sorted_queue.[hc]: pop_compare_func support argument diff --git a/src/tests/test_sorted_array.c b/src/tests/test_sorted_array.c index 931fb0b..287453a 100644 --- a/src/tests/test_sorted_array.c +++ b/src/tests/test_sorted_array.c @@ -66,6 +66,12 @@ static int test_i64() return ENOMEM; } + if (!silence) { + printf("input alloc count: %d, output alloc count: %d\n", + input->alloc, output->alloc); + } + + input->count = ELEMENT_COUNT; for (i=0; icount; i++) { input->elts[i] = i + 1; diff --git a/src/tests/test_uniq_skiplist.c b/src/tests/test_uniq_skiplist.c index 1931c12..dda8e5a 100644 --- a/src/tests/test_uniq_skiplist.c +++ b/src/tests/test_uniq_skiplist.c @@ -36,7 +36,8 @@ static UniqSkiplist *sl = NULL; static UniqSkiplistIterator iterator; static int instance_count = 0; -static void free_test_func(void *ptr, const int delay_seconds) +static void free_test_func(UniqSkiplist *sl, + void *ptr, const int delay_seconds) { instance_count--; } @@ -286,8 +287,9 @@ int main(int argc, char *argv[]) srand(time(NULL)); fast_mblock_manager_init(); - result = uniq_skiplist_init_ex2(&factory, LEVEL_COUNT, compare_func, - free_test_func, 0, MIN_ALLOC_ONCE, 0, true, allocator_use_lock); + result = uniq_skiplist_init_ex2(&factory, LEVEL_COUNT, + compare_func, free_test_func, 0, MIN_ALLOC_ONCE, + 0, true, allocator_use_lock, NULL); if (result != 0) { return result; } diff --git a/src/uniq_skiplist.c b/src/uniq_skiplist.c index da0257d..2e3b856 100644 --- a/src/uniq_skiplist.c +++ b/src/uniq_skiplist.c @@ -54,7 +54,7 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory, const int max_level_count, skiplist_compare_func compare_func, uniq_skiplist_free_func free_func, const int alloc_skiplist_once, const int min_alloc_elements_once, const int delay_free_seconds, - const bool bidirection, const bool allocator_use_lock) + const bool bidirection, const bool allocator_use_lock, void *arg) { const int64_t alloc_elements_limit = 0; char name[64]; @@ -136,6 +136,7 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory, factory->compare_func = compare_func; factory->free_func = free_func; factory->delay_free_seconds = delay_free_seconds; + factory->arg = arg; srand(time(NULL)); return 0; @@ -274,7 +275,7 @@ static void do_clear(UniqSkiplist *sl) deleted = node; node = node->links[0]; - sl->factory->free_func(deleted->data, 0); + sl->factory->free_func(sl, deleted->data, 0); fast_mblock_free_object(sl->factory->node_allocators + deleted->level_index, (void *)deleted); } @@ -505,7 +506,7 @@ void uniq_skiplist_delete_node_ex(UniqSkiplist *sl, } if (need_free && sl->factory->free_func != NULL) { - sl->factory->free_func(deleted->data, + sl->factory->free_func(sl, deleted->data, sl->factory->delay_free_seconds); } @@ -548,7 +549,8 @@ int uniq_skiplist_replace_ex(UniqSkiplist *sl, void *data, deleted_data = current->data; current->data = data; - sl->factory->free_func(deleted_data, sl->factory->delay_free_seconds); + sl->factory->free_func(sl, deleted_data, + sl->factory->delay_free_seconds); } else { current->data = data; } diff --git a/src/uniq_skiplist.h b/src/uniq_skiplist.h index 282d9cf..af4d0b5 100644 --- a/src/uniq_skiplist.h +++ b/src/uniq_skiplist.h @@ -25,7 +25,9 @@ #include "skiplist_common.h" #include "fast_mblock.h" -typedef void (*uniq_skiplist_free_func)(void *ptr, const int delay_seconds); +struct uniq_skiplist; +typedef void (*uniq_skiplist_free_func)(struct uniq_skiplist *skiplist, + void *ptr, const int delay_seconds); typedef struct uniq_skiplist_node { @@ -44,6 +46,7 @@ typedef struct uniq_skiplist_factory UniqSkiplistNode *tail; //the tail node for interator struct fast_mblock_man skiplist_allocator; struct fast_mblock_man *node_allocators; + void *arg; } UniqSkiplistFactory; typedef struct uniq_skiplist @@ -72,15 +75,15 @@ extern "C" { #define uniq_skiplist_init_ex(factory, max_level_count, compare_func, \ free_func, alloc_skiplist_once, min_alloc_elements_once, \ - delay_free_seconds) \ + delay_free_seconds, arg) \ uniq_skiplist_init_ex2(factory, max_level_count, compare_func, \ free_func, alloc_skiplist_once, min_alloc_elements_once, \ - delay_free_seconds, false, false) + delay_free_seconds, false, false, arg) #define uniq_skiplist_init(factory, max_level_count, compare_func, free_func) \ uniq_skiplist_init_ex(factory, max_level_count, \ compare_func, free_func, 64 * 1024, \ - SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE, 0) + SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE, 0, NULL) #define uniq_skiplist_init_pair(pair, init_level_count, max_level_count, \ compare_func, free_func, min_alloc_elements_once, delay_free_seconds) \ @@ -102,7 +105,7 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory, const int max_level_count, skiplist_compare_func compare_func, uniq_skiplist_free_func free_func, const int alloc_skiplist_once, const int min_alloc_elements_once, const int delay_free_seconds, - const bool bidirection, const bool allocator_use_lock); + const bool bidirection, const bool allocator_use_lock, void *arg); void uniq_skiplist_destroy(UniqSkiplistFactory *factory); @@ -124,7 +127,7 @@ static inline int uniq_skiplist_init_pair_ex(UniqSkiplistPair *pair, if ((result=uniq_skiplist_init_ex2(&pair->factory, max_level_count, compare_func, free_func, alloc_skiplist_once, min_alloc_elements_once, delay_free_seconds, - bidirection, allocator_use_lock)) != 0) + bidirection, allocator_use_lock, NULL)) != 0) { return result; }