Skip to content

Commit 1bf1137

Browse files
iscgarruevs
authored andcommitted
mimalloc: add a hack to fix building for Emscripten
We currently use the dlmalloc allocator as a system allocator, and only use mimalloc for the temporary heap. Unfortunately, the Emscripten port of mimalloc assumes that it's used as a system allocator and depends on the slow emmalloc for low-level access to memory allocation. Short of converting to mimalloc as a system allocator as well, our only other option is to enable emmalloc as a system allocator, but that would really slow things down everywhere. As a result, we're forced to use an ugly hack and override the symbol names, so they point to the allocator in use, rather than hard-coded to emmalloc.
1 parent c34d232 commit 1bf1137

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ set(MI_OVERRIDE OFF CACHE BOOL "")
211211
set(MI_BUILD_SHARED OFF CACHE BOOL "")
212212
set(MI_BUILD_OBJECT OFF CACHE BOOL "")
213213
set(MI_BUILD_TESTS OFF CACHE BOOL "")
214+
if(EMSCRIPTEN)
215+
# HACK: We only use mimalloc for the temporary heap, not as a general purpose
216+
# allocator. However, the Emscripten port of mimalloc assumes that it is used
217+
# as the system allocator, and depends on the slow emmalloc allocator.
218+
# We do not want to enable emmalloc, as that would slow down allocations all
219+
# over the place, so we must either use mimalloc as the system allocator as
220+
# well, or override its definitions to just use the existing system allocator.
221+
# The latter option is the least intrusive change from a stability standpoint,
222+
# so for now that's what we do here. We can try to convert to mimalloc wholsale
223+
# and remove this hack if we find the need to do so in the future.
224+
set(MI_EXTRA_CPPDEFS "emmalloc_free=free;emmalloc_memalign=memalign" CACHE INTERNAL "")
225+
endif()
214226
add_subdirectory(extlib/mimalloc EXCLUDE_FROM_ALL)
215227
set(MIMALLOC_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/extlib/mimalloc/include)
216228

0 commit comments

Comments
 (0)