From 82bbc013b27271e53a8c77ecebc730a84b11f1e8 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sat, 27 Aug 2022 21:24:16 +0800 Subject: [PATCH] fast_allocator.[hc] support object size --- HISTORY | 2 +- src/array_allocator.c | 3 ++- src/fast_allocator.c | 26 ++++++++++++++------------ src/fast_allocator.h | 12 +++++++----- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/HISTORY b/HISTORY index 4ec5160..6cc08f6 100644 --- a/HISTORY +++ b/HISTORY @@ -3,7 +3,7 @@ Version 1.60 2022-08-27 * normalize_path for base_path * struct fast_task_info add field recv_body for dynamic recv buffer * 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 * open file with flag O_CLOEXEC diff --git a/src/array_allocator.c b/src/array_allocator.c index 1598ed9..ff060a5 100644 --- a/src/array_allocator.c +++ b/src/array_allocator.c @@ -21,6 +21,7 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx, const int min_bits, const int max_bits, const bool need_lock) { + const int obj_size = 0; const int reclaim_interval = 0; char name[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); 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); } diff --git a/src/fast_allocator.c b/src/fast_allocator.c index b9d78d6..71b0c5f 100644 --- a/src/fast_allocator.c +++ b/src/fast_allocator.c @@ -171,11 +171,11 @@ static int region_init(struct fast_allocator_context *acontext, if (region->count == 1) { if (region->start == 0) { - region->step += sizeof(struct allocator_wrapper); + region->step += acontext->extra_size; } 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; @@ -230,11 +230,11 @@ static void region_destroy(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 - *object_callbacks, struct fast_region_info *regions, - const int region_count, const int64_t alloc_bytes_limit, - const double expect_usage_ratio, const int reclaim_interval, - const bool need_lock) + const char *mblock_name_prefix, const int obj_size, + struct fast_mblock_object_callbacks *object_callbacks, + struct fast_region_info *regions, const int region_count, + const int64_t alloc_bytes_limit, const double expect_usage_ratio, + const int reclaim_interval, const bool need_lock) { int result; 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.expect_usage_ratio; acontext->allocator_array.reclaim_interval = reclaim_interval; + acontext->extra_size = sizeof(struct allocator_wrapper) + obj_size; acontext->need_lock = need_lock; result = 0; previous_end = 0; @@ -361,6 +362,7 @@ int fast_allocator_init(struct fast_allocator_context *acontext, { #define DEFAULT_REGION_COUNT 5 + const int obj_size = 0; struct fast_region_info regions[DEFAULT_REGION_COUNT]; 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[4], 16384, 65536, 1024, 16); - return fast_allocator_init_ex(acontext, mblock_name_prefix, NULL, regions, - DEFAULT_REGION_COUNT, alloc_bytes_limit, expect_usage_ratio, - reclaim_interval, need_lock); + return fast_allocator_init_ex(acontext, mblock_name_prefix, obj_size, + NULL, regions, DEFAULT_REGION_COUNT, alloc_bytes_limit, + expect_usage_ratio, reclaim_interval, need_lock); } void fast_allocator_destroy(struct fast_allocator_context *acontext) @@ -477,7 +479,7 @@ void *fast_allocator_alloc(struct fast_allocator_context *acontext, return NULL; } - alloc_bytes = sizeof(struct allocator_wrapper) + bytes; + alloc_bytes = acontext->extra_size + bytes; allocator_info = get_allocator(acontext, &alloc_bytes); if (allocator_info->pooled) { diff --git a/src/fast_allocator.h b/src/fast_allocator.h index 314f9ff..9e35698 100644 --- a/src/fast_allocator.h +++ b/src/fast_allocator.h @@ -60,6 +60,7 @@ struct fast_allocator_context { struct fast_region_info *regions; int region_count; + int extra_size; struct fast_allocator_array allocator_array; @@ -101,6 +102,7 @@ allocator init parameters: acontext: the context pointer mblock_name_prefix: name prefix of object alloctors + obj_size: element size of object as sizeof(obj) object_callbacks: object init and destroy callbacks regions: the region array region_count: the region count @@ -111,11 +113,11 @@ parameters: return error no, 0 for success, != 0 fail */ int fast_allocator_init_ex(struct fast_allocator_context *acontext, - 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 double expect_usage_ratio, const int reclaim_interval, - const bool need_lock); + const char *mblock_name_prefix, const int obj_size, + struct fast_mblock_object_callbacks *object_callbacks, + struct fast_region_info *regions, const int region_count, + const int64_t alloc_bytes_limit, const double expect_usage_ratio, + const int reclaim_interval, const bool need_lock); /** allocator destroy