Repository navigation
Build mimalloc as C++ - #28000
Open
slowriot wants to merge 1 commit into
Open
Build mimalloc as C++#28000slowriot wants to merge 1 commit into
slowriot wants to merge 1 commit into
Conversation
sbc100
reviewed
Oct 17, 2026
| variation = super().get_default_variation(**kwargs) | ||
| # C-only programs must not acquire a dependency on the C++ runtime. | ||
| if not settings.LINK_AS_CXX: | ||
| variation.eh_mode = Exceptions.NONE |
Collaborator
There was a problem hiding this comment.
I wonder if we should make this the default in ExceptionLibrary?
Contributor
Author
There was a problem hiding this comment.
That sounds sensible. Would you like me to include that into this change or raise a separate PR?
Collaborator
There was a problem hiding this comment.
Lets try to land that change first / separately.
kripken
reviewed
Oct 19, 2026
| #endif | ||
|
|
||
| #if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5)) | ||
| #if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5) || defined(__clang__)) |
Member
There was a problem hiding this comment.
This looks like a change to upstream mimalloc (not the emscripten-specific parts). Has it been discussed upstream?
Collaborator
There was a problem hiding this comment.
The C path also uses || defined(__clang__), so this looks like a good candidate for upstreaming. /cc @daanx
Note that you could also fix this by compiling with -fgnuc-version=5.0.0, though this is probably safer.
$ emcc -dM -E - < /dev/null | grep -e GNUC
#define __GNUC_MINOR__ 2
#define __GNUC_PATCHLEVEL__ 1
#define __GNUC_STDC_INLINE__ 1
#define __GNUC__ 4
$ emcc -fgnuc-version=5.0.0 -dM -E - < /dev/null | grep -e GNUC
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 0
#define __GNUC_STDC_INLINE__ 1
#define __GNUC__ 5This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Emscripten currently compiles all its allocators as C. This makes no difference for dlmalloc and emmalloc, as they have no C++ feature detection and don't supply their own new/delete, but mimalloc does. As a result, building it as C causes abort on bad allocation instead of throwing std::bad_alloc, even when exceptions are enabled and
-sABORTING_MALLOC=0.This change makes sure that mimalloc is built as C++, only for C++ programs with exceptions enabled.
Changes
tools/system_libs.py: Makelibmimallocinherit fromExceptionLibrary, giving it separately cached variants for disabled exceptions, Emscripten/JavaScript exceptions, legacy Wasm exceptions and standardized Wasm exceptions. Compile mimalloc's sources with-x c++ -std=c++17for exception-enabled C++ links. All mimalloc translation units use the same language to keep internal linkage consistent; the underlyingemmalloc.candsbrk.cremain C. C-only programs explicitly select the C-built, no-exception variant, even when Wasm exception support is enabled. C++ programs without exceptions also retain the C build.embuilder.py: Add the single-threaded and multithreaded no-exception variants, plus the legacy and standardized Wasm exception variants, to the minimal prebuild task list.alloc-override.c: Allow Clang through the existing C++ aligned-allocation guard. Clang defines__GNUC__as 4 for compatibility, so the previous GCC-version check excluded it despite support for aligned allocation. The guard still requires C++17 and__cpp_aligned_new. This preserves mimalloc's alignednew/deleteimplementations when switching to C++ compilation.prim/emscripten/prim.c: Usemi_decl_externcfor the declarations ofemmalloc_freeandemmalloc_memalign. This gives themextern "C"linkage in the C++ build, matching the underlying C implementation. Their behaviour is unchanged.test/core/test_new_oom.cppand.out: Add regression coverage for scalar, array and aligned allocation: zero-sized allocation,std::bad_allocon failure, null returns from nothrow allocation, retrying a returningnew_handler, propagating or catching handler exceptions as appropriate, and successful allocation after handling a failure. The test deliberately exceedsMAXIMUM_MEMORYwithout exhausting the memory needed to construct the exception.test/test_core.py: Run the new test with dlmalloc, emmalloc and mimalloc across all three exception-handling modes. Add mimalloc cases to the existing aborting-new and nothrow-new tests, with and without memory growth.test/test_other.py: Add single-threaded and pthread C allocation tests with Wasm exception support enabled, checking that C-only links do not acquire a C++ runtime dependency.