fast_allocator.[hc] support object size

rw_perf_optimization
YuQing 2022-08-27 21:24:16 +08:00
parent 7e52e7607a
commit 82bbc013b2
4 changed files with 24 additions and 19 deletions

View File

@ -3,7 +3,7 @@ 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 * fast_allocator.[hc] support object size and 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

@ -21,6 +21,7 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx,
const int min_bits, const int max_bits, const int min_bits, const int max_bits,
const bool need_lock) const bool need_lock)
{ {
const int obj_size = 0;
const int reclaim_interval = 0; const int reclaim_interval = 0;
char name[32]; char name[32];
struct fast_region_info regions[32]; struct fast_region_info regions[32];
@ -48,7 +49,7 @@ 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, name, return fast_allocator_init_ex(&ctx->allocator, name,
NULL, regions, region - regions, 0, obj_size, NULL, regions, region - regions, 0,
0.9999, reclaim_interval, need_lock); 0.9999, reclaim_interval, need_lock);
} }

View File

@ -171,11 +171,11 @@ static int region_init(struct fast_allocator_context *acontext,
if (region->count == 1) { if (region->count == 1) {
if (region->start == 0) { if (region->start == 0) {
region->step += sizeof(struct allocator_wrapper); region->step += acontext->extra_size;
} else { } else {
region->start += sizeof(struct allocator_wrapper); region->start += acontext->extra_size;
} }
region->end += sizeof(struct allocator_wrapper); region->end += acontext->extra_size;
} }
trunk_callbacks.check_func = fast_allocator_malloc_trunk_check; trunk_callbacks.check_func = fast_allocator_malloc_trunk_check;
@ -230,11 +230,11 @@ 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_mblock_object_callbacks const char *mblock_name_prefix, const int obj_size,
*object_callbacks, struct fast_region_info *regions, struct fast_mblock_object_callbacks *object_callbacks,
const int region_count, const int64_t alloc_bytes_limit, struct fast_region_info *regions, const int region_count,
const double expect_usage_ratio, const int reclaim_interval, const int64_t alloc_bytes_limit, const double expect_usage_ratio,
const bool need_lock) const int reclaim_interval, const bool need_lock)
{ {
int result; int result;
int bytes; int bytes;
@ -269,6 +269,7 @@ int fast_allocator_init_ex(struct fast_allocator_context *acontext,
acontext->allocator_array.malloc_bytes_limit = alloc_bytes_limit / acontext->allocator_array.malloc_bytes_limit = alloc_bytes_limit /
acontext->allocator_array.expect_usage_ratio; acontext->allocator_array.expect_usage_ratio;
acontext->allocator_array.reclaim_interval = reclaim_interval; acontext->allocator_array.reclaim_interval = reclaim_interval;
acontext->extra_size = sizeof(struct allocator_wrapper) + obj_size;
acontext->need_lock = need_lock; acontext->need_lock = need_lock;
result = 0; result = 0;
previous_end = 0; previous_end = 0;
@ -361,6 +362,7 @@ int fast_allocator_init(struct fast_allocator_context *acontext,
{ {
#define DEFAULT_REGION_COUNT 5 #define DEFAULT_REGION_COUNT 5
const int obj_size = 0;
struct fast_region_info regions[DEFAULT_REGION_COUNT]; struct fast_region_info regions[DEFAULT_REGION_COUNT];
FAST_ALLOCATOR_INIT_REGION(regions[0], 0, 256, 8, 4096); FAST_ALLOCATOR_INIT_REGION(regions[0], 0, 256, 8, 4096);
@ -369,9 +371,9 @@ 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, NULL, regions, return fast_allocator_init_ex(acontext, mblock_name_prefix, obj_size,
DEFAULT_REGION_COUNT, alloc_bytes_limit, expect_usage_ratio, NULL, regions, DEFAULT_REGION_COUNT, alloc_bytes_limit,
reclaim_interval, need_lock); expect_usage_ratio, reclaim_interval, need_lock);
} }
void fast_allocator_destroy(struct fast_allocator_context *acontext) void fast_allocator_destroy(struct fast_allocator_context *acontext)
@ -477,7 +479,7 @@ void *fast_allocator_alloc(struct fast_allocator_context *acontext,
return NULL; return NULL;
} }
alloc_bytes = sizeof(struct allocator_wrapper) + bytes; alloc_bytes = acontext->extra_size + bytes;
allocator_info = get_allocator(acontext, &alloc_bytes); allocator_info = get_allocator(acontext, &alloc_bytes);
if (allocator_info->pooled) if (allocator_info->pooled)
{ {

View File

@ -60,6 +60,7 @@ struct fast_allocator_context
{ {
struct fast_region_info *regions; struct fast_region_info *regions;
int region_count; int region_count;
int extra_size;
struct fast_allocator_array allocator_array; struct fast_allocator_array allocator_array;
@ -101,6 +102,7 @@ allocator init
parameters: parameters:
acontext: the context pointer acontext: the context pointer
mblock_name_prefix: name prefix of object alloctors mblock_name_prefix: name prefix of object alloctors
obj_size: element size of object as sizeof(obj)
object_callbacks: object init and destroy callbacks 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
@ -111,11 +113,11 @@ parameters:
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_mblock_object_callbacks const char *mblock_name_prefix, const int obj_size,
*object_callbacks, struct fast_region_info *regions, struct fast_mblock_object_callbacks *object_callbacks,
const int region_count, const int64_t alloc_bytes_limit, struct fast_region_info *regions, const int region_count,
const double expect_usage_ratio, const int reclaim_interval, const int64_t alloc_bytes_limit, const double expect_usage_ratio,
const bool need_lock); const int reclaim_interval, const bool need_lock);
/** /**
allocator destroy allocator destroy