Skip to content

Support the Windows CMake build for the mimalloc redirect#6173

Merged
Grantim merged 2 commits into
masterfrom
mimalloc-windows-cmake
May 27, 2026
Merged

Support the Windows CMake build for the mimalloc redirect#6173
Grantim merged 2 commits into
masterfrom
mimalloc-windows-cmake

Conversation

@Grantim
Copy link
Copy Markdown
Contributor

@Grantim Grantim commented May 27, 2026

Summary

  • Add cmake/Modules/MimallocRedirect.cmake with mr_enable_mimalloc_redirect(target): on Windows it find_package(mimalloc), links the imported target, and forces /INCLUDE:mi_version so mimalloc.dll loads early enough for mimalloc-redirect.dll to take over the CRT allocator process-wide. No-op on other platforms (Emscripten already links mimalloc via -sMALLOC=mimalloc).
  • Apply it to MeshViewer and MRTest, so the Windows CMake build gets the redirect — previously only the MSBuild/.vcxproj path had it.
  • Drop the hardcoded mimalloc import-lib name from MRViewerApp.vcxproj / MRTest.vcxproj. The name differs across versions (2.x mimalloc.lib, 3.x mimalloc.dll.lib); vcpkg's lib\*.lib auto-link plus the mi_version anchor already pull it in, so naming it explicitly only risked LNK1181 on toolchains resolving a different mimalloc version.

Test plan

  • Windows CMake build of MeshViewer/MRTest links and runs.
  • Windows MSBuild build links (no LNK1181).
  • mi_is_redirected() returns 1 at runtime.
  • Non-Windows CMake builds unaffected (helper is a no-op).

Add an mr_enable_mimalloc_redirect() helper and apply it to MeshViewer
and MRTest so the CMake build links mimalloc and engages
mimalloc-redirect.dll, matching the MSBuild setup. Linking the imported
mimalloc target lets CMake resolve the right import library, so also drop
the hardcoded import-lib name from those .vcxproj files (it varies by
mimalloc version - 2.x mimalloc.lib vs 3.x mimalloc.dll.lib); vcpkg's
auto-link plus the mi_version anchor already pull it in.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments are now obsolete, I believe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments are now obsolete, I believe.

Comment thread cmake/Modules/MimallocRedirect.cmake Outdated
# on every other platform (Emscripten links mimalloc via -sMALLOC=mimalloc, see
# ConfigureEmscripten.cmake). Linking the imported mimalloc target lets CMake
# resolve the version-specific import library name for us.
function(mr_enable_mimalloc_redirect target)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name is confusing since it, in fact, doesn't enable anything on any platform other than Windows. I suggest renaming it to mr_win32_enable_mimalloc_redirect and putting the WIN32 check before its calls explicitly, like:

if(WIN32)
  include(MimallocRedirect)
  mr_win32_enable_mimalloc_redirect(${PROJECT_NAME})
endif()

… comments

Rename mr_enable_mimalloc_redirect -> mr_win32_enable_mimalloc_redirect and
move the if(WIN32) guard to the call sites so the helper no longer silently
no-ops off-Windows. Trim the .vcxproj comments to just why mi_version is
force-referenced, dropping the obsolete import-lib rationale.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Grantim
Copy link
Copy Markdown
Contributor Author

Grantim commented May 27, 2026

Thanks @oitel — both addressed in 3836d62:

  • Helper naming + guard. Renamed mr_enable_mimalloc_redirectmr_win32_enable_mimalloc_redirect and moved the if(WIN32) check to the call sites (so the module is only included on Windows and the helper no longer silently no-ops elsewhere), as you suggested.
  • .vcxproj comments. Dropped the obsolete part — the import-lib naming / version rationale only mattered while we still named the lib explicitly. I kept a single one-line note on why mi_version is force-referenced (it's the anchor that makes mimalloc.dll load so mimalloc-redirect.dll can take over the allocator), since that intent isn't obvious from a bare <ForceSymbolReferences>. Happy to drop it entirely if you'd prefer.

@Grantim Grantim merged commit e0b326a into master May 27, 2026
39 checks passed
@Grantim Grantim deleted the mimalloc-windows-cmake branch May 27, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants