Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix use of js_malloc_usable_size #544

Merged
merged 1 commit into from
Sep 24, 2024
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
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,37 @@ jobs:
run: |
cmake --build build --config Release --target qjs
ls -lh build

mimalloc-linux:
runs-on: ubuntu-24.04
env:
BUILD_CLI_WITH_MIMALLOC: ON
MIMALLOC_SHOW_STATS: 1
steps:
- uses: actions/checkout@v4
- name: install dependencies
run: |
sudo apt update && sudo apt -y install libmimalloc-dev
- name: build
run: |
make
- name: test
run: |
make test

mimalloc-macos:
runs-on: macos-latest
env:
BUILD_CLI_WITH_STATIC_MIMALLOC: ON
MIMALLOC_SHOW_STATS: 1
steps:
- uses: actions/checkout@v4
- name: install dependencies
run: |
brew install mimalloc
- name: build
run: |
make
- name: test
run: |
make test
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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(BUILD_CLI_WITH_STATIC_MIMALLOC "Build the qjs executable with mimalloc (statically linked)" 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 @@ -255,10 +256,14 @@ endif()
if(NOT WIN32)
set_target_properties(qjs_exe PROPERTIES ENABLE_EXPORTS TRUE)
endif()
if(BUILD_CLI_WITH_MIMALLOC)
if(BUILD_CLI_WITH_MIMALLOC OR BUILD_CLI_WITH_STATIC_MIMALLOC)
find_package(mimalloc REQUIRED)
target_compile_definitions(qjs_exe PRIVATE QJS_USE_MIMALLOC)
target_link_libraries(qjs_exe mimalloc-static)
# Upstream mimalloc doesn't provide a way to know if both libraries are supported.
if(BUILD_CLI_WITH_STATIC_MIMALLOC)
target_link_libraries(qjs_exe mimalloc-static)
else()
target_link_libraries(qjs_exe mimalloc)
endif()
endif()

# Test262 runner
Expand Down
12 changes: 6 additions & 6 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size)
return NULL;

s->malloc_count++;
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size += rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
return ptr;
}

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

s->malloc_count++;
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size += rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
return ptr;
}

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

s = &rt->malloc_state;
s->malloc_count--;
s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size -= rt->mf.js_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
rt->mf.js_free(s->opaque, ptr);
}

Expand All @@ -1462,7 +1462,7 @@ void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size)
js_free_rt(rt, ptr);
return NULL;
}
old_size = js__malloc_usable_size(ptr);
old_size = rt->mf.js_malloc_usable_size(ptr);
s = &rt->malloc_state;
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
if (s->malloc_size + size - old_size > s->malloc_limit - 1)
Expand All @@ -1472,7 +1472,7 @@ void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size)
if (!ptr)
return NULL;

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

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