build: optional mimalloc replacement for system malloc

Toggle with -Denable_mimalloc=enabled/disabled/auto.
This commit is contained in:
MatthewBeshay 2026-04-09 13:22:54 +10:00
parent b7792622a9
commit fe77d9c2a0
3 changed files with 22 additions and 1 deletions

View file

@ -92,6 +92,8 @@ simdutf_dep = dependency('simdutf',
)
miniaudio_dep = dependency('miniaudio')
mimalloc_dep = cc.find_library('mimalloc', required: get_option('enable_mimalloc'))
subdir('targets/util')
subdir('targets/java')
subdir('targets/nbt')

View file

@ -40,3 +40,10 @@ option(
value: 'frustum',
description: 'Occlusion culling mode. Off disables ALL CULLING (debug only!), Frustum disables offscreen rendering (default), BFS is experimental connectivity culling, hardware uses GPU queries.',
)
option(
'enable_mimalloc',
type: 'feature',
value: 'auto',
description: 'Link mimalloc as the malloc implementation. Requires libmimalloc-dev.',
)

View file

@ -8,7 +8,18 @@ if host_machine.system() == 'linux'
platform_sources += files(fs.read('linux_sources.txt').strip().split('\n'))
endif
client_dependencies = [
client_dependencies = []
# mimalloc must come first so the linker resolves malloc/free symbols to it
# before glibc. --no-as-needed prevents the linker from dropping it as
# "unreferenced" (the override happens via weak symbols, not direct calls).
client_link_args = []
if mimalloc_dep.found()
client_dependencies += mimalloc_dep
client_link_args += ['-Wl,--no-as-needed']
endif
client_dependencies += [
java_dep,
nbt_dep,
render_dep,
@ -52,6 +63,7 @@ client = executable(
'-D_UNICODE',
],
c_args: global_cpp_defs + ['-DUNICODE', '-D_UNICODE'],
link_args: client_link_args,
install: true,
install_dir: '',
)