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
1 change: 1 addition & 0 deletions system/include/emscripten/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ size_t emscripten_get_heap_max(void);
// dlmalloc and emmalloc.
void *emscripten_builtin_memalign(size_t alignment, size_t size);
void *emscripten_builtin_malloc(size_t size);
void *emscripten_builtin_realloc(void *ptr, size_t size);
void *emscripten_builtin_calloc(size_t nmemb, size_t size);
void emscripten_builtin_free(void *ptr);

Expand Down
1 change: 1 addition & 0 deletions system/lib/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6082,6 +6082,7 @@ int mspace_mallopt(int param_number, int value) {
// This allows an easy mechanism for hooking into memory allocation.
#if defined(__EMSCRIPTEN__) && !ONLY_MSPACES
extern __typeof(malloc) emscripten_builtin_malloc __attribute__((alias("dlmalloc")));
extern __typeof(realloc) emscripten_builtin_realloc __attribute__((alias("dlrealloc")));
extern __typeof(calloc) emscripten_builtin_calloc __attribute__((alias("dlcalloc")));
extern __typeof(free) emscripten_builtin_free __attribute__((alias("dlfree")));
extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlmemalign")));
Expand Down
1 change: 1 addition & 0 deletions system/lib/emmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ void *emmalloc_aligned_realloc_uninitialized(void *ptr, size_t alignment, size_t
void *emmalloc_realloc(void *ptr, size_t size) {
return emmalloc_aligned_realloc(ptr, MALLOC_ALIGNMENT, size);
}
EMMALLOC_ALIAS(emscripten_builtin_realloc, emmalloc_realloc);
EMMALLOC_ALIAS(__libc_realloc, emmalloc_realloc);
EMMALLOC_ALIAS(realloc, emmalloc_realloc);

Expand Down
1 change: 1 addition & 0 deletions system/lib/mimalloc/src/alloc-override.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_r

#ifdef __EMSCRIPTEN__ // emscripten adds some more on top of WASI
void* emscripten_builtin_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
void* emscripten_builtin_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
void* emscripten_builtin_free(void* p) MI_FORWARD0(mi_free, p)
void* emscripten_builtin_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
void* emscripten_builtin_calloc(size_t nmemb, size_t size) MI_FORWARD2(mi_calloc, nmemb, size)
Expand Down
15 changes: 14 additions & 1 deletion test/core/test_wrap_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

static int totalAllocs;
static int totalFrees;
static int totalReallocs;

void *malloc(size_t size) {
++totalAllocs;
Expand All @@ -19,6 +20,13 @@ void *malloc(size_t size) {
return ptr;
}

void *realloc(void* ptr, size_t size) {
++totalReallocs;
ptr = emscripten_builtin_realloc(ptr, size);
emscripten_console_logf("Reallocated %zu bytes, got %p. %d pointers re-allocated total.", size, ptr, totalReallocs);
return ptr;
}

void free(void *ptr) {
++totalFrees;
emscripten_builtin_free(ptr);
Expand All @@ -39,9 +47,14 @@ int main() {
free(ptr);
}

void* ptr = malloc(50);
ptr = realloc(ptr, 50);

emscripten_console_logf("totalAllocs: %d", totalAllocs);
emscripten_console_logf("totalFrees: %d", totalFrees);
assert(totalAllocs == 20);
emscripten_console_logf("totalReallocs: %d", totalReallocs);
assert(totalAllocs == 21);
assert(totalReallocs == 1);
assert(totalFrees == 20);
emscripten_console_logf("OK.");
return 0;
Expand Down
10 changes: 8 additions & 2 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8594,8 +8594,14 @@ def test_mallinfo(self):

@no_asan('cannot replace malloc/free with ASan')
@no_lsan('cannot replace malloc/free with LSan')
def test_wrap_malloc(self):
self.do_runf('core/test_wrap_malloc.c', 'OK.')
@parameterized({
'': ([],),
'emmalloc': (['-sMALLOC=emmalloc'],),
# FIXME(https://github.com/emscripten-core/emscripten/issues/23090)
# 'mimalloc': (['-sMALLOC=mimalloc'],),
})
def test_wrap_malloc(self, args):
self.do_runf('core/test_wrap_malloc.c', 'OK.', emcc_args=args)

def test_environment(self):
self.set_setting('ASSERTIONS')
Expand Down
2 changes: 1 addition & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ class libmalloc(MTLibrary):
def __init__(self, **kwargs):
self.malloc = kwargs.pop('malloc')
if self.malloc not in ('dlmalloc', 'emmalloc', 'emmalloc-debug', 'emmalloc-memvalidate', 'emmalloc-verbose', 'emmalloc-memvalidate-verbose', 'mimalloc', 'none'):
raise Exception('malloc must be one of "emmalloc[-debug|-memvalidate][-verbose]", "dlmalloc" or "none", see settings.js')
raise Exception('malloc must be one of "emmalloc[-debug|-memvalidate][-verbose]", "mimalloc", "dlmalloc" or "none", see settings.js')

self.is_tracing = kwargs.pop('is_tracing')
self.memvalidate = kwargs.pop('memvalidate')
Expand Down
Loading