From b8fc2fdecebe29a5dd6ebc9d36670f459aca4ee3 Mon Sep 17 00:00:00 2001 From: ErosZy Date: Tue, 14 Feb 2023 21:32:32 +0800 Subject: [PATCH 1/2] feat: add mimalloc --- CMakeLists.txt | 8 +++++--- src/core/base.h | 8 +------- src/core/malloc.c | 19 ++++--------------- src/core/runtime.c | 2 +- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 79f0281d7..30c1eef44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,9 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/include) +include_directories(${PROJECT_SOURCE_DIR}/vendor/mimalloc/include) add_subdirectory(src) +add_subdirectory(vendor/mimalloc) set(COMPILE_FLAGS -Wall -MMD -Wno-array-bounds -Wno-format-truncation) if (CMAKE_BUILD_TYPE STREQUAL "Debug") @@ -49,7 +51,7 @@ if(WIN32) endif() add_executable(qjsc qjsc.c quickjs-libc.c) -target_link_libraries(qjsc quickjs ${LINK_LIBRARIES}) +target_link_libraries(qjsc quickjs ${LINK_LIBRARIES} mimalloc-static) target_compile_definitions(qjsc PUBLIC ${QJSC_CONFIG}) add_custom_command( @@ -59,7 +61,7 @@ add_custom_command( ) add_executable(qjs qjs.c quickjs-libc.c repl.c qjscalc.c) -target_link_libraries(qjs quickjs ${LINK_LIBRARIES}) +target_link_libraries(qjs quickjs ${LINK_LIBRARIES} mimalloc-static) add_executable(run-test262 run-test262.c quickjs-libc.c) -target_link_libraries(run-test262 quickjs ${LINK_LIBRARIES}) \ No newline at end of file +target_link_libraries(run-test262 quickjs ${LINK_LIBRARIES} mimalloc-static) \ No newline at end of file diff --git a/src/core/base.h b/src/core/base.h index f02747fc8..7e6ffbdb8 100644 --- a/src/core/base.h +++ b/src/core/base.h @@ -36,13 +36,7 @@ #include #include #include -#if defined(__APPLE__) -#include -#elif defined(__linux__) -#include -#elif defined(__FreeBSD__) -#include -#endif +#include "vendor/mimalloc/include/mimalloc.h" #ifdef CONFIG_BIGNUM #include "quickjs/libbf.h" diff --git a/src/core/malloc.c b/src/core/malloc.c index abe08160d..4aeda5a09 100644 --- a/src/core/malloc.c +++ b/src/core/malloc.c @@ -46,18 +46,7 @@ void js_trigger_gc(JSRuntime* rt, size_t size) { /* default memory allocation functions with memory limitation */ static inline size_t js_def_malloc_usable_size(void* ptr) { -#if defined(__APPLE__) - return malloc_size(ptr); -#elif defined(_WIN32) - return _msize(ptr); -#elif defined(EMSCRIPTEN) - return 0; -#elif defined(__linux__) - return malloc_usable_size(ptr); -#else - /* change this to `return 0;` if compilation fails */ - return malloc_usable_size(ptr); -#endif + return mi_malloc_usable_size(ptr); } size_t js_malloc_usable_size_unknown(const void* ptr) { @@ -191,7 +180,7 @@ void* js_def_malloc(JSMallocState* s, size_t size) { if (unlikely(s->malloc_size + size > s->malloc_limit)) return NULL; - ptr = malloc(size); + ptr = mi_malloc(size); if (!ptr) return NULL; @@ -206,7 +195,7 @@ void js_def_free(JSMallocState* s, void* ptr) { s->malloc_count--; s->malloc_size -= js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD; - free(ptr); + mi_free(ptr); } void* js_def_realloc(JSMallocState* s, void* ptr, size_t size) { @@ -221,7 +210,7 @@ void* js_def_realloc(JSMallocState* s, void* ptr, size_t size) { if (size == 0) { s->malloc_count--; s->malloc_size -= old_size + MALLOC_OVERHEAD; - free(ptr); + mi_free(ptr); return NULL; } if (s->malloc_size + size - old_size > s->malloc_limit) diff --git a/src/core/runtime.c b/src/core/runtime.c index 55f49e066..8504ec547 100644 --- a/src/core/runtime.c +++ b/src/core/runtime.c @@ -3087,7 +3087,7 @@ static const JSMallocFunctions def_malloc_funcs = { #elif defined(EMSCRIPTEN) NULL, #elif defined(__linux__) - (size_t(*)(const void*))malloc_usable_size, + (size_t(*)(const void*))mi_malloc_usable_size, #else /* change this to `NULL,` if compilation fails */ malloc_usable_size, From 4fa67053bdc8b4f1b25f8b1854593aa50b141448 Mon Sep 17 00:00:00 2001 From: ErosZy Date: Tue, 14 Feb 2023 21:39:28 +0800 Subject: [PATCH 2/2] feat: add mimalloc submodule --- .gitmodules | 3 +++ vendor/mimalloc | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 vendor/mimalloc diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..4df2ce32a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/mimalloc"] + path = vendor/mimalloc + url = https://github.com/microsoft/mimalloc/tree/v2.0.9 diff --git a/vendor/mimalloc b/vendor/mimalloc new file mode 160000 index 000000000..28cf67e5b --- /dev/null +++ b/vendor/mimalloc @@ -0,0 +1 @@ +Subproject commit 28cf67e5b64c704cad993c71f29a24e781bee544