Skip to content

Commit 1b76a11

Browse files
committed
Upgrade bundled mimalloc to 3.0.8
1 parent a531ce4 commit 1b76a11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+7232
-5872
lines changed

third-party/mimalloc/CMakeLists.txt

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@ option(MI_PADDING "Enable padding to detect heap block overflow (alway
1010
option(MI_OVERRIDE "Override the standard malloc interface (i.e. define entry points for 'malloc', 'free', etc)" ON)
1111
option(MI_XMALLOC "Enable abort() call on memory allocation failure by default" OFF)
1212
option(MI_SHOW_ERRORS "Show error and warning messages by default (only enabled by default in DEBUG mode)" OFF)
13-
option(MI_TRACK_VALGRIND "Compile with Valgrind support (adds a small overhead)" OFF)
14-
option(MI_TRACK_ASAN "Compile with address sanitizer support (adds a small overhead)" OFF)
15-
option(MI_TRACK_ETW "Compile with Windows event tracing (ETW) support (adds a small overhead)" OFF)
13+
option(MI_GUARDED "Build with guard pages behind certain object allocations (implies MI_NO_PADDING=ON)" OFF)
1614
option(MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF)
17-
option(MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for arm64: '-march=armv8.1-a' (2016))" OFF)
15+
option(MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for x64: '-march=haswell;-mavx2' (2013), for arm64: '-march=armv8.1-a' (2016))" OFF)
16+
option(MI_OPT_SIMD "Use SIMD instructions (requires MI_OPT_ARCH to be enabled)" OFF)
1817
option(MI_SEE_ASM "Generate assembly files" OFF)
1918
option(MI_OSX_INTERPOSE "Use interpose to override standard malloc on macOS" ON)
2019
option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macOS" ON)
2120
option(MI_WIN_REDIRECT "Use redirection module ('mimalloc-redirect') on Windows if compiling mimalloc as a DLL" ON)
2221
option(MI_WIN_USE_FIXED_TLS "Use a fixed TLS slot on Windows to avoid extra tests in the malloc fast path" OFF)
2322
option(MI_LOCAL_DYNAMIC_TLS "Use local-dynamic-tls, a slightly slower but dlopen-compatible thread local storage mechanism (Unix)" OFF)
24-
option(MI_LIBC_MUSL "Set this when linking with musl libc" OFF)
23+
option(MI_LIBC_MUSL "Enable this when linking with musl libc" OFF)
24+
25+
option(MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF)
26+
option(MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF)
27+
option(MI_TRACK_VALGRIND "Compile with Valgrind support (adds a small overhead)" OFF)
28+
option(MI_TRACK_ASAN "Compile with address sanitizer support (adds a small overhead)" OFF)
29+
option(MI_TRACK_ETW "Compile with Windows event tracing (ETW) support (adds a small overhead)" OFF)
30+
2531
option(MI_BUILD_SHARED "Build shared library" ON)
2632
option(MI_BUILD_STATIC "Build static library" ON)
2733
option(MI_BUILD_OBJECT "Build object library" ON)
2834
option(MI_BUILD_TESTS "Build test executables" ON)
29-
option(MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF)
30-
option(MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF)
31-
option(MI_GUARDED "Build with guard pages behind certain object allocations (implies MI_NO_PADDING=ON)" OFF)
35+
3236
option(MI_SKIP_COLLECT_ON_EXIT "Skip collecting memory on program exit" OFF)
3337
option(MI_NO_PADDING "Force no use of padding even in DEBUG mode etc." OFF)
3438
option(MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF)
@@ -54,16 +58,16 @@ set(mi_sources
5458
src/alloc-aligned.c
5559
src/alloc-posix.c
5660
src/arena.c
61+
src/arena-meta.c
5762
src/bitmap.c
5863
src/heap.c
5964
src/init.c
6065
src/libc.c
6166
src/options.c
6267
src/os.c
6368
src/page.c
69+
src/page-map.c
6470
src/random.c
65-
src/segment.c
66-
src/segment-map.c
6771
src/stats.c
6872
src/prim/prim.c)
6973

@@ -126,7 +130,7 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|x64|amd64|AMD64)$" OR CMAKE_GENE
126130
set(MI_ARCH "x64")
127131
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv[89].?|ARM64)$" OR CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64" OR "arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
128132
set(MI_ARCH "arm64")
129-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567]|ARM)$")
133+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|armv[34567].?|ARM)$")
130134
set(MI_ARCH "arm32")
131135
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv|riscv32|riscv64)$")
132136
if(CMAKE_SIZEOF_VOID_P==4)
@@ -173,7 +177,8 @@ if(CMAKE_C_COMPILER_ID MATCHES "Intel")
173177
list(APPEND mi_cflags -Wall)
174178
endif()
175179

