Merge branch 'dev3' into dev3-bin

This commit is contained in:
daanx 2025-01-13 16:04:15 -08:00
commit 4dc3537414
10 changed files with 25 additions and 23 deletions

View file

@ -586,10 +586,13 @@ if(MI_BUILD_SHARED)
install(EXPORT mimalloc DESTINATION ${mi_install_cmakedir}) install(EXPORT mimalloc DESTINATION ${mi_install_cmakedir})
if(WIN32) if(WIN32)
# On windows, the import library name for the dll would clash with the static mimalloc.lib # On windows, the import library name for the dll would clash with the static mimalloc.lib library
# so we postfix the dll import library with `.dll.lib`. # so we postfix the dll import library with `.dll.lib` (and also the .pdb debug file)
set_property(TARGET mimalloc PROPERTY ARCHIVE_OUTPUT_NAME "${mi_libname}.dll" ) set_property(TARGET mimalloc PROPERTY ARCHIVE_OUTPUT_NAME "${mi_libname}.dll" )
install(FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.lib" DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.lib" DESTINATION ${CMAKE_INSTALL_LIBDIR})
set_property(TARGET mimalloc PROPERTY PDB_NAME "${mi_libname}.dll")
# don't try to install the pdb since it may not be generated depending on the configuration
# install(FILES "$<TARGET_FILE_DIR:mimalloc>/${mi_libname}.dll.pdb" DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif() endif()
if(WIN32 AND MI_WIN_REDIRECT) if(WIN32 AND MI_WIN_REDIRECT)
# On windows, link and copy the mimalloc redirection dll too. # On windows, link and copy the mimalloc redirection dll too.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,15 +1,14 @@
# Windows Override # Windows Override
<span id="override_on_windows">Dynamically overriding on mimalloc on Windows</span> <span id="override_on_windows">We use a separate redirection DLL to override mimalloc on Windows</span>
is robust and has the particular advantage to be able to redirect all malloc/free calls such that we redirect all malloc/free calls that go through the (dynamic) C runtime allocator,
that go through the (dynamic) C runtime allocator, including those from other DLL's or including those from other DLL's or libraries. As it intercepts all allocation calls on a low level,
libraries. As it intercepts all allocation calls on a low level, it can be used reliably it can be used reliably on large programs that include other 3rd party components.
on large programs that include other 3rd party components.
There are four requirements to make the overriding work well: There are four requirements to make the overriding work well:
1. Use the C-runtime library as a DLL (using the `/MD` or `/MDd` switch). 1. Use the C-runtime library as a DLL (using the `/MD` or `/MDd` switch).
2. Link your program explicitly with the `mimalloc.lib` export library for 2. Link your program explicitly with the `mimalloc.dll.lib` export library for
the `mimalloc.dll` -- which contains all mimalloc functionality. the `mimalloc.dll` -- which contains all mimalloc functionality.
To ensure the `mimalloc.dll` is actually loaded at run-time it is easiest To ensure the `mimalloc.dll` is actually loaded at run-time it is easiest
to insert some call to the mimalloc API in the `main` function, like `mi_version()` to insert some call to the mimalloc API in the `main` function, like `mi_version()`

View file

@ -9,7 +9,7 @@ to check out a specific commit, version, or branch of mimalloc, or set further o
You can install such custom port as: You can install such custom port as:
```sh ```sh
$ vcpkg install mimalloc[override] --recurse --overlay-ports=./contrib/vcpkg $ vcpkg install "mimalloc[override]" --recurse --overlay-ports=./contrib/vcpkg
``` ```
This will also show the correct sha512 hash if you use a custom version. This will also show the correct sha512 hash if you use a custom version.

View file

@ -17,4 +17,5 @@ endif()
if(TARGET mimalloc-static AND NOT TARGET mimalloc) if(TARGET mimalloc-static AND NOT TARGET mimalloc)
add_library(mimalloc INTERFACE IMPORTED) add_library(mimalloc INTERFACE IMPORTED)
set_target_properties(mimalloc PROPERTIES INTERFACE_LINK_LIBRARIES mimalloc-static) set_target_properties(mimalloc PROPERTIES INTERFACE_LINK_LIBRARIES mimalloc-static)
set_target_properties(mimalloc PROPERTIES INTERFACE_INCLUDE_DIRECTORIES mimalloc-static)
endif() endif()

View file

