Skip to content

Commit 63b76c0

Browse files
committed
mimalloc: add Emscripten-specific changes
1 parent 52f91b4 commit 63b76c0

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

system/lib/mimalloc/src/alloc-override.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ typedef void* mi_nothrow_t;
3535
#pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward
3636
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
3737
#else
38-
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")));
38+
// XXX EMSCRIPTEN: Add "weak"
39+
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), weak));
3940
#endif
4041
#define MI_FORWARD1(fun,x) MI_FORWARD(fun)
4142
#define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
@@ -255,9 +256,11 @@ extern "C" {
255256
#endif
256257

257258
// No forwarding here due to aliasing/name mangling issues
259+
mi_decl_weak // XXX EMSCRIPTEN
258260
void* valloc(size_t size) { return mi_valloc(size); }
259261
void vfree(void* p) { mi_free(p); }
260262
size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }
263+
mi_decl_weak // XXX EMSCRIPTEN
261264
int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
262265

263266
// `aligned_alloc` is only available when __USE_ISOC11 is defined.
@@ -268,15 +271,18 @@ extern "C" {
268271
// Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
269272
// uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
270273
#if !defined(__GLIBC__) || __USE_ISOC11
274+
mi_decl_weak // XXX EMSCRIPTEN
271275
void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
272276
#endif
273277
#endif
274278

275279
// no forwarding here due to aliasing/name mangling issues
276280
void cfree(void* p) { mi_free(p); }
277281
void* pvalloc(size_t size) { return mi_pvalloc(size); }
282+
mi_decl_weak // XXX EMSCRIPTEN
278283
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
279284
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
285+
mi_decl_weak // XXX EMSCRIPTEN
280286
void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
281287
// some systems define reallocarr so mark it as a weak symbol (#751)
282288
mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
@@ -287,8 +293,16 @@ mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_r
287293
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)
288294
void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
289295
void __libc_free(void* p) MI_FORWARD0(mi_free, p)
296+
mi_decl_weak // XXX EMSCRIPTEN
290297
void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
291298

299+
#ifdef __EMSCRIPTEN__ // emscripten adds some more on top of WASI
300+
void* emscripten_builtin_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
301+
void* emscripten_builtin_free(void* p) MI_FORWARD0(mi_free, p)
302+
void* emscripten_builtin_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
303+
void* emscripten_builtin_calloc(size_t nmemb, size_t size) MI_FORWARD2(mi_calloc, nmemb, size)
304+
#endif
305+
292306
#elif defined(__linux__)
293307
// forward __libc interface (needed for glibc-based and musl-based Linux distributions)
294308
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)

system/lib/mimalloc/src/prim/emscripten/prim.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extern void* emmalloc_memalign(size_t alignment, size_t size);
7272

7373
// Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned.
7474
int _mi_prim_alloc(void* hint_addr, size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero, void** addr) {
75-
MI_UNUSED(try_alignment); MI_UNUSED(allow_large); MI_UNUSED(commit); MI_UNUSED(hint_addr);
75+
MI_UNUSED(allow_large); MI_UNUSED(commit); MI_UNUSED(hint_addr);
7676
*is_large = false;
7777
// TODO: Track the highest address ever seen; first uses of it are zeroes.
7878
// That assumes no one else uses sbrk but us (they could go up,
@@ -83,12 +83,8 @@ int _mi_prim_alloc(void* hint_addr, size_t size, size_t try_alignment, bool comm
8383
if (try_alignment < MIN_EMMALLOC_ALIGN) {
8484
try_alignment = MIN_EMMALLOC_ALIGN;
8585
}
86-
void* p = emmalloc_memalign(try_alignment, size);
87-
*addr = p;
88-
if (p == 0) {
89-
return ENOMEM;
90-
}
91-
return 0;
86+
*addr = emmalloc_memalign(try_alignment, size);
87+
return (*addr != NULL ? 0 : ENOMEM);
9288
}
9389

9490

@@ -169,7 +165,7 @@ void _mi_prim_process_info(mi_process_info_t* pinfo)
169165
#include <emscripten/console.h>
170166

171167
void _mi_prim_out_stderr( const char* msg) {
172-
emscripten_console_error(msg);
168+
emscripten_err(msg);
173169
}
174170

175171

@@ -218,7 +214,9 @@ void _mi_prim_thread_init_auto_done(void) {
218214
}
219215

220216
void _mi_prim_thread_done_auto_done(void) {
221-
// nothing to do
217+
if (_mi_heap_default_key != (pthread_key_t)(-1)) { // do not leak the key, see issue #809
218+
pthread_key_delete(_mi_heap_default_key);
219+
}
222220
}
223221

224222
void _mi_prim_thread_associate_default_heap(mi_heap_t* heap) {

0 commit comments

Comments
 (0)