176-
if(CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel")
180+
# force C++ compilation with msvc or clang-cl to use modern C++ atomics
181+
if(CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" OR MI_CLANG_CL)
177182
set(MI_USE_CXX "ON")
178183
endif()
179184

@@ -278,7 +283,7 @@ endif()
278283
if(MI_SEE_ASM)
279284
message(STATUS "Generate assembly listings (MI_SEE_ASM=ON)")
280285
list(APPEND mi_cflags -save-temps)
281-
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
286+
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 14)
282287
message(STATUS "No GNU Line marker")
283288
list(APPEND mi_cflags -Wno-gnu-line-marker)
284289
endif()
@@ -427,28 +432,39 @@ if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM
427432
list(APPEND mi_cflags -ftls-model=initial-exec)
428433
endif()
429434
endif()
435+
endif()
436+
437+
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel")
430438
if(MI_OVERRIDE)
431439
list(APPEND mi_cflags -fno-builtin-malloc)
432440
endif()
433441
endif()
434442

443+
# Compiler and architecture specific flags
435444
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel" AND NOT CMAKE_SYSTEM_NAME MATCHES "Haiku")
436445
if(MI_OPT_ARCH)
437-
if(APPLE AND CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_OSX_ARCHITECTURES) # to support multi-arch binaries (#999)
446+
if(APPLE AND CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang" AND CMAKE_OSX_ARCHITECTURES) # to support multi-arch binaries (#999)
438447
if("arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
439448
list(APPEND MI_OPT_ARCH_FLAGS "-Xarch_arm64;-march=armv8.1-a")
440449
endif()
450+
if("x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES)
451+
list(APPEND MI_OPT_ARCH_FLAGS "-Xarch_x86_64;-march=haswell;-Xarch_x86_64;-mavx2")
452+
endif()
453+
elseif(MI_ARCH STREQUAL "x64")
454+
set(MI_OPT_ARCH_FLAGS "-march=haswell;-mavx2") # fast bit scan (since 2013)
441455
elseif(MI_ARCH STREQUAL "arm64")
442-
set(MI_OPT_ARCH_FLAGS "-march=armv8.1-a") # fast atomics
456+
set(MI_OPT_ARCH_FLAGS "-march=armv8.1-a") # fast atomics (since 2016)
443457
endif()
444458
endif()
445459
endif()
446460

447-
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
461+
if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914) # vs2017+
448462
list(APPEND mi_cflags /Zc:__cplusplus)
449463
if(MI_OPT_ARCH AND NOT MI_CLANG_CL)
450-
if(MI_ARCH STREQUAL "arm64")
451-
set(MI_OPT_ARCH_FLAGS "/arch:armv8.1") # fast atomics
464+
if(MI_ARCH STREQUAL "x64")
465+
set(MI_OPT_ARCH_FLAGS "/arch:AVX2")
466+
elseif(MI_ARCH STREQUAL "arm64")
467+
set(MI_OPT_ARCH_FLAGS "/arch:armv8.1")
452468
endif()
453469
endif()
454470
endif()
@@ -460,6 +476,12 @@ endif()
460476
if(MI_OPT_ARCH_FLAGS)
461477
list(APPEND mi_cflags ${MI_OPT_ARCH_FLAGS})
462478
message(STATUS "Architecture specific optimization is enabled (with ${MI_OPT_ARCH_FLAGS}) (MI_OPT_ARCH=ON)")
479+
if (MI_OPT_SIMD)
480+
list(APPEND mi_defines "MI_OPT_SIMD=1")
481+
message(STATUS "SIMD instructions are enabled (MI_OPT_SIMD=ON)")
482+
endif()
483+
elseif(MI_OPT_SIMD)
484+
message(STATUS "SIMD instructions are not enabled (either MI_OPT_ARCH=OFF or this architecture has no SIMD support)")
463485
endif()
464486

465487
# extra needed libraries
@@ -532,7 +554,9 @@ if(MI_TRACK_ASAN)
532554
endif()
533555
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
534556
list(APPEND mi_defines "MI_CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE_LC}") #todo: multi-config project needs $<CONFIG> ?
535-
if(NOT(CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$"))
557+
if(CMAKE_BUILD_TYPE_LC MATCHES "^(release|relwithdebinfo|minsizerel|none)$")
558+
list(APPEND mi_defines MI_BUILD_RELEASE)
559+
else()
536560
set(mi_libname "${mi_libname}-${CMAKE_BUILD_TYPE_LC}") #append build type (e.g. -debug) if not a release version
537561
endif()
538562

@@ -582,7 +606,7 @@ if(MI_BUILD_SHARED)
582606
install(TARGETS mimalloc EXPORT mimalloc ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
583607
install(EXPORT mimalloc DESTINATION ${mi_install_cmakedir})
584608

585-
if(WIN32)
609+
if(WIN32 AND NOT MINGW)
586610
# On windows, the import library name for the dll would clash with the static mimalloc.lib library
587611
# so we postfix the dll import library with `.dll.lib` (and also the .pdb debug file)
588612
set_property(TARGET mimalloc PROPERTY ARCHIVE_OUTPUT_NAME "${mi_libname}.dll" )
@@ -592,6 +616,9 @@ if(MI_BUILD_SHARED)
592616
# install(FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.pdb" DESTINATION ${CMAKE_INSTALL_LIBDIR})
593617
endif()
594618
if(WIN32 AND MI_WIN_REDIRECT)
619+
if(MINGW)
620+
set_property(TARGET mimalloc PROPERTY PREFIX "")
621+
endif()
595622
# On windows, link and copy the mimalloc redirection dll too.
596623
if(CMAKE_GENERATOR_PLATFORM STREQUAL "arm64ec")
597624
set(MIMALLOC_REDIRECT_SUFFIX "-arm64ec")
@@ -707,10 +734,12 @@ if (MI_BUILD_TESTS)
707734
target_compile_definitions(mimalloc-test-${TEST_NAME} PRIVATE ${mi_defines})
708735
target_compile_options(mimalloc-test-${TEST_NAME} PRIVATE ${mi_cflags})
709736
target_include_directories(mimalloc-test-${TEST_NAME} PRIVATE include)
710-
if(MI_BUILD_SHARED AND (MI_TRACK_ASAN OR MI_DEBUG_TSAN OR MI_DEBUG_UBSAN))
737+
if(MI_BUILD_STATIC AND NOT MI_DEBUG_TSAN)
738+
target_link_libraries(mimalloc-test-${TEST_NAME} PRIVATE mimalloc-static ${mi_libraries})
739+
elseif(MI_BUILD_SHARED)
711740
target_link_libraries(mimalloc-test-${TEST_NAME} PRIVATE mimalloc ${mi_libraries})
712741
else()
713-
target_link_libraries(mimalloc-test-${TEST_NAME} PRIVATE mimalloc-static ${mi_libraries})
742+
message(STATUS "cannot build TSAN tests without MI_BUILD_SHARED being enabled")
714743
endif()
715744
add_test(NAME test-${TEST_NAME} COMMAND mimalloc-test-${TEST_NAME})
716745
endforeach()
@@ -719,21 +748,19 @@ if (MI_BUILD_TESTS)
719748
if(MI_BUILD_SHARED AND NOT (MI_TRACK_ASAN OR MI_DEBUG_TSAN OR MI_DEBUG_UBSAN))
720749
add_executable(mimalloc-test-stress-dynamic test/test-stress.c)
721750
target_compile_definitions(mimalloc-test-stress-dynamic PRIVATE ${mi_defines} "USE_STD_MALLOC=1")
722-
if(WIN32)
723-
target_compile_definitions(mimalloc-test-stress-dynamic PRIVATE "MI_LINK_VERSION=1")
724-
endif()
725751
target_compile_options(mimalloc-test-stress-dynamic PRIVATE ${mi_cflags})
726752
target_include_directories(mimalloc-test-stress-dynamic PRIVATE include)
727-
target_link_libraries(mimalloc-test-stress-dynamic PRIVATE mimalloc ${mi_libraries}) # mi_version
728753
if(WIN32)
729-
add_test(NAME test-stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_SHOW_STATS=1 $<TARGET_FILE:mimalloc-test-stress-dynamic>)
754+
target_compile_definitions(mimalloc-test-stress-dynamic PRIVATE "MI_LINK_VERSION=1") # link mi_version
755+
target_link_libraries(mimalloc-test-stress-dynamic PRIVATE mimalloc ${mi_libraries}) # link mi_version
756+
add_test(NAME test-stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_VERBOSE=1 $<TARGET_FILE:mimalloc-test-stress-dynamic>)
730757
else()
731758
if(APPLE)
732759
set(LD_PRELOAD "DYLD_INSERT_LIBRARIES")
733760
else()
734761
set(LD_PRELOAD "LD_PRELOAD")
735762
endif()
736-
add_test(NAME test-stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_SHOW_STATS=1 ${LD_PRELOAD}=$<TARGET_FILE:mimalloc> $<TARGET_FILE:mimalloc-test-stress-dynamic>)
763+
add_test(NAME test-stress-dynamic COMMAND ${CMAKE_COMMAND} -E env MIMALLOC_VERBOSE=1 ${LD_PRELOAD}=$<TARGET_FILE:mimalloc> $<TARGET_FILE:mimalloc-test-stress-dynamic>)
737764
endif()
738765
endif()
739766
endif()

third-party/mimalloc/azure-pipelines.yml

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
trigger:
77
branches:
88
include:
9-
- master
10-
- dev
11-
- dev2
12-
- dev3
9+
- main
10+
- dev*
1311
tags:
1412
include:
1513
- v*
@@ -30,6 +28,10 @@ jobs:
3028
BuildType: release
3129
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release
3230
MSBuildConfiguration: Release
31+
Release SIMD:
32+
BuildType: release-simd
33+
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release -DMI_OPT_SIMD=ON -DMI_WIN_USE_FIXED_TLS=ON
34+
MSBuildConfiguration: Release
3335
Secure:
3436
BuildType: secure
3537
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release -DMI_SECURE=ON
@@ -89,6 +91,11 @@ jobs:
8991
CXX: clang++
9092
BuildType: release-clang
9193
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release
94+
Release SIMD Clang:
95+
CC: clang
96+
CXX: clang++
97+
BuildType: release-simd-clang
98+
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release -DMI_OPT_SIMD=ON
9299
Secure Clang:
93100
CC: clang
94101
CXX: clang++
@@ -148,6 +155,9 @@ jobs:
148155
Release:
149156
BuildType: release
150157
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release
158+
Release SIMD:
159+
BuildType: release-simd
160+
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release -DMI_OPT_SIMD=ON
151161
Secure:
152162
BuildType: secure
153163
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release -DMI_SECURE=ON
@@ -161,43 +171,13 @@ jobs:
161171
- script: ctest --verbose --timeout 240
162172
workingDirectory: $(BuildType)
163173
displayName: CTest
164-
165174
# - upload: $(Build.SourcesDirectory)/$(BuildType)
166175
# artifact: mimalloc-macos-$(BuildType)
167176

168177
# ----------------------------------------------------------
169178
# Other OS versions (just debug mode)
170179
# ----------------------------------------------------------
171180

172-
- job:
173-
displayName: Windows 2019
174-
pool:
175-
vmImage:
176-
windows-2019
177-
strategy:
178-
matrix:
179-
Debug:
180-
BuildType: debug
181-
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Debug -DMI_DEBUG_FULL=ON
182-
MSBuildConfiguration: Debug
183-
Release:
184-
BuildType: release
185-
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release
186-
MSBuildConfiguration: Release
187-
steps:
188-
- task: CMake@1
189-
inputs:
190-
workingDirectory: $(BuildType)
191-
cmakeArgs: .. $(cmakeExtraArgs)
192-
- task: MSBuild@1
193-
inputs:
194-
solution: $(BuildType)/libmimalloc.sln
195-
configuration: '$(MSBuildConfiguration)'
196-
msbuildArguments: -m
197-
- script: ctest --verbose --timeout 240 -C $(MSBuildConfiguration)
198-
workingDirectory: $(BuildType)
199-
displayName: CTest
200-
201181
- job:
202182
displayName: Ubuntu 24.04
203183
pool:
@@ -264,3 +244,28 @@ jobs:
264244
- script: ctest --verbose --timeout 240
265245
workingDirectory: $(BuildType)
266246
displayName: CTest
247+
248+
- job:
249+
displayName: macOS 13 (Ventura)
250+
pool:
251+
vmImage:
252+
macOS-13
253+
strategy:
254+
matrix:
255+
Debug:
256+
BuildType: debug
257+
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Debug -DMI_DEBUG_FULL=ON
258+
Release:
259+
BuildType: release
260+
cmakeExtraArgs: -DCMAKE_BUILD_TYPE=Release
261+
steps:
262+
- task: CMake@1
263+
inputs:
264+
workingDirectory: $(BuildType)
265+
cmakeArgs: .. $(cmakeExtraArgs)
266+
- script: make -j$(sysctl -n hw.ncpu) -C $(BuildType)
267+
displayName: Make
268+
- script: ctest --verbose --timeout 180
269+
workingDirectory: $(BuildType)
270+
displayName: CTest
271+

third-party/mimalloc/bin/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<span id="override_on_windows">We use a separate redirection DLL to override mimalloc on Windows</span>
44
such that we redirect all malloc/free calls that go through the (dynamic) C runtime allocator,
55
including those from other DLL's or libraries. As it intercepts all allocation calls on a low level,
6-
it can be used on large programs that include other 3rd party components.
6+
it can be used reliably on large programs that include other 3rd party components.
77
There are four requirements to make the overriding work well:
88

99
1. Use the C-runtime library as a DLL (using the `/MD` or `/MDd` switch).
@@ -63,7 +63,7 @@ need a specific redirection DLL:
6363
mode on Windows arm64. Unfortunately we cannot run x64 code emulated on Windows arm64 with
6464
the x64 mimalloc override directly (since the C runtime always uses `arm64ec`). Instead:
6565
1. Build the program as normal for x64 and link as normal with the x64
66-
`mimalloc.lib` export library.
66+
`mimalloc.dll.lib` export library.
6767
2. Now separately build `mimalloc.dll` in `arm64ec` mode and _overwrite_ your
6868
previous (x64) `mimalloc.dll` -- the loader can handle the mix of arm64ec
6969
and x64 code. Now use `mimalloc-redirect-arm64ec.dll` to match your new

third-party/mimalloc/cmake/mimalloc-config-version.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
set(mi_version_major 2)
2-
set(mi_version_minor 2)
3-
set(mi_version_patch 2)
1+
set(mi_version_major 3)
2+
set(mi_version_minor 0)
3+
set(mi_version_patch 8)
44
set(mi_version ${mi_version_major}.${mi_version_minor})
55

66
set(PACKAGE_VERSION ${mi_version})

0 commit comments

Comments
 (0)