Skip to content

Commit e6c51fc

Browse files
author
Seethepalli
committed
Add optional mimalloc allocator support for Release builds.
1 parent 3976872 commit e6c51fc

5 files changed

Lines changed: 122 additions & 1 deletion

File tree

CMake/find_runtime_deps.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ if(unresolved)
8787
endif()
8888
endforeach()
8989
endif()
90+
91+
# Add any additional runtime dependencies that weren't auto-detected
92+
# This is passed as EXTRA_RUNTIME_DEPS variable
93+
if(DEFINED EXTRA_RUNTIME_DEPS)
94+
foreach(extra_dep IN LISTS EXTRA_RUNTIME_DEPS)
95+
if(EXISTS "${extra_dep}")
96+
get_filename_component(dep_name "${extra_dep}" NAME)
97+
set(dest "${INSTALL_BIN}/${dep_name}")
98+
if(NOT EXISTS "${dest}")
99+
file(INSTALL "${extra_dep}" DESTINATION "${INSTALL_BIN}")
100+
message(STATUS "Copied extra dependency: ${dep_name}")
101+
else()
102+
message(STATUS "Extra dependency already exists: ${dep_name}")
103+
endif()
104+
# Add to the runtime dependencies list
105+
file(APPEND "${BINARY_DIR}/cvutil_runtime_dependencies-${BUILD_CONFIG}.cmake"
106+
"set(CVUTIL_RUNTIME_DEPENDENCIES \"\${CVUTIL_RUNTIME_DEPENDENCIES};\${CMAKE_CURRENT_LIST_DIR}/../../../bin/${dep_name}\")\n")
107+
endif()
108+
endforeach()
109+
endif()

CMakeLists.txt

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
2929
option(ENABLE_AVX512 "Enable AVX-512 support" OFF) # To be enabled on x86_64 server processors
3030
option(ENABLE_AVX2_FMA "Enable AVX2 and FMA support" ON) # Default support for x86_64 desktop processors
3131

32+
# Option to enable mimalloc for Release builds
33+
option(USE_MIMALLOC "Use mimalloc allocator for Release builds" ON)
34+
3235
# Detect the OS
3336
if(WIN32)
3437
message(STATUS "Configuring for Windows")
@@ -43,7 +46,7 @@ message(STATUS "Using ${CMAKE_CXX_COMPILER_ID} as the C++ compiler")
4346
# Create an interface library for compiler flags (modern CMake approach)
4447
add_library(cvutil_compiler_flags INTERFACE)
4548

