Skip to content

Commit 49674d7

Browse files
committed
Export system malloc() and free() as emscripten_builtin_malloc() and emscripten_builtin_free() so that applications can have an easier mechanism to just hook in between to the existing malloc and free, but still keep calling the original dlmalloc and dlfree. Bump version to 1.36.12.
1 parent 0bff677 commit 49674d7

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

emscripten-version.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
"1.36.11"
2-
1+
"1.36.12"

system/lib/dlmalloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6017,6 +6017,15 @@ int mspace_mallopt(int param_number, int value) {
60176017

60186018
#endif /* MSPACES */
60196019

6020+
// Export malloc and free as duplicate names emscripten_builtin_malloc and
6021+
// emscripten_builtin_free so that applications can replace malloc and free
6022+
// in their code, and make those replacements refer to the original dlmalloc
6023+
// and dlfree from this file.
6024+
// This allows an easy mechanism for hooking into memory allocation.
6025+
#if defined(__EMSCRIPTEN__)
6026+
extern __typeof(malloc) emscripten_builtin_malloc __attribute__((weak, alias("malloc")));
6027+
extern __typeof(free) emscripten_builtin_free __attribute__((weak, alias("free")));
6028+
#endif
60206029

60216030
/* -------------------- Alternative MORECORE functions ------------------- */
60226031

system/lib/libc.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,8 @@
739739
W malloc_trim
740740
W malloc_usable_size
741741
W mallopt
742+
W emscripten_builtin_malloc
743+
W emscripten_builtin_free
742744
T mblen
743745
T mbrlen
744746
T mbrtowc

tests/test_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7184,6 +7184,9 @@ def test_brk(self):
71847184
def test_mallinfo(self):
71857185
self.do_run(open(path_from_root('tests', 'mallinfo.cpp')).read(), 'OK.')
71867186

7187+
def test_wrap_malloc(self):
7188+
self.do_run(open(path_from_root('tests', 'wrap_malloc.cpp')).read(), 'OK.')
7189+
71877190
# Generate tests for everything
71887191
def make_run(fullname, name=-1, compiler=-1, embetter=0, quantum_size=0,
71897192
typed_arrays=0, emcc_args=None, env=None):

tests/wrap_malloc.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int totalAllocated = 0;
5+
int totalFreed = 0;
6+
7+
extern "C"
8+
{
9+
10+
extern void* emscripten_builtin_malloc(size_t bytes);
11+
extern void emscripten_builtin_free(void* mem);
12+
13+
void * __attribute__((noinline)) malloc(size_t size)
14+
{
15+
++totalAllocated;
16+
void *ptr = emscripten_builtin_malloc(size);
17+
printf("Allocated %u bytes, got %p. %d pointers allocated total.\n", size, ptr, totalAllocated);
18+
return ptr;
19+
}
20+
21+
void __attribute__((noinline)) free(void *ptr)
22+
{
23+
++totalFreed;
24+
emscripten_builtin_free(ptr);
25+
printf("Freed ptr %p, %d pointers freed total.\n", ptr, totalFreed);
26+
}
27+
28+
}
29+
30+
int main()
31+
{
32+
for(int i = 0; i < 20; ++i)
33+
{
34+
void *ptr = malloc(1024 * 1024);
35+
free(ptr);
36+
}
37+
printf("OK.\n");
38+
}

0 commit comments

Comments
 (0)