diff --git a/CMakeLists.txt b/CMakeLists.txt index 27e52d58..e2976bbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -586,10 +586,13 @@ if(MI_BUILD_SHARED) install(EXPORT mimalloc DESTINATION ${mi_install_cmakedir}) if(WIN32) - # On windows, the import library name for the dll would clash with the static mimalloc.lib - # so we postfix the dll import library with `.dll.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` (and also the .pdb debug file) set_property(TARGET mimalloc PROPERTY ARCHIVE_OUTPUT_NAME "${mi_libname}.dll" ) install(FILES "$/${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 "$/${mi_libname}.dll.pdb" DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() if(WIN32 AND MI_WIN_REDIRECT) # On windows, link and copy the mimalloc redirection dll too. diff --git a/bin/minject-arm64.exe b/bin/minject-arm64.exe index cf1ef0ca..9b2b7763 100644 Binary files a/bin/minject-arm64.exe and b/bin/minject-arm64.exe differ diff --git a/bin/minject.exe b/bin/minject.exe index bffe661b..d4dee37d 100644 Binary files a/bin/minject.exe and b/bin/minject.exe differ diff --git a/bin/minject32.exe b/bin/minject32.exe index 47cdf41b..91b5ca92 100644 Binary files a/bin/minject32.exe and b/bin/minject32.exe differ diff --git a/bin/readme.md b/bin/readme.md index bc115ce1..926f25df 100644 --- a/bin/readme.md +++ b/bin/readme.md @@ -1,15 +1,14 @@ # Windows Override -Dynamically overriding on mimalloc on Windows -is robust and has the particular advantage to be able to redirect all malloc/free calls -that go through the (dynamic) C runtime allocator, including those from other DLL's or -libraries. As it intercepts all allocation calls on a low level, it can be used reliably -on large programs that include other 3rd party components. +We use a separate redirection DLL to override mimalloc on Windows +such that we redirect all malloc/free calls that go through the (dynamic) C runtime allocator, +including those from other DLL's or libraries. As it intercepts all allocation calls on a low level, +it can be used reliably on large programs that include other 3rd party components. 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). -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. 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()` diff --git a/contrib/vcpkg/readme.md b/contrib/vcpkg/readme.md index b1f6047c..014f2867 100644 --- a/contrib/vcpkg/readme.md +++ b/contrib/vcpkg/readme.md @@ -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: ```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. diff --git a/contrib/vcpkg/vcpkg-cmake-wrapper.cmake b/contrib/vcpkg/vcpkg-cmake-wrapper.cmake index 1b355722..6b917347 100644 --- a/contrib/vcpkg/vcpkg-cmake-wrapper.cmake +++ b/contrib/vcpkg/vcpkg-cmake-wrapper.cmake @@ -17,4 +17,5 @@ endif() if(TARGET mimalloc-static AND NOT TARGET mimalloc) add_library(mimalloc INTERFACE IMPORTED) set_target_properties(mimalloc PROPERTIES INTERFACE_LINK_LIBRARIES mimalloc-static) + set_target_properties(mimalloc PROPERTIES INTERFACE_INCLUDE_DIRECTORIES mimalloc-static) endif() diff --git a/contrib/vcpkg/vcpkg.json b/contrib/vcpkg/vcpkg.json index 69a72edf..bdbe9ba1 100644 --- a/contrib/vcpkg/vcpkg.json +++ b/contrib/vcpkg/vcpkg.json @@ -19,7 +19,7 @@ "features": { "c": { "description": "Use C11 compilation (this can still override new/delete)" - }, + }, "override": { "description": "Override the standard malloc/free interface" }, @@ -31,6 +31,6 @@ }, "asm": { "description": "Generate assembly files" - } + } } } \ No newline at end of file diff --git a/readme.md b/readme.md index d62942e9..f1dd3965 100644 --- a/readme.md +++ b/readme.md @@ -165,8 +165,8 @@ mimalloc is used in various large scale low-latency services and programs, for e ## Windows Open `ide/vs2022/mimalloc.sln` in Visual Studio 2022 and build. -The `mimalloc` project builds a static library (in `out/msvc-x64`), while the -`mimalloc-override` project builds a DLL for overriding malloc +The `mimalloc-lib` project builds a static library (in `out/msvc-x64`), while the +`mimalloc-override-dll` project builds a DLL for overriding malloc in the entire program. ## Linux, macOS, BSD, etc. @@ -475,16 +475,15 @@ the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-i # Windows Override -Dynamically overriding on mimalloc on Windows -is robust and has the particular advantage to be able to redirect all malloc/free calls -that go through the (dynamic) C runtime allocator, including those from other DLL's or -libraries. As it intercepts all allocation calls on a low level, it can be used reliably -on large programs that include other 3rd party components. +We use a separate redirection DLL to override mimalloc on Windows +such that we redirect all malloc/free calls that go through the (dynamic) C runtime allocator, +including those from other DLL's or libraries. As it intercepts all allocation calls on a low level, +it can be used reliably on large programs that include other 3rd party components. 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). -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). 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()` @@ -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). You can use `minject -l ` to check this if needed. -For best performance on Windows with C++, it -is also recommended to also override the `new`/`delete` operations (by including -[`mimalloc-new-delete.h`](include/mimalloc-new-delete.h) +For best performance on Windows with C++, it is also recommended to also override +the `new`/`delete` operations (by including [`mimalloc-new-delete.h`](include/mimalloc-new-delete.h) a single(!) source file in your project). The environment variable `MIMALLOC_DISABLE_REDIRECT=1` can be used to disable dynamic diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 4c4a013e..8ef0bd72 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -201,7 +201,8 @@ static void* unix_mmap_prim(void* addr, size_t size, size_t try_alignment, int p void* p = NULL; #if defined(MAP_ALIGNED) // BSD 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 p = mmap(addr, size, protect_flags, flags | MAP_ALIGNED(n), fd, 0); if (p==MAP_FAILED || !_mi_is_aligned(p,try_alignment)) {