fast_mblock.[hc]: add init_args for init_func
parent
a6066181ae
commit
90e61572c0
3
HISTORY
3
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 test file src/tests/test_pthread_lock.c
|
||||||
* add uniq_skiplist.[hc]
|
* add uniq_skiplist.[hc]
|
||||||
* add function split_string_ex
|
* add function split_string_ex
|
||||||
|
* fast_mblock.[hc]: add init_args for init_func
|
||||||
|
|
||||||
Version 1.43 2019-12-25
|
Version 1.43 2019-12-25
|
||||||
* replace function call system to getExecResult,
|
* replace function call system to getExecResult,
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ int common_blocked_queue_init_ex(struct common_blocked_queue *queue,
|
||||||
|
|
||||||
if ((result=fast_mblock_init_ex(&queue->mblock,
|
if ((result=fast_mblock_init_ex(&queue->mblock,
|
||||||
sizeof(struct common_blocked_node),
|
sizeof(struct common_blocked_node),
|
||||||
alloc_elements_once, NULL, false)) != 0)
|
alloc_elements_once, NULL, NULL, false)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ static int region_init(struct fast_allocator_context *acontext,
|
||||||
element_size+=region->step,allocator++)
|
element_size+=region->step,allocator++)
|
||||||
{
|
{
|
||||||
result = fast_mblock_init_ex2(&allocator->mblock, NULL, element_size,
|
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_check,
|
||||||
fast_allocator_malloc_trunk_notify_func, acontext);
|
fast_allocator_malloc_trunk_notify_func, acontext);
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
|
|
|
||||||
|
|
@ -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,
|
int fast_mblock_init_ex(struct fast_mblock_man *mblock,
|
||||||
const int element_size, const int alloc_elements_once,
|
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,
|
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,
|
int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
|
||||||
const int element_size, const int alloc_elements_once,
|
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,
|
||||||
fast_mblock_malloc_trunk_check_func malloc_trunk_check,
|
void *init_args, const bool need_lock,
|
||||||
fast_mblock_malloc_trunk_notify_func malloc_trunk_notify,
|
fast_mblock_malloc_trunk_check_func malloc_trunk_check,
|
||||||
void *malloc_trunk_args)
|
fast_mblock_malloc_trunk_notify_func malloc_trunk_notify,
|
||||||
|
void *malloc_trunk_args)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
int block_size;
|
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->alloc_init_func = init_func;
|
||||||
|
mblock->init_args = init_args;
|
||||||
INIT_HEAD(&mblock->trunks.head);
|
INIT_HEAD(&mblock->trunks.head);
|
||||||
mblock->info.trunk_total_count = 0;
|
mblock->info.trunk_total_count = 0;
|
||||||
mblock->info.trunk_used_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)
|
for (p=pTrunkStart; p<=pLast; p += block_size)
|
||||||
{
|
{
|
||||||
pNode = (struct fast_mblock_node *)p;
|
pNode = (struct fast_mblock_node *)p;
|
||||||
|
|
||||||
if (mblock->alloc_init_func != NULL)
|
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);
|
free(pNew);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ struct fast_mblock_chain {
|
||||||
struct fast_mblock_node *tail;
|
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)(
|
typedef int (*fast_mblock_malloc_trunk_check_func)(
|
||||||
const int alloc_bytes, void *args);
|
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
|
pthread_mutex_t lock; //the lock for read / write free node chain
|
||||||
struct fast_mblock_man *prev; //for stat manager
|
struct fast_mblock_man *prev; //for stat manager
|
||||||
struct fast_mblock_man *next; //for stat manager
|
struct fast_mblock_man *next; //for stat manager
|
||||||
|
void *init_args; //args for alloc_init_func
|
||||||
};
|
};
|
||||||
|
|
||||||
#define GET_BLOCK_SIZE(info) \
|
#define GET_BLOCK_SIZE(info) \
|
||||||
|
|
@ -108,7 +109,8 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define fast_mblock_init(mblock, element_size, alloc_elements_once) \
|
#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
|
mblock init
|
||||||
|
|
@ -117,12 +119,14 @@ parameters:
|
||||||
element_size: element size, such as sizeof(struct xxx)
|
element_size: element size, such as sizeof(struct xxx)
|
||||||
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
|
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
|
||||||
init_func: the init function
|
init_func: the init function
|
||||||
|
init_args: the args for init_func
|
||||||
need_lock: if need lock
|
need_lock: if need lock
|
||||||
return error no, 0 for success, != 0 fail
|
return error no, 0 for success, != 0 fail
|
||||||
*/
|
*/
|
||||||
int fast_mblock_init_ex(struct fast_mblock_man *mblock,
|
int fast_mblock_init_ex(struct fast_mblock_man *mblock,
|
||||||
const int element_size, const int alloc_elements_once,
|
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
|
mblock init
|
||||||
|
|
@ -132,6 +136,7 @@ parameters:
|
||||||
element_size: element size, such as sizeof(struct xxx)
|
element_size: element size, such as sizeof(struct xxx)
|
||||||
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
|
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
|
||||||
init_func: the init function
|
init_func: the init function
|
||||||
|
init_args: the args for init_func
|
||||||
need_lock: if need lock
|
need_lock: if need lock
|
||||||
malloc_trunk_check: the malloc trunk check function pointor
|
malloc_trunk_check: the malloc trunk check function pointor
|
||||||
malloc_trunk_notify: the malloc trunk notify 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,
|
int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
|
||||||
const int element_size, const int alloc_elements_once,
|
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_check_func malloc_trunk_check,
|
||||||
fast_mblock_malloc_trunk_notify_func malloc_trunk_notify,
|
fast_mblock_malloc_trunk_notify_func malloc_trunk_notify,
|
||||||
void *malloc_trunk_args);
|
void *malloc_trunk_args);
|
||||||
|
|
@ -153,15 +159,18 @@ parameters:
|
||||||
element_size: element size, such as sizeof(struct xxx)
|
element_size: element size, such as sizeof(struct xxx)
|
||||||
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
|
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
|
||||||
init_func: the init function
|
init_func: the init function
|
||||||
|
init_args: the args for init_func
|
||||||
need_lock: if need lock
|
need_lock: if need lock
|
||||||
return error no, 0 for success, != 0 fail
|
return error no, 0 for success, != 0 fail
|
||||||
*/
|
*/
|
||||||
static inline int fast_mblock_init_ex1(struct fast_mblock_man *mblock,
|
static inline int fast_mblock_init_ex1(struct fast_mblock_man *mblock,
|
||||||
const char *name, const int element_size, const int alloc_elements_once,
|
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,
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,10 @@ int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count,
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=level_count-1; i>=0; i--) {
|
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,
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,9 +71,10 @@ int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count,
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=level_count-1; i>=0; i--) {
|
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,
|
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;
|
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,
|
if ((result=fast_mblock_init_ex(&sl->data_mblock,
|
||||||
sizeof(MultiSkiplistData), alloc_elements_once,
|
sizeof(MultiSkiplistData), alloc_elements_once,
|
||||||
NULL, false)) != 0)
|
NULL, NULL, false)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,10 @@ int skiplist_set_init_ex(SkiplistSet *sl, const int level_count,
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=level_count-1; i>=0; i--) {
|
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,
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -165,8 +165,10 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
fast_mblock_manager_init();
|
fast_mblock_manager_init();
|
||||||
|
|
||||||
fast_mblock_init_ex2(&mblock1, "mblock1", 1024, 128, NULL, false, NULL, NULL, NULL);
|
fast_mblock_init_ex2(&mblock1, "mblock1", 1024, 128, NULL, NULL,
|
||||||
fast_mblock_init_ex2(&mblock2, "mblock2", 1024, 100, NULL, false, NULL, NULL, NULL);
|
false, NULL, NULL, NULL);
|
||||||
|
fast_mblock_init_ex2(&mblock2, "mblock2", 1024, 100, NULL, NULL,
|
||||||
|
false, NULL, NULL, NULL);
|
||||||
|
|
||||||
count = 2048;
|
count = 2048;
|
||||||
objs = (struct my_struct *)malloc(sizeof(struct my_struct) * count);
|
objs = (struct my_struct *)malloc(sizeof(struct my_struct) * count);
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ static void init_best_element_counts()
|
||||||
|
|
||||||
int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
|
int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
|
||||||
const int max_level_count, skiplist_compare_func compare_func,
|
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)
|
const int min_alloc_elements_once, const int delay_free_seconds)
|
||||||
{
|
{
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|
@ -94,7 +94,7 @@ int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
|
||||||
element_size = sizeof(UniqSkiplistNode) +
|
element_size = sizeof(UniqSkiplistNode) +
|
||||||
sizeof(UniqSkiplistNode *) * (i + 1);
|
sizeof(UniqSkiplistNode *) * (i + 1);
|
||||||
if ((result=fast_mblock_init_ex(factory->node_allocators + i,
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -105,7 +105,8 @@ int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
|
||||||
|
|
||||||
if ((result=fast_mblock_init_ex(&factory->skiplist_allocator,
|
if ((result=fast_mblock_init_ex(&factory->skiplist_allocator,
|
||||||
sizeof(UniqSkiplist), alloc_skiplist_once > 0 ?
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -236,17 +237,12 @@ void uniq_skiplist_free(UniqSkiplist *sl)
|
||||||
return;
|
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];
|
node = sl->top->links[0];
|
||||||
if (sl->factory->free_func != NULL) {
|
if (sl->factory->free_func != NULL) {
|
||||||
while (node != sl->tail) {
|
while (node != sl->tail) {
|
||||||
deleted = node;
|
deleted = node;
|
||||||
node = node->links[0];
|
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 +
|
fast_mblock_free_object(sl->factory->node_allocators +
|
||||||
deleted->level_index, (void *)deleted);
|
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->top = NULL;
|
||||||
sl->tail = NULL;
|
sl->tail = NULL;
|
||||||
sl->element_count = 0;
|
sl->element_count = 0;
|
||||||
|
|
@ -445,7 +446,8 @@ int uniq_skiplist_delete(UniqSkiplist *sl, void *data)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sl->factory->free_func != NULL) {
|
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);
|
UNIQ_SKIPLIST_FREE_MBLOCK_OBJECT(sl, level_index, deleted);
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@
|
||||||
#include "skiplist_common.h"
|
#include "skiplist_common.h"
|
||||||
#include "fast_mblock.h"
|
#include "fast_mblock.h"
|
||||||
|
|
||||||
|
typedef void (*uniq_skiplist_free_func)(void *ptr, const int delay_seconds);
|
||||||
|
|
||||||
typedef struct uniq_skiplist_node
|
typedef struct uniq_skiplist_node
|
||||||
{
|
{
|
||||||
void *data;
|
void *data;
|
||||||
|
|
@ -30,7 +32,7 @@ typedef struct uniq_skiplist_factory
|
||||||
int max_level_count;
|
int max_level_count;
|
||||||
int delay_free_seconds;
|
int delay_free_seconds;
|
||||||
skiplist_compare_func compare_func;
|
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 skiplist_allocator;
|
||||||
struct fast_mblock_man *node_allocators;
|
struct fast_mblock_man *node_allocators;
|
||||||
} UniqSkiplistFactory;
|
} UniqSkiplistFactory;
|
||||||
|
|
@ -60,7 +62,7 @@ extern "C" {
|
||||||
|
|
||||||
int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
|
int uniq_skiplist_init_ex(UniqSkiplistFactory *factory,
|
||||||
const int max_level_count, skiplist_compare_func compare_func,
|
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);
|
const int min_alloc_elements_once, const int delay_free_seconds);
|
||||||
|
|
||||||
void uniq_skiplist_destroy(UniqSkiplistFactory *factory);
|
void uniq_skiplist_destroy(UniqSkiplistFactory *factory);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue