fast_allocator.[hc] support object callbacks

rw_perf_optimization
YuQing 2022-08-27 09:49:20 +08:00
parent f47f136f56
commit 7e52e7607a
4 changed files with 54 additions and 24 deletions

View File

@ -1,8 +1,9 @@
Version 1.60 2022-08-26 Version 1.60 2022-08-27
* normalize_path for base_path * normalize_path for base_path
* struct fast_task_info add field recv_body for dynamic recv buffer * struct fast_task_info add field recv_body for dynamic recv buffer
* add functions: iniGetDoubleCorrectValueEx and iniGetPercentCorrectValueEx * add functions: iniGetDoubleCorrectValueEx and iniGetPercentCorrectValueEx
* fast_allocator.[hc] support object callbacks
Version 1.59 2022-07-21 Version 1.59 2022-07-21
* open file with flag O_CLOEXEC * open file with flag O_CLOEXEC

View File

@ -47,8 +47,8 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx,
} }
snprintf(name, sizeof(name), "%s-array", name_prefix); snprintf(name, sizeof(name), "%s-array", name_prefix);
return fast_allocator_init_ex(&ctx->allocator, return fast_allocator_init_ex(&ctx->allocator, name,
name, regions, region - regions, 0, NULL, regions, region - regions, 0,
0.9999, reclaim_interval, need_lock); 0.9999, reclaim_interval, need_lock);
} }

View File

