-
Notifications
You must be signed in to change notification settings - Fork 112
Linux/macOS: link mimalloc into executables; unify allocator helper #6183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Grantim
wants to merge
9
commits into
master
Choose a base branch
from
mimalloc-native
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
10a8a04
Linux/macOS: link mimalloc into executables; unify allocator helper
Grantim e87da6b
Ubuntu 22.04: fall back to find_library when mimalloc ships no CMake …
Grantim 3b70abb
MRTest: also verify operator new and a cross-library allocation use m…
Grantim 1c6cc82
ci(diagnostic): macOS arm64 Debug only + capture mimalloc/dyld abort …
Grantim 355edac
ci: re-roll macOS runner (diagnostic, will squash)
Grantim ea2cd68
macOS: static-link mimalloc via -force_load (avoid macOS<=13 dyld TLS…
Grantim 16d7311
Revert "ci(diagnostic): macOS arm64 Debug only + capture mimalloc/dyl…
Grantim 9b06322
Mimalloc.cmake: address review comments
Grantim 43041f8
Merge branch 'master' into mimalloc-native
Grantim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # mr_enable_mimalloc(<target>) - enable the mimalloc allocator for an EXE target. | ||
| # EXE-only; call unconditionally (it branches internally). Gated by MESHLIB_USE_MIMALLOC. | ||
| # Verified by MRMesh.MimallocRedirectActive, which keys off the MR_MIMALLOC_ENABLED | ||
| # define this sets. MSBuild twin: MimallocRedirect.props. | ||
| function(mr_enable_mimalloc target) | ||
| if(NOT MESHLIB_USE_MIMALLOC) | ||
| return() | ||
| endif() | ||
|
|
||
| if(EMSCRIPTEN) | ||
| target_link_options(${target} PRIVATE "-sMALLOC=mimalloc") | ||
| return() | ||
| endif() | ||
|
|
||
| if(WIN32) | ||
| # mimalloc.dll must be the FIRST PE import (prepend) so its redirect beats | ||
| # ucrtbase; /INCLUDE:mi_version forces the import. | ||
| find_package(mimalloc CONFIG REQUIRED) | ||
| get_target_property(_existing_libs ${target} LINK_LIBRARIES) | ||
| if(NOT _existing_libs) # unset -> get_target_property yields ...-NOTFOUND | ||
| set(_existing_libs "") | ||
| endif() | ||
| set_property(TARGET ${target} PROPERTY LINK_LIBRARIES mimalloc ${_existing_libs}) | ||
| target_link_options(${target} PRIVATE "/INCLUDE:mi_version") | ||
| elseif(APPLE) | ||
| # macOS: static-link mimalloc via -force_load. brew's DYNAMIC mimalloc 3.x aborts | ||
| # at launch on macOS <= 13: an allocation during libSystem init hits mi_thread_init, | ||
| # which touches a thread-local before dyld can bootstrap it -> dyld abort. Static- | ||
| # linking ties mimalloc's init to this exe's own initializers (after libSystem), | ||
| # avoiding that early path. | ||
| find_package(mimalloc CONFIG QUIET) | ||
| if(TARGET mimalloc-static) | ||
| target_link_options(${target} PRIVATE "LINKER:-force_load,$<TARGET_FILE:mimalloc-static>") | ||
| else() | ||
| set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") # function scope; not restored for the caller | ||
| find_library(MR_MIMALLOC_STATIC NAMES mimalloc REQUIRED) | ||
| target_link_options(${target} PRIVATE "LINKER:-force_load,${MR_MIMALLOC_STATIC}") | ||
| endif() | ||
| else() | ||
| # Linux: dynamic link engages the override (ubuntu24 via CONFIG; ubuntu22's | ||
| # libmimalloc-dev ships no config, so fall back to find_library). | ||
| find_package(mimalloc CONFIG QUIET) | ||
| if(TARGET mimalloc) | ||
| target_link_libraries(${target} PRIVATE mimalloc) | ||
| else() | ||
| find_library(MR_MIMALLOC_LIBRARY NAMES mimalloc REQUIRED) | ||
| target_link_libraries(${target} PRIVATE ${MR_MIMALLOC_LIBRARY}) | ||
| endif() | ||
| endif() | ||
|
|
||
| target_compile_definitions(${target} PRIVATE MR_MIMALLOC_ENABLED) | ||
| endfunction() | ||
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ libharu | |
| libpng | ||
| libtiff | ||
| libzip | ||
| mimalloc | ||
| ninja | ||
| nlohmann-json | ||
| opencascade | ||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ libe57format | |
| libharu | ||
| libjpeg-turbo | ||
| libzip | ||
| mimalloc[override] | ||
| opencascade-minimal | ||
| openctm | ||
| openvdb | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,59 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #ifdef _WIN32 | ||
| #if defined( MR_MIMALLOC_ENABLED ) | ||
|
|
||
| #include <MRMesh/MRMesh.h> | ||
| #include <MRMesh/MRCube.h> | ||
|
|
||
| #include <cstdlib> | ||
| #include <string_view> | ||
|
|
||
| // mimalloc.h declares these as `bool`. Matching the actual return type is | ||
| // critical: MSVC returns `bool` in the low byte of EAX, so reading the call | ||
| // site as `int` picks up garbage in the upper 24 bits (e.g. -980287487). | ||
| // Local Debug happened to zero those bits; Release CI didn't. | ||
| extern "C" bool mi_is_redirected(); | ||
| // Declared here to avoid <mimalloc.h>. Return type MUST be bool, not int: MSVC | ||
| // returns bool in AL, so an int read picks up garbage in the upper bits (broke Release CI). | ||
| extern "C" bool mi_is_in_heap_region( const void* p ); | ||
| #ifdef _WIN32 | ||
| extern "C" bool mi_is_redirected(); | ||
| #endif | ||
|
|
||
| namespace MR | ||
| { | ||
|
|
||
| // Verifies mimalloc's transparent CRT-allocator redirect engaged for this EXE. | ||
| // Wired in MeshLib/source/MimallocRedirect.props (MSBuild) and | ||
| // MeshLib/cmake/Modules/MimallocRedirect.cmake (CMake). Skipped when | ||
| // MIMALLOC_DISABLE_REDIRECT=1 (mimalloc's own runtime kill-switch). | ||
| // Confirms mimalloc services real allocations, not just that the lib is linked. | ||
| // Wired by Mimalloc.cmake (CMake) / MimallocRedirect.props (MSBuild) via the | ||
| // MR_MIMALLOC_ENABLED define. Each allocation below must land in a mimalloc region: | ||
| // C malloc, C++ operator new, and an allocation made INSIDE MRMesh (cross-library - | ||
| // the real goal, what the process-wide override must reach, esp. macOS two-level ns). | ||
| // On Windows also asserts the redirect engaged; MIMALLOC_DISABLE_REDIRECT=1 skips that. | ||
| TEST( MRMesh, MimallocRedirectActive ) | ||
| { | ||
| #ifdef _WIN32 | ||
| if ( const char* disable = std::getenv( "MIMALLOC_DISABLE_REDIRECT" ); | ||
| disable && std::string_view( disable ) == "1" ) | ||
| { | ||
| GTEST_SKIP() << "MIMALLOC_DISABLE_REDIRECT=1; redirect intentionally disabled."; | ||
| } | ||
|
|
||
| EXPECT_TRUE( mi_is_redirected() ); | ||
| #endif | ||
|
|
||
| // C malloc, in this executable | ||
| void* p = std::malloc( 64 ); | ||
| ASSERT_NE( p, nullptr ); | ||
| EXPECT_TRUE( mi_is_in_heap_region( p ) ); | ||
| std::free( p ); | ||
|
|
||
| // C++ operator new, in this executable | ||
| int* q = new int[16]; | ||
| EXPECT_TRUE( mi_is_in_heap_region( q ) ); | ||
| delete[] q; | ||
|
|
||
| // cross-library: makeCube() fills points via operator new compiled into MRMesh, | ||
| // so its buffer must be a mimalloc region too (proves the override reaches the libs) | ||
| const Mesh cube = makeCube(); | ||
| ASSERT_NE( cube.points.data(), nullptr ); | ||
| EXPECT_TRUE( mi_is_in_heap_region( cube.points.data() ) ); | ||
| } | ||
|
|
||
| } // namespace MR | ||
|
|
||
| #endif // _WIN32 | ||
| #endif // MR_MIMALLOC_ENABLED |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excess comment.