Skip to content

Commit 7e151e1

Browse files
committed
Fix use of js_malloc_usable_size
Make sure the one set in the malloc functions is used rather than the default one, since it will likely use a different allocator. For some reason, this didn't cause a problem on macOS, but it does in Linux. Opsie! Added some CI to prevent these kinds of bugs.
1 parent 647e32c commit 7e151e1

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

.github/workflows/ci.yml

+34
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,37 @@ jobs:
484484
run: |
485485
cmake --build build --config Release --target qjs
486486
ls -lh build
487+
488+
mimalloc-linux:
489+
runs-on: ubuntu-24.04
490+
env:
491+
BUILD_CLI_WITH_MIMALLOC: ON
492+
MIMALLOC_SHOW_STATS: 1
493+
steps:
494+
- uses: actions/checkout@v4
495+
- name: install dependencies
496+
run: |
497+
sudo apt update && sudo apt -y install libmimalloc-dev
498+
- name: build
499+
run: |
500+
make
501+
- name: test
502+
run: |
503+
make test
504+
505+
mimalloc-macos:
506+
runs-on: macos-latest
507+
env:
508+
BUILD_CLI_WITH_STATIC_MIMALLOC: ON
509+
MIMALLOC_SHOW_STATS: 1
510+
steps:
511+
- uses: actions/checkout@v4
512+
- name: install dependencies
513+
run: |
514+
brew install mimalloc
515+
- name: build
516+
run: |
517+
make
518+
- name: test
519+
run: |
520+
make test

CMakeLists.txt

+8-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ endif()
112112
xoption(BUILD_EXAMPLES "Build examples" OFF)
113113
xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF)
114114
xoption(BUILD_CLI_WITH_MIMALLOC "Build the qjs executable with mimalloc" OFF)
115+
xoption(BUILD_CLI_WITH_STATIC_MIMALLOC "Build the qjs executable with mimalloc (statically linked)" OFF)
115116
xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF)
116117
xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF)
117118
xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
@@ -255,10 +256,14 @@ endif()
255256
if(NOT WIN32)
256257
set_target_properties(qjs_exe PROPERTIES ENABLE_EXPORTS TRUE)
257258
endif()
258-
if(BUILD_CLI_WITH_MIMALLOC)
259+
if(BUILD_CLI_WITH_MIMALLOC OR BUILD_CLI_WITH_STATIC_MIMALLOC)
259260
find_package(mimalloc REQUIRED)
260-
target_compile_definitions(qjs_exe PRIVATE QJS_USE_MIMALLOC)
261-
target_link_libraries(qjs_exe mimalloc-static)
261+
# Upstream mimalloc doesn't provide a way to know if both libraries are supported.
262+
if(BUILD_CLI_WITH_STATIC_MIMALLOC)
263+
target_link_libraries(qjs_exe mimalloc-static)
264+
else()
265+
target_link_libraries(qjs_exe mimalloc)
266+
endif()
262267
endif()
263268

264269
# Test262 runner

quickjs.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size)
14091409
return NULL;
14101410

14111411
s->malloc_count++;
1412-
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1412+
s->malloc_size += rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
14131413
return ptr;
14141414
}
14151415

@@ -1431,7 +1431,7 @@ void *js_malloc_rt(JSRuntime *rt, size_t size)
14311431
return NULL;
14321432

14331433
s->malloc_count++;
1434-
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1434+
s->malloc_size += rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
14351435
return ptr;
14361436
}
14371437

@@ -1444,7 +1444,7 @@ void js_free_rt(JSRuntime *rt, void *ptr)
14441444

14451445
s = &rt->malloc_state;
14461446
s->malloc_count--;
1447-
s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
1447+
s->malloc_size -= rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
14481448
rt->mf.js_free(s->opaque, ptr);
14491449
}
14501450

@@ -1462,7 +1462,7 @@ void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size)
14621462
js_free_rt(rt, ptr);
14631463
return NULL;
14641464
}
1465-
old_size = js__malloc_usable_size(ptr);
1465+
old_size = rt->mf.js_malloc_usable_size(ptr);
14661466
s = &rt->malloc_state;
14671467
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
14681468
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
@@ -1472,7 +1472,7 @@ void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size)
14721472
if (!ptr)
14731473
return NULL;
14741474

1475-
s->malloc_size += js__malloc_usable_size(ptr) - old_size;
1475+
s->malloc_size += rt->mf.js_malloc_usable_size(ptr) - old_size;
14761476
return ptr;
14771477
}
14781478

@@ -1734,7 +1734,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
17341734
return NULL;
17351735
/* Inline what js_malloc_rt does since we cannot use it here. */
17361736
ms.malloc_count++;
1737-
ms.malloc_size += js__malloc_usable_size(rt) + MALLOC_OVERHEAD;
1737+
ms.malloc_size += mf->js_malloc_usable_size(rt) + MALLOC_OVERHEAD;
17381738
rt->mf = *mf;
17391739
if (!rt->mf.js_malloc_usable_size) {
17401740
/* use dummy function if none provided */

0 commit comments

Comments
 (0)