From 88aa31df07e02a862212641b4dba692ed1599d5b Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 15 Mar 2021 20:57:48 +0800 Subject: [PATCH] add uniq_skiplist_pair struct and init function --- HISTORY | 3 ++- src/uniq_skiplist.c | 2 +- src/uniq_skiplist.h | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/HISTORY b/HISTORY index 83afa29..b870c42 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.49 2021-03-10 +Version 1.49 2021-03-15 * add macros: FC_ABS and FC_NEGATIVE + * uniq_skiplist.c: add uniq_skiplist_pair struct and init function Version 1.48 2021-02-01 * fast_buffer.[hc]: add function fast_buffer_append_binary diff --git a/src/uniq_skiplist.c b/src/uniq_skiplist.c index 706a76f..0611bad 100644 --- a/src/uniq_skiplist.c +++ b/src/uniq_skiplist.c @@ -117,7 +117,7 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory, if ((result=fast_mblock_init_ex1(&factory->skiplist_allocator, "skiplist", sizeof(UniqSkiplist), alloc_skiplist_once > 0 ? alloc_skiplist_once : - 16 * 1024, alloc_elements_limit, NULL, NULL, false)) != 0) + 4 * 1024, alloc_elements_limit, NULL, NULL, false)) != 0) { return result; } diff --git a/src/uniq_skiplist.h b/src/uniq_skiplist.h index c8a92c3..1c91e9b 100644 --- a/src/uniq_skiplist.h +++ b/src/uniq_skiplist.h @@ -54,6 +54,11 @@ typedef struct uniq_skiplist UniqSkiplistNode *top; //the top node } UniqSkiplist; +typedef struct uniq_skiplist_pair { + UniqSkiplistFactory factory; + struct uniq_skiplist *skiplist; +} UniqSkiplistPair; + typedef struct uniq_skiplist_iterator { volatile UniqSkiplistNode *current; volatile UniqSkiplistNode *tail; @@ -70,13 +75,19 @@ extern "C" { delay_free_seconds) \ uniq_skiplist_init_ex2(factory, max_level_count, compare_func, \ free_func, alloc_skiplist_once, min_alloc_elements_once, \ - delay_free_seconds, false) \ + delay_free_seconds, false) #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) +#define uniq_skiplist_init_pair(pair, init_level_count, max_level_count, \ + compare_func, free_func, min_alloc_elements_once, delay_free_seconds) \ + uniq_skiplist_init_pair_ex(pair, init_level_count, max_level_count, \ + compare_func, free_func, min_alloc_elements_once, \ + delay_free_seconds, false) + #define uniq_skiplist_delete(sl, data) \ uniq_skiplist_delete_ex(sl, data, true) @@ -100,6 +111,33 @@ UniqSkiplist *uniq_skiplist_new(UniqSkiplistFactory *factory, void uniq_skiplist_free(UniqSkiplist *sl); +static inline int uniq_skiplist_init_pair_ex(UniqSkiplistPair *pair, + const int init_level_count, const int max_level_count, + skiplist_compare_func compare_func, uniq_skiplist_free_func + free_func, const int min_alloc_elements_once, + const int delay_free_seconds, const bool bidirection) +{ + const int alloc_skiplist_once = 1; + int result; + + 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)) != 0) + { + return result; + } + + if ((pair->skiplist=uniq_skiplist_new(&pair->factory, + init_level_count)) == NULL) + { + uniq_skiplist_destroy(&pair->factory); + return ENOMEM; + } + + return 0; +} + int uniq_skiplist_insert(UniqSkiplist *sl, void *data); int uniq_skiplist_delete_ex(UniqSkiplist *sl, void *data, const bool need_free);