Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ endif()

xoption(BUILD_EXAMPLES "Build examples" OFF)
xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF)
xoption(BUILD_CLI_WITH_MIMALLOC "Build the qjs executable with mimalloc" OFF)
xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF)
xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF)
xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
Expand Down Expand Up @@ -254,7 +255,11 @@ endif()
if(NOT WIN32)
set_target_properties(qjs_exe PROPERTIES ENABLE_EXPORTS TRUE)
endif()

if(BUILD_CLI_WITH_MIMALLOC)
find_package(mimalloc REQUIRED)
target_compile_definitions(qjs_exe PRIVATE QJS_USE_MIMALLOC)
target_link_libraries(qjs_exe mimalloc-static)
endif()

# Test262 runner
#
Expand Down
41 changes: 40 additions & 1 deletion qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "cutils.h"
#include "quickjs-libc.h"

#ifdef QJS_USE_MIMALLOC
#include <mimalloc.h>
#endif

extern const uint8_t qjsc_repl[];
extern const uint32_t qjsc_repl_size;

Expand Down Expand Up @@ -230,7 +234,6 @@ static void *js_trace_calloc(void *opaque, size_t count, size_t size)

static void *js_trace_malloc(void *opaque, size_t size)
{
(void) opaque;
void *ptr;
ptr = malloc(size);
js_trace_malloc_printf(opaque, "A %zd -> %p\n", size, ptr);
Expand Down Expand Up @@ -261,6 +264,38 @@ static const JSMallocFunctions trace_mf = {
js__malloc_usable_size
};

#ifdef QJS_USE_MIMALLOC
static void *js_mi_calloc(void *opaque, size_t count, size_t size)
{
return mi_calloc(count, size);
}

static void *js_mi_malloc(void *opaque, size_t size)
{
return mi_malloc(size);
}

static void js_mi_free(void *opaque, void *ptr)
{
if (!ptr)
return;
mi_free(ptr);
}

static void *js_mi_realloc(void *opaque, void *ptr, size_t size)
{
return mi_realloc(ptr, size);
}

static const JSMallocFunctions mi_mf = {
js_mi_calloc,
js_mi_malloc,
js_mi_free,
js_mi_realloc,
mi_malloc_usable_size
};
#endif

#define PROG_NAME "qjs"

void help(void)
Expand Down Expand Up @@ -438,7 +473,11 @@ int main(int argc, char **argv)
js_trace_malloc_init(&trace_data);
rt = JS_NewRuntime2(&trace_mf, &trace_data);
} else {
#ifdef QJS_USE_MIMALLOC
rt = JS_NewRuntime2(&mi_mf, NULL);
#else
rt = JS_NewRuntime();
#endif
}
if (!rt) {
fprintf(stderr, "qjs: cannot allocate JS runtime\n");
Expand Down
Loading