From 0810cb4d21e3972ddceffd122db2af0438c81034 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sun, 29 Mar 2020 22:03:13 +0800 Subject: [PATCH] bugfixed: allocator_array_check_capacity alloc size --- .gitignore | 1 + src/fast_allocator.c | 36 +++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 70795cb..6567fad 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ src/tests/test_split_string src/tests/test_uniq_skiplist src/tests/test_server_id_func src/tests/test_pipe +src/tests/test_atomic # other *.swp diff --git a/src/fast_allocator.c b/src/fast_allocator.c index d77000e..48f8b38 100644 --- a/src/fast_allocator.c +++ b/src/fast_allocator.c @@ -66,26 +66,48 @@ static int allocator_array_check_capacity(struct fast_allocator_context *acontex { int result; int bytes; + int target_count; + int alloc_count; struct fast_allocator_info **new_allocators; - if (acontext->allocator_array.alloc >= acontext->allocator_array.count + - allocator_count) + target_count = acontext->allocator_array.count + allocator_count; + if (acontext->allocator_array.alloc >= target_count) { return 0; } if (acontext->allocator_array.alloc == 0) { - acontext->allocator_array.alloc = 2 * allocator_count; + if (target_count < 128) + { + alloc_count = 128; + } + else if (target_count < 256) + { + alloc_count = 256; + } + else if (target_count < 512) + { + alloc_count = 512; + } + else if (target_count < 1024) + { + alloc_count = 1024; + } + else + { + alloc_count = 2 * target_count; + } } else { + alloc_count = acontext->allocator_array.alloc; do { - acontext->allocator_array.alloc *= 2; - } while (acontext->allocator_array.alloc < allocator_count); + alloc_count *= 2; + } while (alloc_count < target_count); } - bytes = sizeof(struct fast_allocator_info*) * acontext->allocator_array.alloc; + bytes = sizeof(struct fast_allocator_info *) * alloc_count; new_allocators = (struct fast_allocator_info **)malloc(bytes); if (new_allocators == NULL) { @@ -103,6 +125,7 @@ static int allocator_array_check_capacity(struct fast_allocator_context *acontex acontext->allocator_array.count); free(acontext->allocator_array.allocators); } + acontext->allocator_array.alloc = alloc_count; acontext->allocator_array.allocators = new_allocators; return 0; } @@ -132,7 +155,6 @@ static int region_init(struct fast_allocator_context *acontext, } memset(region->allocators, 0, bytes); - if ((result=allocator_array_check_capacity(acontext, allocator_count)) != 0) { return result;