Skip to content

Commit

Permalink
Merge pull request bellard#30 from openwebf/feat/mimalloc
Browse files Browse the repository at this point in the history
feat: replace malloc to mimalloc
  • Loading branch information
ErosZy authored Feb 14, 2023
2 parents 4745d41 + 4fa6705 commit 870700e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/mimalloc"]
path = vendor/mimalloc
url = https://github.com/microsoft/mimalloc/tree/v2.0.9
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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})
target_link_libraries(run-test262 quickjs ${LINK_LIBRARIES} mimalloc-static)
8 changes: 1 addition & 7 deletions src/core/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@
#include <time.h>
#include <fenv.h>
#include <math.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif
#include "vendor/mimalloc/include/mimalloc.h"

#ifdef CONFIG_BIGNUM
#include "quickjs/libbf.h"
Expand Down
19 changes: 4 additions & 15 deletions src/core/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions vendor/mimalloc
Submodule mimalloc added at 28cf67

0 comments on commit 870700e

Please sign in to comment.