46-
# MSVC-specific flags
49+
# MSVC-specific flags
4750
target_compile_options(cvutil_compiler_flags INTERFACE
4851
$<$<CXX_COMPILER_ID:MSVC>:
4952
$<$<CONFIG:Debug>:/MDd /Zi /Ob0 /Od /RTC1 /fsanitize=address>
@@ -97,6 +100,57 @@ target_link_options(cvutil_compiler_flags INTERFACE
97100
# endif()
98101
# endif()
99102

103+
# Build mimalloc for both Debug and Release builds using ExternalProject
104+
if(USE_MIMALLOC)
105+
include(ExternalProject)
106+
107+
message(STATUS "Building mimalloc as external project (will be linked for Debug and Release configurations)...")
108+
109+
# Determine build directory for mimalloc
110+
set(MIMALLOC_PREFIX ${CMAKE_BINARY_DIR}/_external/mimalloc)
111+
set(MIMALLOC_INSTALL_DIR ${MIMALLOC_PREFIX}/install)
112+
113+
# Use ExternalProject_Add for complete control - this excludes mimalloc from install
114+
# Build for both Debug (with ASAN tracking) and Release (without ASAN)
115+
ExternalProject_Add(
116+
mimalloc_external
117+
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
118+
GIT_TAG v2.2.4
119+
PREFIX ${MIMALLOC_PREFIX}
120+
CMAKE_ARGS
121+
-DCMAKE_BUILD_TYPE=$<CONFIG>
122+
-DMI_BUILD_SHARED=ON
123+
-DMI_BUILD_STATIC=OFF
124+
-DMI_BUILD_TESTS=OFF
125+
-DMI_BUILD_OBJECT=OFF
126+
-DMI_OVERRIDE=ON
127+
# $<$<CONFIG:Debug>:-DMI_TRACK_ASAN=ON>
128+
# $<$<CONFIG:Debug>:-DMI_SHOW_ERRORS=ON>
129+
-DCMAKE_INSTALL_PREFIX=${MIMALLOC_INSTALL_DIR}
130+
BUILD_BYPRODUCTS
131+
${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll
132+
${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll
133+
${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib
134+
)
135+
136+
# Create imported target for linking
137+
add_library(mimalloc SHARED IMPORTED GLOBAL)
138+
add_dependencies(mimalloc mimalloc_external)
139+
set_target_properties(mimalloc PROPERTIES
140+
IMPORTED_LOCATION ${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll
141+
IMPORTED_IMPLIB ${MIMALLOC_INSTALL_DIR}/lib/mimalloc.dll.lib
142+
)
143+
144+
# Create imported target for mimalloc-redirect
145+
add_library(mimalloc-redirect SHARED IMPORTED GLOBAL)
146+
add_dependencies(mimalloc-redirect mimalloc_external)
147+
set_target_properties(mimalloc-redirect PROPERTIES
148+
IMPORTED_LOCATION ${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll
149+
)
150+
151+
message(STATUS "mimalloc external project configured")
152+
endif()
153+
100154
# Find OpenCV
101155
find_package(OpenCV REQUIRED)
102156

@@ -175,6 +229,17 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
175229
endif()
176230
endif()
177231

232+
# Install mimalloc DLLs if used (only runtime DLLs, no headers/cmake files)
233+
if(USE_MIMALLOC)
234+
install(FILES
235+
${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll
236+
${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll
237+
CONFIGURATIONS Release
238+
DESTINATION bin
239+
COMPONENT runtime
240+
)
241+
endif()
242+
178243
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
179244
# Set RPATH to include our bundled libraries
180245
# set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib/cvutil;\$ORIGIN/../lib;/usr/lib/x86_64-linux-gnu;/lib/x86_64-linux-gnu")
@@ -329,6 +394,10 @@ install(EXPORT cvutilTargets
329394
COMPONENT development
330395
)
331396

397+
# Note: mimalloc is NOT exported in cvutilTargets
398+
# It's an internal dependency that gets loaded automatically via DLL dependencies
399+
# Applications should NOT link to cvutil::mimalloc - it doesn't exist
400+
332401
# Add to your root CMakeLists.txt
333402
configure_file(
334403
${CMAKE_CURRENT_SOURCE_DIR}/CMake/copy_runtime_dependencies.cmake

PluginManager/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ target_link_libraries(PluginManager
5959
Qt6::OpenGL
6060
)
6161

62+
# Link mimalloc for Release builds only
63+
if(USE_MIMALLOC)
64+
target_link_libraries(PluginManager PRIVATE $<$<CONFIG:Release>:mimalloc>)
65+
target_link_options(PluginManager PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
66+
message(STATUS "PluginManager will use mimalloc shared library for Release configuration")
67+
endif()
68+
6269
target_compile_definitions(PluginManager PRIVATE PLUGINMANAGER_SOURCE)
6370

6471
set(PUBLIC_HEADERS

RoiManager/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ target_link_libraries(RoiManager
5252
Qt6::OpenGL
5353
)
5454

55+
# Link mimalloc for Release builds only
56+
if(USE_MIMALLOC)
57+
target_link_libraries(RoiManager PRIVATE $<$<CONFIG:Release>:mimalloc>)
58+
target_link_options(RoiManager PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
59+
message(STATUS "RoiManager will use mimalloc shared library for Release configuration")
60+
endif()
61+
5562
target_compile_definitions(RoiManager PRIVATE ROIMANAGER_SOURCE)
5663

5764
set(PUBLIC_HEADERS

cvutil/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ target_link_libraries(cvutil
120120
RoiManager
121121
)
122122

123+
# Link mimalloc for Release builds only
124+
if(USE_MIMALLOC)
125+
if(TARGET mimalloc)
126+
target_link_libraries(cvutil PRIVATE $<$<CONFIG:Release>:mimalloc>)
127+
# Force the linker to include mimalloc.dll as a dependency
128+
target_link_options(cvutil PRIVATE $<$<CONFIG:Release>:/INCLUDE:mi_version>)
129+
message(STATUS "cvutil will use mimalloc shared library for Release configuration")
130+
else()
131+
message(WARNING "USE_MIMALLOC is ON but mimalloc target not found!")
132+
endif()
133+
endif()
134+
123135
target_compile_definitions(cvutil PRIVATE CVUTIL_SOURCE)
124136

125137
set(PUBLIC_HEADERS
@@ -233,11 +245,17 @@ endif()
233245
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
234246
# Build search directories list for runtime dependency detection
235247
set(RUNTIME_SEARCH_DIRS ${DEP_PATH})
248+
if(USE_MIMALLOC AND TARGET mimalloc)
249+
# Add mimalloc install directory to search paths
250+
list(APPEND RUNTIME_SEARCH_DIRS ${MIMALLOC_INSTALL_DIR}/bin)
251+
endif()
252+
236253
add_custom_command(TARGET cvutil POST_BUILD
237254
COMMAND ${CMAKE_COMMAND}
238255
-D TARGET_FILE=$<TARGET_FILE:cvutil>
239256
-D INSTALL_BIN=${CMAKE_INSTALL_PREFIX}/$<IF:$<BOOL:${WIN32}>,bin,lib/cvutil>
240257
-D SEARCH_DIRS="${RUNTIME_SEARCH_DIRS}"
258+
-D EXTRA_RUNTIME_DEPS="$<$<AND:$<BOOL:${USE_MIMALLOC}>,$<CONFIG:Release>>:${MIMALLOC_INSTALL_DIR}/bin/mimalloc.dll;${MIMALLOC_INSTALL_DIR}/bin/mimalloc-redirect.dll>$<$<CONFIG:Debug>:;${ASAN_RUNTIME_DEP}>"
241259
-D BUILD_CONFIG=$<CONFIG>
242260
-D BINARY_DIR=${CMAKE_BINARY_DIR}
243261
-P "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/find_runtime_deps.cmake"

0 commit comments

Comments
 (0)