From b5f3b197a4220460bbf793c8e4770f802c0d1c79 Mon Sep 17 00:00:00 2001 From: AmuroEita <1071307515@qq.com> Date: Mon, 28 Sep 2026 01:41:48 +0800 Subject: [PATCH] Fix empty child capacity hint in box allocator --- CMakeLists.txt | 1 + include/slotsboxmalloc/doc.h | 2 +- include/slotsboxmalloc/slotsboxobj.h | 2 +- tutorial/03_stress.c | 6 +++--- tutorial/18_capacity_hint.c | 23 +++++++++++++++++++++++ tutorial/CMakeLists.txt | 3 +++ 6 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 tutorial/18_capacity_hint.c diff --git a/CMakeLists.txt b/CMakeLists.txt index cc092f4..1218aa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_EXTENSIONS OFF) include(GNUInstallDirs) +include(CTest) # shared library (for FFI) + header-only (with SLOTSBOXMALLOC_IMPLEMENTATION) add_library(slotsboxmalloc SHARED diff --git a/include/slotsboxmalloc/doc.h b/include/slotsboxmalloc/doc.h index 165d7c8..68a8410 100644 --- a/include/slotsboxmalloc/doc.h +++ b/include/slotsboxmalloc/doc.h @@ -152,7 +152,7 @@ * box.children[i] = -1 * box.v2p[i] = box.p2v[i] = i // 恒等映射 (预留) * - * box.child_max_cap = {level: objlevel+1, mult: 1} // 满 box + * box.child_max_cap = {level: 0, mult: 0} // no children */ /* --- 3.2 alloc_box_slot: 分配一个子 box slot(从左侧) --- diff --git a/include/slotsboxmalloc/slotsboxobj.h b/include/slotsboxmalloc/slotsboxobj.h index 113711c..9035c62 100644 --- a/include/slotsboxmalloc/slotsboxobj.h +++ b/include/slotsboxmalloc/slotsboxobj.h @@ -292,7 +292,7 @@ static inline void sbo_box_format(sbo_meta_t *meta, sbo_box_t *box, box->slots[i].state = SBO_FREE; box->children[i] = -1; } - box->child_max_cap = (sbo_usage_t){objlevel + 1, 1}; + box->child_max_cap = (sbo_usage_t){0, 0}; } /* ================================================================ diff --git a/tutorial/03_stress.c b/tutorial/03_stress.c index d473099..1f0e554 100644 --- a/tutorial/03_stress.c +++ b/tutorial/03_stress.c @@ -10,14 +10,14 @@ int main() { // 初始化 slotsboxmalloc 的元数据和数据区,使用更大的内存池以支持高负载 uint8_t *buddy = malloc(4 * 1024 * 1024); // 元数据区,增大到4MB - uint8_t *data = malloc(64 * 1024 * 1024); // 数据区,增大到64MB + uint8_t *data = malloc(128 * 1024 * 1024); if (!buddy || !data) { perror("Failed to allocate memory for slotsboxmalloc"); return 1; } // 初始化 slotsboxmalloc - if (sbo_init(buddy, 4 * 1024 * 1024, 64 * 1024 * 1024) != 0) { + if (sbo_init(buddy, 4 * 1024 * 1024, 128 * 1024 * 1024) != 0) { printf("Failed to initialize slotsboxmalloc\n"); free(buddy); free(data); @@ -104,4 +104,4 @@ int main() { free(data); printf("Stress test completed after %lld loops.\n", loop_count); return 0; -} \ No newline at end of file +} diff --git a/tutorial/18_capacity_hint.c b/tutorial/18_capacity_hint.c new file mode 100644 index 0000000..14c4714 --- /dev/null +++ b/tutorial/18_capacity_hint.c @@ -0,0 +1,23 @@ +#define BLOCKMALLOC_IMPLEMENTATION +#include +#define SLOTSBOXMALLOC_IMPLEMENTATION +#include + +int main(void) { + sbo_box_t box; + sbo_box_format(NULL, &box, 1, 0); + if (box.child_max_cap.level != 0 || box.child_max_cap.multiple != 0) + return 1; + + for (int i = 0; i < SBO_N; i++) { + if (sbo_alloc_obj_slots(NULL, &box, 1) < 0) + return 2; + } + if (sbo_usage_cmp(sbo_box_and_child_max_cap(&box), (sbo_usage_t){0, 0}) != 0) + return 3; + + sbo_free_obj_slots(NULL, &box, 0); + if (sbo_usage_cmp(sbo_box_and_child_max_cap(&box), (sbo_usage_t){1, 1}) != 0) + return 4; + return 0; +} diff --git a/tutorial/CMakeLists.txt b/tutorial/CMakeLists.txt index 9551d40..1ba6121 100644 --- a/tutorial/CMakeLists.txt +++ b/tutorial/CMakeLists.txt @@ -71,3 +71,6 @@ target_link_libraries(16_bench_malloc slotsboxmalloc Threads::Threads) add_executable(17_data_integrity 17_data_integrity.c) target_link_libraries(17_data_integrity slotsboxmalloc) + +add_executable(18_capacity_hint 18_capacity_hint.c) +add_test(NAME 18_capacity_hint COMMAND 18_capacity_hint)