From bc10fe27c657d72fb26592f78d31e9d763165438 Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 15 Jan 2025 11:37:20 -0800 Subject: [PATCH 1/6] fix unregister from the page-map --- src/page-map.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/page-map.c b/src/page-map.c index 1cf0b07b..25f8a7ec 100644 --- a/src/page-map.c +++ b/src/page-map.c @@ -296,12 +296,16 @@ void _mi_page_map_register(mi_page_t* page) { void _mi_page_map_unregister(mi_page_t* page) { mi_assert_internal(_mi_page_map != NULL); + mi_assert_internal(page != NULL); + mi_assert_internal(_mi_is_aligned(page, MI_PAGE_ALIGN)); + mi_assert_internal(_mi_page_map != NULL); + if mi_unlikely(_mi_page_map == NULL) return; // get index and count size_t slice_count; size_t sub_idx; const size_t idx = mi_page_map_get_idx(page, &sub_idx, &slice_count); // unset the offsets - mi_page_map_set_range(page, idx, sub_idx, slice_count); + mi_page_map_set_range(NULL, idx, sub_idx, slice_count); } void _mi_page_map_unregister_range(void* start, size_t size) { From be2cb44de44b13c5905886a16c7b46c498125321 Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 15 Jan 2025 12:02:34 -0800 Subject: [PATCH 2/6] fix NULL pointer in _mi_safe_ptr_page to return a reference to the empty page --- src/page-map.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/page-map.c b/src/page-map.c index 25f8a7ec..641ab405 100644 --- a/src/page-map.c +++ b/src/page-map.c @@ -305,7 +305,7 @@ void _mi_page_map_unregister(mi_page_t* page) { size_t sub_idx; const size_t idx = mi_page_map_get_idx(page, &sub_idx, &slice_count); // unset the offsets - mi_page_map_set_range(NULL, idx, sub_idx, slice_count); + // mi_page_map_set_range(NULL, idx, sub_idx, slice_count); } void _mi_page_map_unregister_range(void* start, size_t size) { @@ -318,6 +318,7 @@ void _mi_page_map_unregister_range(void* start, size_t size) { mi_page_t* _mi_safe_ptr_page(const void* p) { if mi_unlikely(p >= mi_page_map_max_address) return NULL; + if (p == NULL) return (mi_page_t*)&_mi_page_empty; // to match mi_free expectation size_t sub_idx; const size_t idx = _mi_page_map_index(p,&sub_idx); if mi_unlikely(!mi_page_map_is_committed(idx,NULL)) return NULL; From 5af1eb1144bf4777495f76bfff435443e8302e7f Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 15 Jan 2025 12:07:06 -0800 Subject: [PATCH 3/6] fix NULL pointer in _mi_safe_ptr_page to return a reference to the empty page --- src/page-map.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/page-map.c b/src/page-map.c index 641ab405..be99814c 100644 --- a/src/page-map.c +++ b/src/page-map.c @@ -315,10 +315,10 @@ void _mi_page_map_unregister_range(void* start, size_t size) { mi_page_map_set_range(NULL, idx, sub_idx, slice_count); // todo: avoid committing if not already committed? } - +// Return the empty page for the NULL pointer to match the behaviour of `_mi_ptr_page` mi_page_t* _mi_safe_ptr_page(const void* p) { if mi_unlikely(p >= mi_page_map_max_address) return NULL; - if (p == NULL) return (mi_page_t*)&_mi_page_empty; // to match mi_free expectation + if (p == NULL) return (mi_page_t*)&_mi_page_empty; // to match `_mi_ptr_page` (see `mi_free` as well) size_t sub_idx; const size_t idx = _mi_page_map_index(p,&sub_idx); if mi_unlikely(!mi_page_map_is_committed(idx,NULL)) return NULL; @@ -328,7 +328,7 @@ mi_page_t* _mi_safe_ptr_page(const void* p) { } mi_decl_nodiscard mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept { - return (_mi_safe_ptr_page(p) != NULL); + return (p != NULL && _mi_safe_ptr_page(p) != NULL); } #endif From 4a14c69554f7d07cc72d8b99f4322990c9cd2696 Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 15 Jan 2025 12:11:52 -0800 Subject: [PATCH 4/6] disable using vcpkg in the VS projects --- ide/vs2022/mimalloc-lib.vcxproj | 3 +++ ide/vs2022/mimalloc-override-dll.vcxproj | 3 +++ ide/vs2022/mimalloc-override-test-dep.vcxproj | 3 +++ ide/vs2022/mimalloc-override-test.vcxproj | 3 +++ ide/vs2022/mimalloc-test-api.vcxproj | 3 +++ ide/vs2022/mimalloc-test-stress.vcxproj | 3 +++ ide/vs2022/mimalloc-test.vcxproj | 3 +++ 7 files changed, 21 insertions(+) diff --git a/ide/vs2022/mimalloc-lib.vcxproj b/ide/vs2022/mimalloc-lib.vcxproj index 3359e6c0..c6c2cb5f 100644 --- a/ide/vs2022/mimalloc-lib.vcxproj +++ b/ide/vs2022/mimalloc-lib.vcxproj @@ -164,6 +164,9 @@ .lib mimalloc + + false + Level4 diff --git a/ide/vs2022/mimalloc-override-dll.vcxproj b/ide/vs2022/mimalloc-override-dll.vcxproj index d80d0b02..133b3efa 100644 --- a/ide/vs2022/mimalloc-override-dll.vcxproj +++ b/ide/vs2022/mimalloc-override-dll.vcxproj @@ -160,6 +160,9 @@ .dll mimalloc + + false + Level3 diff --git a/ide/vs2022/mimalloc-override-test-dep.vcxproj b/ide/vs2022/mimalloc-override-test-dep.vcxproj index 606bec9c..c1b89690 100644 --- a/ide/vs2022/mimalloc-override-test-dep.vcxproj +++ b/ide/vs2022/mimalloc-override-test-dep.vcxproj @@ -148,6 +148,9 @@ $(ProjectDir)..\..\out\msvc-$(Platform)\$(Configuration)\ $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + false + Level3 diff --git a/ide/vs2022/mimalloc-override-test.vcxproj b/ide/vs2022/mimalloc-override-test.vcxproj index ae465349..1dc2cee7 100644 --- a/ide/vs2022/mimalloc-override-test.vcxproj +++ b/ide/vs2022/mimalloc-override-test.vcxproj @@ -148,6 +148,9 @@ $(ProjectDir)..\..\out\msvc-$(Platform)\$(Configuration)\ $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + false + Level3 diff --git a/ide/vs2022/mimalloc-test-api.vcxproj b/ide/vs2022/mimalloc-test-api.vcxproj index b7f97ad2..440693a2 100644 --- a/ide/vs2022/mimalloc-test-api.vcxproj +++ b/ide/vs2022/mimalloc-test-api.vcxproj @@ -148,6 +148,9 @@ $(ProjectDir)..\..\out\msvc-$(Platform)\$(Configuration)\ $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + false + Level3 diff --git a/ide/vs2022/mimalloc-test-stress.vcxproj b/ide/vs2022/mimalloc-test-stress.vcxproj index cb761f94..d6af71ce 100644 --- a/ide/vs2022/mimalloc-test-stress.vcxproj +++ b/ide/vs2022/mimalloc-test-stress.vcxproj @@ -148,6 +148,9 @@ $(ProjectDir)..\..\out\msvc-$(Platform)\$(Configuration)\ $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + false + Level3 diff --git a/ide/vs2022/mimalloc-test.vcxproj b/ide/vs2022/mimalloc-test.vcxproj index 83202dbe..1e41fca1 100644 --- a/ide/vs2022/mimalloc-test.vcxproj +++ b/ide/vs2022/mimalloc-test.vcxproj @@ -148,6 +148,9 @@ $(ProjectDir)..\..\out\msvc-$(Platform)\$(Configuration)\ $(ProjectDir)..\..\out\msvc-$(Platform)\$(ProjectName)\$(Configuration)\ + + false + Level3 From 2bc52557c59cb67fdb53f9d10600c059b4818d19 Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 15 Jan 2025 12:31:35 -0800 Subject: [PATCH 5/6] add figure to readme for mimalloc-redirect --- bin/readme.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bin/readme.md b/bin/readme.md index 926f25df..2815f168 100644 --- a/bin/readme.md +++ b/bin/readme.md @@ -25,6 +25,23 @@ 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. +```cpp +┌──────────────┐ +│ Your Program │ +└────┬─────────┘ + │ + │ mi_version() ┌───────────────┐ ┌───────────────────────┐ + ├──────────────►│ mimalloc.dll ├────►│ mimalloc-redirect.dll │ + │ └──────┬────────┘ └───────────────────────┘ + │ ▼ + │ malloc() etc. ┌──────────────┐ + ├──────────────►│ ucrtbase.dll │ + │ └──────────────┘ + │ + │ + └──────────────► ... +``` + 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) From 14b4f674fa2cff00f28333365fe07ce916575c30 Mon Sep 17 00:00:00 2001 From: daanx Date: Wed, 15 Jan 2025 12:33:11 -0800 Subject: [PATCH 6/6] nicer figure --- bin/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/readme.md b/bin/readme.md index 2815f168..f08b2e87 100644 --- a/bin/readme.md +++ b/bin/readme.md @@ -25,7 +25,7 @@ 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. -```cpp +```csharp ┌──────────────┐ │ Your Program │ └────┬─────────┘