@ -142,7 +142,8 @@ static int allocator_array_check_capacity(struct fast_allocator_context *acontex
} }
static int region_init(struct fast_allocator_context *acontext, static int region_init(struct fast_allocator_context *acontext,
const char *mblock_name_prefix, struct fast_region_info *region) const char *mblock_name_prefix, struct fast_mblock_object_callbacks
*object_callbacks, struct fast_region_info *region)
{ {
const int64_t alloc_elements_limit = 0; const int64_t alloc_elements_limit = 0;
int result; int result;
@ -198,8 +199,8 @@ static int region_init(struct fast_allocator_context *acontext,
trunk_callbacks.args = acontext; trunk_callbacks.args = acontext;
result = fast_mblock_init_ex2(&allocator->mblock, name, element_size, result = fast_mblock_init_ex2(&allocator->mblock, name, element_size,
region->alloc_elements_once, alloc_elements_limit, NULL, region->alloc_elements_once, alloc_elements_limit,
acontext->need_lock, &trunk_callbacks); object_callbacks, acontext->need_lock, &trunk_callbacks);
if (result != 0) if (result != 0)
{ {
break; break;
@ -229,7 +230,8 @@ static void region_destroy(struct fast_allocator_context *acontext,
} }
int fast_allocator_init_ex(struct fast_allocator_context *acontext, int fast_allocator_init_ex(struct fast_allocator_context *acontext,
const char *mblock_name_prefix, struct fast_region_info *regions, const char *mblock_name_prefix, struct fast_mblock_object_callbacks
*object_callbacks, struct fast_region_info *regions,
const int region_count, const int64_t alloc_bytes_limit, const int region_count, const int64_t alloc_bytes_limit,
const double expect_usage_ratio, const int reclaim_interval, const double expect_usage_ratio, const int reclaim_interval,
const bool need_lock) const bool need_lock)
@ -327,7 +329,8 @@ int fast_allocator_init_ex(struct fast_allocator_context *acontext,
} }
previous_end = pRegion->end; previous_end = pRegion->end;
if ((result=region_init(acontext, mblock_name_prefix, pRegion)) != 0) if ((result=region_init(acontext, mblock_name_prefix,
object_callbacks, pRegion)) != 0)
{ {
break; break;
} }
@ -366,7 +369,7 @@ int fast_allocator_init(struct fast_allocator_context *acontext,
FAST_ALLOCATOR_INIT_REGION(regions[3], 4096, 16384, 256, 64); FAST_ALLOCATOR_INIT_REGION(regions[3], 4096, 16384, 256, 64);
FAST_ALLOCATOR_INIT_REGION(regions[4], 16384, 65536, 1024, 16); FAST_ALLOCATOR_INIT_REGION(regions[4], 16384, 65536, 1024, 16);
return fast_allocator_init_ex(acontext, mblock_name_prefix, regions, return fast_allocator_init_ex(acontext, mblock_name_prefix, NULL, regions,
DEFAULT_REGION_COUNT, alloc_bytes_limit, expect_usage_ratio, DEFAULT_REGION_COUNT, alloc_bytes_limit, expect_usage_ratio,
reclaim_interval, need_lock); reclaim_interval, need_lock);
} }
@ -467,6 +470,7 @@ void *fast_allocator_alloc(struct fast_allocator_context *acontext,
int64_t total_reclaim_bytes; int64_t total_reclaim_bytes;
struct fast_allocator_info *allocator_info; struct fast_allocator_info *allocator_info;
void *ptr; void *ptr;
void *obj;
if (bytes < 0) if (bytes < 0)
{ {
@ -499,6 +503,7 @@ void *fast_allocator_alloc(struct fast_allocator_context *acontext,
return NULL; return NULL;
} }
} }
obj = (char *)ptr + sizeof(struct allocator_wrapper);
} }
else else
{ {
@ -512,28 +517,40 @@ void *fast_allocator_alloc(struct fast_allocator_context *acontext,
return NULL; return NULL;
} }
fast_allocator_malloc_trunk_notify_func(alloc_bytes, acontext); fast_allocator_malloc_trunk_notify_func(alloc_bytes, acontext);
obj = (char *)ptr + sizeof(struct allocator_wrapper);
if (acontext->allocator_array.allocators[0]->mblock.
object_callbacks.init_func != NULL)
{
struct fast_mblock_man *mblock;
mblock = &acontext->allocator_array.allocators[0]->mblock;
mblock->object_callbacks.init_func(obj,
mblock->object_callbacks.args);
}
} }
((struct allocator_wrapper *)ptr)->allocator_index = allocator_info->index; ((struct allocator_wrapper *)ptr)->allocator_index =
((struct allocator_wrapper *)ptr)->magic_number = allocator_info->magic_number; allocator_info->index;
((struct allocator_wrapper *)ptr)->magic_number =
allocator_info->magic_number;
((struct allocator_wrapper *)ptr)->alloc_bytes = alloc_bytes; ((struct allocator_wrapper *)ptr)->alloc_bytes = alloc_bytes;
__sync_add_and_fetch(&acontext->alloc_bytes, alloc_bytes); __sync_add_and_fetch(&acontext->alloc_bytes, alloc_bytes);
return (char *)ptr + sizeof(struct allocator_wrapper); return obj;
} }
void fast_allocator_free(struct fast_allocator_context *acontext, void *ptr) void fast_allocator_free(struct fast_allocator_context *acontext, void *obj)
{ {
struct allocator_wrapper *pWrapper; struct allocator_wrapper *pWrapper;
struct fast_allocator_info *allocator_info; struct fast_allocator_info *allocator_info;
void *obj; void *ptr;
if (ptr == NULL)
if (obj == NULL)
{ {
return; return;
} }
obj = (char *)ptr - sizeof(struct allocator_wrapper); ptr = (char *)obj - sizeof(struct allocator_wrapper);
pWrapper = (struct allocator_wrapper *)obj; pWrapper = (struct allocator_wrapper *)ptr;
if (pWrapper->allocator_index < 0 || pWrapper->allocator_index >= if (pWrapper->allocator_index < 0 || pWrapper->allocator_index >=
acontext->allocator_array.count) acontext->allocator_array.count)
{ {
@ -559,13 +576,22 @@ void fast_allocator_free(struct fast_allocator_context *acontext, void *ptr)
pWrapper->magic_number = 0; pWrapper->magic_number = 0;
if (allocator_info->pooled) if (allocator_info->pooled)
{ {
fast_mblock_free_object(&allocator_info->mblock, obj); fast_mblock_free_object(&allocator_info->mblock, ptr);
} }
else else
{ {
fast_allocator_malloc_trunk_notify_func(-1 * fast_allocator_malloc_trunk_notify_func(-1 *
pWrapper->alloc_bytes, acontext); pWrapper->alloc_bytes, acontext);
free(obj);
if (acontext->allocator_array.allocators[0]->mblock.
object_callbacks.destroy_func != NULL)
{
struct fast_mblock_man *mblock;
mblock = &acontext->allocator_array.allocators[0]->mblock;
mblock->object_callbacks.destroy_func(obj,
mblock->object_callbacks.args);
}
free(ptr);
} }
} }

View File

@ -100,16 +100,19 @@ int fast_allocator_init(struct fast_allocator_context *acontext,
allocator init allocator init
parameters: parameters:
acontext: the context pointer acontext: the context pointer
mblock_name_prefix: name prefix of object alloctors
object_callbacks: object init and destroy callbacks
regions: the region array regions: the region array
region_count: the region count region_count: the region count
alloc_bytes_limit: the alloc limit, 0 for no limit alloc_bytes_limit: the alloc limit, 0 for no limit
expect_usage_ratio: the trunk usage ratio expect_usage_ratio: the trunk usage ratio
reclaim_interval: reclaim interval in second, < 0 for never reclaim reclaim_interval: reclaim interval in second, < 0 for never reclaim
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_allocator_init_ex(struct fast_allocator_context *acontext, int fast_allocator_init_ex(struct fast_allocator_context *acontext,
const char *mblock_name_prefix, struct fast_region_info *regions, const char *mblock_name_prefix, struct fast_mblock_object_callbacks
*object_callbacks, struct fast_region_info *regions,
const int region_count, const int64_t alloc_bytes_limit, const int region_count, const int64_t alloc_bytes_limit,
const double expect_usage_ratio, const int reclaim_interval, const double expect_usage_ratio, const int reclaim_interval,
const bool need_lock); const bool need_lock);
@ -135,10 +138,10 @@ void* fast_allocator_alloc(struct fast_allocator_context *acontext,
free a node (put a node to the context) free a node (put a node to the context)
parameters: parameters:
acontext: the context pointer acontext: the context pointer
ptr: the pointer to free obj: the object ptr to free
return none return none
*/ */
void fast_allocator_free(struct fast_allocator_context *acontext, void *ptr); void fast_allocator_free(struct fast_allocator_context *acontext, void *obj);
/** /**
retry reclaim free trunks retry reclaim free trunks