uniq_skiplist support arg for free callback

pull/47/head
YuQing 2023-08-05 20:46:35 +08:00
parent fafbbb557e
commit 4480669e03
5 changed files with 28 additions and 14 deletions

View File

@ -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

View File

@ -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; i<input->count; i++) {
input->elts[i] = i + 1;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}