From 152d6bbd4961810ac3d4387a23b5ba205c848785 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 1 Jul 2025 15:34:26 +0800 Subject: [PATCH] test mimalloc Update cmake_build.py Update CMakeLists.txt Update CMakeLists.txt --- .gitignore | 1 + CMakeLists.txt | 5 ++++- cmake_build.py | 2 +- include/pocketpy/common/utils.h | 1 + include/pocketpy/config.h | 2 +- src2/main.c | 4 ++-- 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index f4b0ceb0..5d28157c 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ docs/C-API/functions.md cmake-build-* +tmp/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 37f24fc5..533fbf35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,10 +86,13 @@ if(PK_ENABLE_MIMALLOC) ) set(MI_OVERRIDE OFF CACHE BOOL "" FORCE) + set(MI_NO_USE_CXX ON CACHE BOOL "" FORCE) set(MI_BUILD_SHARED OFF CACHE BOOL "" FORCE) + set(MI_BUILD_OBJECT OFF CACHE BOOL "" FORCE) set(MI_BUILD_STATIC ON CACHE BOOL "" FORCE) set(MI_BUILD_TESTS OFF CACHE BOOL "" FORCE) FetchContent_MakeAvailable(mimalloc) + include_directories(${mimalloc_SOURCE_DIR}/include) add_definitions(-DPK_ENABLE_MIMALLOC=1) else() @@ -173,5 +176,5 @@ if(PK_BUILD_MODULE_LIBHV) endif() if(PK_ENABLE_MIMALLOC) - target_link_libraries(${PROJECT_NAME} mimalloc::mimalloc) + target_link_libraries(${PROJECT_NAME} mimalloc-static) endif() \ No newline at end of file diff --git a/cmake_build.py b/cmake_build.py index 8be0b2f2..954f9650 100644 --- a/cmake_build.py +++ b/cmake_build.py @@ -20,7 +20,7 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo'] os.chdir("build") -code = os.system(f"cmake .. -DPK_ENABLE_DETERMINISM=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}") +code = os.system(f"cmake .. -DPK_ENABLE_MIMALLOC=ON -DPK_ENABLE_DETERMINISM=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}") assert code == 0 code = os.system(f"cmake --build . --config {config} -j 4") assert code == 0 diff --git a/include/pocketpy/common/utils.h b/include/pocketpy/common/utils.h index 96514e2a..ef6f1134 100644 --- a/include/pocketpy/common/utils.h +++ b/include/pocketpy/common/utils.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #define PK_REGION(name) 1 diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 2d711333..a6571453 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -85,7 +85,7 @@ // Memory allocation functions #ifndef PK_MALLOC #if PK_ENABLE_MIMALLOC - #include + #include "mimalloc.h" #define PK_MALLOC(size) mi_malloc(size) #define PK_REALLOC(ptr, size) mi_realloc(ptr, size) #define PK_FREE(ptr) mi_free(ptr) diff --git a/src2/main.c b/src2/main.c index 52e1d2e1..14e16421 100644 --- a/src2/main.c +++ b/src2/main.c @@ -19,7 +19,7 @@ static char* read_file(const char* path) { fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); - char* buffer = malloc(size + 1); + char* buffer = PK_MALLOC(size + 1); size = fread(buffer, 1, size, file); buffer[size] = 0; return buffer; @@ -84,7 +84,7 @@ int main(int argc, char** argv) { char* source = read_file(filename); if(source) { if(!py_exec(source, filename, EXEC_MODE, NULL)) py_printexc(); - free(source); + PK_FREE(source); } }