Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/slotsboxmalloc/doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(从左侧) ---
Expand Down
2 changes: 1 addition & 1 deletion include/slotsboxmalloc/slotsboxobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}

/* ================================================================
Expand Down
6 changes: 3 additions & 3 deletions tutorial/03_stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -104,4 +104,4 @@ int main() {
free(data);
printf("Stress test completed after %lld loops.\n", loop_count);
return 0;
}
}
23 changes: 23 additions & 0 deletions tutorial/18_capacity_hint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define BLOCKMALLOC_IMPLEMENTATION
#include <blockmalloc/blockmalloc.h>
#define SLOTSBOXMALLOC_IMPLEMENTATION
#include <slotsboxmalloc/slotsboxobj.h>

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;
}
3 changes: 3 additions & 0 deletions tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)