Skip to content

Commit 870700e

Browse files
authored
Merge pull request #30 from openwebf/feat/mimalloc
feat: replace malloc to mimalloc
2 parents 4745d41 + 4fa6705 commit 870700e

File tree

6 files changed

+15
-26
lines changed

6 files changed

+15
-26
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "vendor/mimalloc"]
2+
path = vendor/mimalloc
3+
url = https://github.com/microsoft/mimalloc/tree/v2.0.9

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
1414

1515
include_directories(${PROJECT_SOURCE_DIR})
1616
include_directories(${PROJECT_SOURCE_DIR}/include)
17+
include_directories(${PROJECT_SOURCE_DIR}/vendor/mimalloc/include)
1718
add_subdirectory(src)
19+
add_subdirectory(vendor/mimalloc)
1820

1921
set(COMPILE_FLAGS -Wall -MMD -Wno-array-bounds -Wno-format-truncation)
2022
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -49,7 +51,7 @@ if(WIN32)
4951
endif()
5052

5153
add_executable(qjsc qjsc.c quickjs-libc.c)
52-
target_link_libraries(qjsc quickjs ${LINK_LIBRARIES})
54+
target_link_libraries(qjsc quickjs ${LINK_LIBRARIES} mimalloc-static)
5355
target_compile_definitions(qjsc PUBLIC ${QJSC_CONFIG})
5456

5557
add_custom_command(
@@ -59,7 +61,7 @@ add_custom_command(
5961
)
6062

6163
add_executable(qjs qjs.c quickjs-libc.c repl.c qjscalc.c)
62-
target_link_libraries(qjs quickjs ${LINK_LIBRARIES})
64+
target_link_libraries(qjs quickjs ${LINK_LIBRARIES} mimalloc-static)
6365

6466
add_executable(run-test262 run-test262.c quickjs-libc.c)
65-
target_link_libraries(run-test262 quickjs ${LINK_LIBRARIES})
67+
target_link_libraries(run-test262 quickjs ${LINK_LIBRARIES} mimalloc-static)

src/core/base.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@
3636
#include <time.h>
3737
#include <fenv.h>
3838
#include <math.h>
39-
#if defined(__APPLE__)
40-
#include <malloc/malloc.h>
41-
#elif defined(__linux__)
42-
#include <malloc.h>
43-
#elif defined(__FreeBSD__)
44-
#include <malloc_np.h>
45-
#endif
39+
#include "vendor/mimalloc/include/mimalloc.h"
4640

4741
#ifdef CONFIG_BIGNUM
4842
#include "quickjs/libbf.h"

src/core/malloc.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,7 @@ void js_trigger_gc(JSRuntime* rt, size_t size) {
4646

4747
/* default memory allocation functions with memory limitation */
4848
static inline size_t js_def_malloc_usable_size(void* ptr) {
49-
#if defined(__APPLE__)
50-
return malloc_size(ptr);
51-
#elif defined(_WIN32)
52-
return _msize(ptr);
53-
#elif defined(EMSCRIPTEN)
54-
return 0;
55-
#elif defined(__linux__)
56-
return malloc_usable_size(ptr);
57-
#else
58-
/* change this to `return 0;` if compilation fails */
59-
return malloc_usable_size(ptr);
60-
#endif
49+
return mi_malloc_usable_size(ptr);
6150
}
6251

6352
size_t js_malloc_usable_size_unknown(const void* ptr) {
@@ -191,7 +180,7 @@ void* js_def_malloc(JSMallocState* s, size_t size) {
191180
if (unlikely(s->malloc_size + size > s->malloc_limit))
192181
return NULL;
193182

194-
ptr = malloc(size);
183+
ptr = mi_malloc(size);
195184
if (!ptr)
196185
return NULL;
197186

@@ -206,7 +195,7 @@ void js_def_free(JSMallocState* s, void* ptr) {
206195

207196
s->malloc_count--;
208197
s->malloc_size -= js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
209-
free(ptr);
198+
mi_free(ptr);
210199
}
211200

212201
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) {
221210
if (size == 0) {
222211
s->malloc_count--;
223212
s->malloc_size -= old_size + MALLOC_OVERHEAD;
224-
free(ptr);
213+
mi_free(ptr);
225214
return NULL;
226215
}
227216
if (s->malloc_size + size - old_size > s->malloc_limit)

src/core/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3087,7 +3087,7 @@ static const JSMallocFunctions def_malloc_funcs = {
30873087
#elif defined(EMSCRIPTEN)
30883088
NULL,
30893089
#elif defined(__linux__)
3090-
(size_t(*)(const void*))malloc_usable_size,
3090+
(size_t(*)(const void*))mi_malloc_usable_size,
30913091
#else
30923092
/* change this to `NULL,` if compilation fails */
30933093
malloc_usable_size,

vendor/mimalloc

Submodule mimalloc added at 28cf67e

0 commit comments

Comments
 (0)