From fa154f6944ce6b104e62702b3ccc31985ab29566 Mon Sep 17 00:00:00 2001 From: Jorge Prendes Date: Thu, 25 Sep 2025 16:32:18 +0100 Subject: [PATCH] Fix large allocations in preallocated arenas Signed-off-by: Jorge Prendes --- CMakeLists.txt | 2 +- src/bitmap.c | 2 -- test/CMakeLists.txt | 4 ++++ test/test-large-arena-alloc.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/test-large-arena-alloc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ce084f6..a56197d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -708,7 +708,7 @@ if (MI_BUILD_TESTS) enable_testing() # static link tests - foreach(TEST_NAME api api-fill stress) + foreach(TEST_NAME api api-fill stress large-arena-alloc) add_executable(mimalloc-test-${TEST_NAME} test/test-${TEST_NAME}.c) target_compile_definitions(mimalloc-test-${TEST_NAME} PRIVATE ${mi_defines}) target_compile_options(mimalloc-test-${TEST_NAME} PRIVATE ${mi_cflags}) diff --git a/src/bitmap.c b/src/bitmap.c index 32d1e954..9e07d9cb 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -304,13 +304,11 @@ bool _mi_bitmap_try_find_from_claim_across(mi_bitmap_t bitmap, const size_t bitm for (size_t visited = 0; visited < bitmap_fields; visited++, idx++) { if (idx >= bitmap_fields) { idx = 0; } // wrap // first try to claim inside a field - /* if (count <= MI_BITMAP_FIELD_BITS) { if (_mi_bitmap_try_find_claim_field(bitmap, idx, count, bitmap_idx)) { return true; } } - */ // if that fails, then try to claim across fields if (mi_bitmap_try_find_claim_field_across(bitmap, bitmap_fields, idx, count, 0, bitmap_idx)) { return true; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c5fff1a6..72b2d0b0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -54,3 +54,7 @@ target_link_libraries(static-override-cxx PUBLIC mimalloc-static) ## test memory errors add_executable(test-wrong test-wrong.c) target_link_libraries(test-wrong PUBLIC mimalloc) + +## test large memory allocations in a preallocated arena +add_executable(test-large-arena-alloc test-large-arena-alloc.c) +target_link_libraries(test-large-arena-alloc PUBLIC mimalloc) diff --git a/test/test-large-arena-alloc.c b/test/test-large-arena-alloc.c new file mode 100644 index 00000000..47b48dc8 --- /dev/null +++ b/test/test-large-arena-alloc.c @@ -0,0 +1,30 @@ +// Issue #1142: Test that allocating large amounts of memory (3+ bits in the bitmap) +// uses the preallocated arena when a large enough arena is provided. +#include +#include + +#define ARENA_SIZE (256 * 1024 * 1024) +#define ALLOC_SIZE (64 * 1024 * 1024) + +char memory[ARENA_SIZE] = {0}; + +int main(void) { + mi_option_set_enabled(mi_option_disallow_os_alloc, true); + mi_option_set_enabled(mi_option_verbose, true); + + mi_manage_os_memory( + memory, + sizeof(memory), + 1 /* committed */, + 0 /* large */, + 0 /* zero */, + -1 /* numa_node */ + ); + + void * ptr = mi_malloc(ALLOC_SIZE); + + fprintf(stderr, "ptr = %p\n", ptr); + + mi_option_set_enabled(mi_option_verbose, false); + return ptr ? 0 : 1; +} \ No newline at end of file