diff --git a/HISTORY b/HISTORY index 1db0fa0..b64d2d8 100644 --- a/HISTORY +++ b/HISTORY @@ -1,8 +1,9 @@ -Version 1.44 2020-02-02 +Version 1.44 2020-02-03 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex + * fast_mblock.[hc]: add init_args for init_func Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/common_blocked_queue.c b/src/common_blocked_queue.c index 551cdae..79dcc2f 100644 --- a/src/common_blocked_queue.c +++ b/src/common_blocked_queue.c @@ -33,7 +33,7 @@ int common_blocked_queue_init_ex(struct common_blocked_queue *queue, if ((result=fast_mblock_init_ex(&queue->mblock, sizeof(struct common_blocked_node), - alloc_elements_once, NULL, false)) != 0) + alloc_elements_once, NULL, NULL, false)) != 0) { return result; } diff --git a/src/fast_allocator.c b/src/fast_allocator.c index d08809f..59b112b 100644 --- a/src/fast_allocator.c +++ b/src/fast_allocator.c @@ -142,7 +142,7 @@ static int region_init(struct fast_allocator_context *acontext, element_size+=region->step,allocator++) { result = fast_mblock_init_ex2(&allocator->mblock, NULL, element_size, - region->alloc_elements_once, NULL, acontext->need_lock, + region->alloc_elements_once, NULL, NULL, acontext->need_lock, fast_allocator_malloc_trunk_check, fast_allocator_malloc_trunk_notify_func, acontext); if (result != 0) diff --git a/src/fast_mblock.c b/src/fast_mblock.c index 11946d0..3951ad5 100644 --- a/src/fast_mblock.c +++ b/src/fast_mblock.c @@ -311,18 +311,21 @@ int fast_mblock_manager_stat_print_ex(const bool hide_empty, const int order_by) int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, const int alloc_elements_once, - fast_mblock_alloc_init_func init_func, const bool need_lock) + fast_mblock_alloc_init_func init_func, void *init_args, + const bool need_lock) { return fast_mblock_init_ex2(mblock, NULL, element_size, - alloc_elements_once, init_func, need_lock, NULL, NULL, NULL); + alloc_elements_once, init_func, init_args, + need_lock, NULL, NULL, NULL); } int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name, const int element_size, const int alloc_elements_once, - fast_mblock_alloc_init_func init_func, const bool need_lock, - fast_mblock_malloc_trunk_check_func malloc_trunk_check, - fast_mblock_malloc_trunk_notify_func malloc_trunk_notify, - void *malloc_trunk_args) + fast_mblock_alloc_init_func init_func, + void *init_args, const bool need_lock, + fast_mblock_malloc_trunk_check_func malloc_trunk_check, + fast_mblock_malloc_trunk_notify_func malloc_trunk_notify, + void *malloc_trunk_args) { int result; int block_size; @@ -355,6 +358,7 @@ int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name, } mblock->alloc_init_func = init_func; + mblock->init_args = init_args; INIT_HEAD(&mblock->trunks.head); mblock->info.trunk_total_count = 0; mblock->info.trunk_used_count = 0; @@ -423,10 +427,10 @@ static int fast_mblock_prealloc(struct fast_mblock_man *mblock) for (p=pTrunkStart; p<=pLast; p += block_size) { pNode = (struct fast_mblock_node *)p; - if (mblock->alloc_init_func != NULL) { - if ((result=mblock->alloc_init_func(pNode->data)) != 0) + if ((result=mblock->alloc_init_func(pNode->data, + mblock->init_args)) != 0) { free(pNew); return result; diff --git a/src/fast_mblock.h b/src/fast_mblock.h index b6c87e8..3944a84 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -45,7 +45,7 @@ struct fast_mblock_chain { struct fast_mblock_node *tail; }; -typedef int (*fast_mblock_alloc_init_func)(void *element); +typedef int (*fast_mblock_alloc_init_func)(void *element, void *args); typedef int (*fast_mblock_malloc_trunk_check_func)( const int alloc_bytes, void *args); @@ -92,6 +92,7 @@ struct fast_mblock_man pthread_mutex_t lock; //the lock for read / write free node chain struct fast_mblock_man *prev; //for stat manager struct fast_mblock_man *next; //for stat manager + void *init_args; //args for alloc_init_func }; #define GET_BLOCK_SIZE(info) \ @@ -108,7 +109,8 @@ extern "C" { #endif #define fast_mblock_init(mblock, element_size, alloc_elements_once) \ - fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL, true) + fast_mblock_init_ex(mblock, element_size, alloc_elements_once, \ + NULL, NULL, true) /** mblock init @@ -117,12 +119,14 @@ parameters: element_size: element size, such as sizeof(struct xxx) alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once init_func: the init function + init_args: the args for init_func need_lock: if need lock return error no, 0 for success, != 0 fail */ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size, const int alloc_elements_once, - fast_mblock_alloc_init_func init_func, const bool need_lock); + fast_mblock_alloc_init_func init_func, void *init_args, + const bool need_lock); /** mblock init @@ -132,6 +136,7 @@ parameters: element_size: element size, such as sizeof(struct xxx) alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once init_func: the init function + init_args: the args for init_func need_lock: if need lock malloc_trunk_check: the malloc trunk check function pointor malloc_trunk_notify: the malloc trunk notify function pointor @@ -140,7 +145,8 @@ return error no, 0 for success, != 0 fail */ int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name, const int element_size, const int alloc_elements_once, - fast_mblock_alloc_init_func init_func, const bool need_lock, + fast_mblock_alloc_init_func init_func, + void *init_args, const bool need_lock, fast_mblock_malloc_trunk_check_func malloc_trunk_check, fast_mblock_malloc_trunk_notify_func malloc_trunk_notify, void *malloc_trunk_args); @@ -153,15 +159,18 @@ parameters: element_size: element size, such as sizeof(struct xxx) alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once init_func: the init function + init_args: the args for init_func need_lock: if need lock return error no, 0 for success, != 0 fail */ static inline int fast_mblock_init_ex1(struct fast_mblock_man *mblock, const char *name, const int element_size, const int alloc_elements_once, - fast_mblock_alloc_init_func init_func, const bool need_lock) + fast_mblock_alloc_init_func init_func, + void *init_args, const bool need_lock) { return fast_mblock_init_ex2(mblock, name, element_size, - alloc_elements_once, init_func, need_lock, NULL, NULL, NULL); + alloc_elements_once, init_func, init_args, + need_lock, NULL, NULL, NULL); } /** diff --git a/src/flat_skiplist.c b/src/flat_skiplist.c index 398c069..6b7a307 100644 --- a/src/flat_skiplist.c +++ b/src/flat_skiplist.c @@ -70,9 +70,10 @@ int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count, } for (i=level_count-1; i>=0; i--) { - element_size = sizeof(FlatSkiplistNode) + sizeof(FlatSkiplistNode *) * (i + 1); + element_size = sizeof(FlatSkiplistNode) + + sizeof(FlatSkiplistNode *) * (i + 1); if ((result=fast_mblock_init_ex(sl->mblocks + i, - element_size, alloc_elements_once, NULL, false)) != 0) + element_size, alloc_elements_once, NULL, NULL, false)) != 0) { return result; } diff --git a/src/multi_skiplist.c b/src/multi_skiplist.c index 3764840..1cae4ab 100644 --- a/src/multi_skiplist.c +++ b/src/multi_skiplist.c @@ -71,9 +71,10 @@ int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, } for (i=level_count-1; i>=0; i--) { - element_size = sizeof(MultiSkiplistNode) + sizeof(MultiSkiplistNode *) * (i + 1); + element_size = sizeof(MultiSkiplistNode) + + sizeof(MultiSkiplistNode *) * (i + 1); if ((result=fast_mblock_init_ex(sl->mblocks + i, - element_size, alloc_elements_once, NULL, false)) != 0) + element_size, alloc_elements_once, NULL, NULL, false)) != 0) { return result; } @@ -98,7 +99,7 @@ int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, if ((result=fast_mblock_init_ex(&sl->data_mblock, sizeof(MultiSkiplistData), alloc_elements_once, - NULL, false)) != 0) + NULL, NULL, false)) != 0) { return result; } diff --git a/src/skiplist_set.c b/src/skiplist_set.c index b138cf7..bf9a823 100644 --- a/src/skiplist_set.c +++ b/src/skiplist_set.c @@ -70,9 +70,10 @@ int skiplist_set_init_ex(SkiplistSet *sl, const int level_count, } for (i=level_count-1; i>=0; i--) { - element_size = sizeof(SkiplistSetNode) + sizeof(SkiplistSetNode *) * (i + 1); + element_size = sizeof(SkiplistSetNode) + + sizeof(SkiplistSetNode *) * (i + 1); if ((result=fast_mblock_init_ex(sl->mblocks + i, - element_size, alloc_elements_once, NULL, false)) != 0) + element_size, alloc_elements_once, NULL, NULL, false)) != 0) { return result; } diff --git a/src/tests/test_mblock.c b/src/tests/test_mblock.c index e30bdb1..9826ecf 100644 --- a/src/tests/test_mblock.c +++ b/src/tests/test_mblock.c @@ -165,8 +165,10 @@ int main(int argc, char *argv[]) fast_mblock_manager_init(); - fast_mblock_init_ex2(&mblock1, "mblock1", 1024, 128, NULL, false, NULL, NULL, NULL); - fast_mblock_init_ex2(&mblock2, "mblock2", 1024, 100, NULL, false, NULL, NULL, NULL); + fast_mblock_init_ex2(&mblock1, "mblock1", 1024, 128, NULL, NULL, + false, NULL, NULL, NULL); + fast_mblock_init_ex2(&mblock2, "mblock2", 1024, 100, NULL, NULL, + false, NULL, NULL, NULL); count = 2048; objs = (struct my_struct *)malloc(sizeof(struct my_struct) * count); diff --git a/src/uniq_skiplist.c b/src/uniq_skiplist.c index dd43a98..9f09c2d 100644 --- a/src/uniq_skiplist.c +++ b/src/uniq_skiplist.c @@ -45,7 +45,7 @@ static void init_best_element_counts() int uniq_skiplist_init_ex(UniqSkiplistFactory *factory, const int max_level_count, skiplist_compare_func compare_func, - skiplist_free_func free_func, const int alloc_skiplist_once, + uniq_skiplist_free_func free_func, const int alloc_skiplist_once, const int min_alloc_elements_once, const int delay_free_seconds) { int bytes; @@ -94,7 +94,7 @@ int uniq_skiplist_init_ex(UniqSkiplistFactory *factory, element_size = sizeof(UniqSkiplistNode) + sizeof(UniqSkiplistNode *) * (i + 1); if ((result=fast_mblock_init_ex(factory->node_allocators + i, - element_size, alloc_elements_once, NULL, false)) != 0) + element_size, alloc_elements_once, NULL, NULL, false)) != 0) { return result; } @@ -105,7 +105,8 @@ int uniq_skiplist_init_ex(UniqSkiplistFactory *factory, if ((result=fast_mblock_init_ex(&factory->skiplist_allocator, sizeof(UniqSkiplist), alloc_skiplist_once > 0 ? - alloc_skiplist_once : 16 * 1024, NULL, false)) != 0) + alloc_skiplist_once : 16 * 1024, + NULL, NULL, false)) != 0) { return result; } @@ -236,17 +237,12 @@ void uniq_skiplist_free(UniqSkiplist *sl) return; } - fast_mblock_free_object(sl->factory->node_allocators + - sl->top_level_index, (void *)sl->top); - fast_mblock_free_object(sl->factory->node_allocators + 0, - (void *)sl->tail); - node = sl->top->links[0]; if (sl->factory->free_func != NULL) { while (node != sl->tail) { deleted = node; node = node->links[0]; - sl->factory->free_func(deleted->data); + sl->factory->free_func(deleted->data, 0); fast_mblock_free_object(sl->factory->node_allocators + deleted->level_index, (void *)deleted); } @@ -259,6 +255,11 @@ void uniq_skiplist_free(UniqSkiplist *sl) } } + fast_mblock_free_object(sl->factory->node_allocators + + sl->top_level_index, (void *)sl->top); + fast_mblock_free_object(sl->factory->node_allocators + 0, + (void *)sl->tail); + sl->top = NULL; sl->tail = NULL; sl->element_count = 0; @@ -445,7 +446,8 @@ int uniq_skiplist_delete(UniqSkiplist *sl, void *data) } if (sl->factory->free_func != NULL) { - sl->factory->free_func(deleted->data); + sl->factory->free_func(deleted->data, + sl->factory->delay_free_seconds); } UNIQ_SKIPLIST_FREE_MBLOCK_OBJECT(sl, level_index, deleted); diff --git a/src/uniq_skiplist.h b/src/uniq_skiplist.h index f589355..62075a9 100644 --- a/src/uniq_skiplist.h +++ b/src/uniq_skiplist.h @@ -18,6 +18,8 @@ #include "skiplist_common.h" #include "fast_mblock.h" +typedef void (*uniq_skiplist_free_func)(void *ptr, const int delay_seconds); + typedef struct uniq_skiplist_node { void *data; @@ -30,7 +32,7 @@ typedef struct uniq_skiplist_factory int max_level_count; int delay_free_seconds; skiplist_compare_func compare_func; - skiplist_free_func free_func; + uniq_skiplist_free_func free_func; struct fast_mblock_man skiplist_allocator; struct fast_mblock_man *node_allocators; } UniqSkiplistFactory; @@ -60,7 +62,7 @@ extern "C" { int uniq_skiplist_init_ex(UniqSkiplistFactory *factory, const int max_level_count, skiplist_compare_func compare_func, - skiplist_free_func free_func, const int alloc_skiplist_once, + uniq_skiplist_free_func free_func, const int alloc_skiplist_once, const int min_alloc_elements_once, const int delay_free_seconds); void uniq_skiplist_destroy(UniqSkiplistFactory *factory);