@ -165,8 +165,8 @@ mimalloc is used in various large scale low-latency services and programs, for e
## Windows ## Windows
Open `ide/vs2022/mimalloc.sln` in Visual Studio 2022 and build. Open `ide/vs2022/mimalloc.sln` in Visual Studio 2022 and build.
The `mimalloc` project builds a static library (in `out/msvc-x64`), while the The `mimalloc-lib` project builds a static library (in `out/msvc-x64`), while the
`mimalloc-override` project builds a DLL for overriding malloc `mimalloc-override-dll` project builds a DLL for overriding malloc
in the entire program. in the entire program.
## Linux, macOS, BSD, etc. ## Linux, macOS, BSD, etc.
@ -475,16 +475,15 @@ the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-i
# Windows Override # Windows Override
<span id="override_on_windows">Dynamically overriding on mimalloc on Windows</span> <span id="override_on_windows">We use a separate redirection DLL to override mimalloc on Windows</span>
is robust and has the particular advantage to be able to redirect all malloc/free calls such that we redirect all malloc/free calls that go through the (dynamic) C runtime allocator,
that go through the (dynamic) C runtime allocator, including those from other DLL's or including those from other DLL's or libraries. As it intercepts all allocation calls on a low level,
libraries. As it intercepts all allocation calls on a low level, it can be used reliably it can be used reliably on large programs that include other 3rd party components.
on large programs that include other 3rd party components.
There are four requirements to make the overriding work well: There are four requirements to make the overriding work well:
1. Use the C-runtime library as a DLL (using the `/MD` or `/MDd` switch). 1. Use the C-runtime library as a DLL (using the `/MD` or `/MDd` switch).
2. Link your program explicitly with the `mimalloc.lib` export library for the `mimalloc.dll`. 2. Link your program explicitly with the `mimalloc.dll.lib` export library for the `mimalloc.dll`.
(which must be compiled with `-DMI_OVERRIDE=ON`, which is the default though). (which must be compiled with `-DMI_OVERRIDE=ON`, which is the default though).
To ensure the `mimalloc.dll` is actually loaded at run-time it is easiest To ensure the `mimalloc.dll` is actually loaded at run-time it is easiest
to insert some call to the mimalloc API in the `main` function, like `mi_version()` to insert some call to the mimalloc API in the `main` function, like `mi_version()`
@ -501,9 +500,8 @@ There are four requirements to make the overriding work well:
list of the final executable (so it can intercept all potential allocations). list of the final executable (so it can intercept all potential allocations).
You can use `minject -l <exe>` to check this if needed. You can use `minject -l <exe>` to check this if needed.
For best performance on Windows with C++, it For best performance on Windows with C++, it is also recommended to also override
is also recommended to also override the `new`/`delete` operations (by including the `new`/`delete` operations (by including [`mimalloc-new-delete.h`](include/mimalloc-new-delete.h)
[`mimalloc-new-delete.h`](include/mimalloc-new-delete.h)
a single(!) source file in your project). a single(!) source file in your project).
The environment variable `MIMALLOC_DISABLE_REDIRECT=1` can be used to disable dynamic The environment variable `MIMALLOC_DISABLE_REDIRECT=1` can be used to disable dynamic

View file

@ -201,7 +201,8 @@ static void* unix_mmap_prim(void* addr, size_t size, size_t try_alignment, int p
void* p = NULL; void* p = NULL;
#if defined(MAP_ALIGNED) // BSD #if defined(MAP_ALIGNED) // BSD
if (addr == NULL && try_alignment > 1 && (try_alignment % _mi_os_page_size()) == 0) { if (addr == NULL && try_alignment > 1 && (try_alignment % _mi_os_page_size()) == 0) {
size_t n = mi_bsr(try_alignment); size_t idx;
size_t n = mi_bsr(try_alignment, &idx);
if (((size_t)1 << n) == try_alignment && n >= 12 && n <= 30) { // alignment is a power of 2 and 4096 <= alignment <= 1GiB if (((size_t)1 << n) == try_alignment && n >= 12 && n <= 30) { // alignment is a power of 2 and 4096 <= alignment <= 1GiB
p = mmap(addr, size, protect_flags, flags | MAP_ALIGNED(n), fd, 0); p = mmap(addr, size, protect_flags, flags | MAP_ALIGNED(n), fd, 0);
if (p==MAP_FAILED || !_mi_is_aligned(p,try_alignment)) { if (p==MAP_FAILED || !_mi_is_aligned(p,try_alignment)) {