From 068dc014ec5f2bc32133e986a273b69e0c8a936a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodor=20Sp=C3=A6ren?= Date: Sat, 9 Mar 2024 14:13:33 +0100 Subject: [PATCH 1/3] Avoid compilation error when passing in heap to allocators Before it would not work to create the mi_heap_stl_allocator types with passing in a "mi_heap_t*", since sizeof is used and it gives a compilation error. This change fixes that. --- include/mimalloc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/mimalloc.h b/include/mimalloc.h index e6693899..dfef2c69 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -487,6 +487,7 @@ template bool operator!=(const mi_stl_allocator& , const #define MI_HAS_HEAP_STL_ALLOCATOR 1 #include // std::shared_ptr +#include "mimalloc/types.h" // Common base class for STL allocators in a specific heap template struct _mi_heap_stl_allocator_common : public _mi_stl_allocator_common { From 3d89f6388e4b416a1ae588ef3fa28cdc54f67bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodor=20Sp=C3=A6ren?= Date: Sat, 9 Mar 2024 15:14:32 +0100 Subject: [PATCH 2/3] Fix std::shared_pointer calling free on provided heap pointers --- include/mimalloc.h | 2 +- test/test-api.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/include/mimalloc.h b/include/mimalloc.h index dfef2c69..c125932f 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -495,7 +495,7 @@ template struct _mi_heap_stl_allocator_common : publi using typename _mi_stl_allocator_common::value_type; using typename _mi_stl_allocator_common::pointer; - _mi_heap_stl_allocator_common(mi_heap_t* hp) : heap(hp) { } /* will not delete nor destroy the passed in heap */ + _mi_heap_stl_allocator_common(mi_heap_t* hp) : heap(hp, [](mi_heap_t*) {}) {} /* will not delete nor destroy the passed in heap */ #if (__cplusplus >= 201703L) // C++17 mi_decl_nodiscard T* allocate(size_type count) { return static_cast(mi_heap_alloc_new_n(this->heap.get(), count, sizeof(T))); } diff --git a/test/test-api.c b/test/test-api.c index 8dd24e1b..edc506b3 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -46,6 +46,11 @@ bool test_heap2(void); bool test_stl_allocator1(void); bool test_stl_allocator2(void); +bool test_stl_heap_allocator1(void); +bool test_stl_heap_allocator2(void); +bool test_stl_heap_allocator3(void); +bool test_stl_heap_allocator4(void); + bool mem_is_zero(uint8_t* p, size_t size) { if (p==NULL) return false; for (size_t i = 0; i < size; ++i) { @@ -304,6 +309,11 @@ int main(void) { CHECK("stl_allocator1", test_stl_allocator1()); CHECK("stl_allocator2", test_stl_allocator2()); + CHECK("stl_heap_allocator1", test_stl_heap_allocator1()); + CHECK("stl_heap_allocator2", test_stl_heap_allocator2()); + CHECK("stl_heap_allocator3", test_stl_heap_allocator3()); + CHECK("stl_heap_allocator4", test_stl_heap_allocator4()); + // --------------------------------------------------- // Done // ---------------------------------------------------[] @@ -357,3 +367,61 @@ bool test_stl_allocator2(void) { return true; #endif } + +bool test_stl_heap_allocator1(void) { +#ifdef __cplusplus + std::vector > vec; + vec.push_back(some_struct()); + vec.pop_back(); + return vec.size() == 0; +#else + return true; +#endif +} + +bool test_stl_heap_allocator2(void) { +#ifdef __cplusplus + std::vector > vec; + vec.push_back(some_struct()); + vec.pop_back(); + return vec.size() == 0; +#else + return true; +#endif +} + +bool test_stl_heap_allocator3(void) { +#ifdef __cplusplus + mi_heap_t* heap = mi_heap_new(); + bool good = false; + { + mi_heap_stl_allocator myAlloc(heap); + std::vector > vec(myAlloc); + vec.push_back(some_struct()); + vec.pop_back(); + good = vec.size() == 0; + } + mi_heap_delete(heap); + return good; +#else + return true; +#endif +} + +bool test_stl_heap_allocator4(void) { +#ifdef __cplusplus + mi_heap_t* heap = mi_heap_new(); + bool good = false; + { + mi_heap_destroy_stl_allocator myAlloc(heap); + std::vector > vec(myAlloc); + vec.push_back(some_struct()); + vec.pop_back(); + good = vec.size() == 0; + } + mi_heap_destroy(heap); + return good; +#else + return true; +#endif +} From 10721ddbfd5827cca9a81a06ab6fccdd33a8a0f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodor=20Sp=C3=A6ren?= Date: Sun, 31 Mar 2024 23:18:52 +0200 Subject: [PATCH 3/3] Remove unneeded include --- include/mimalloc.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/mimalloc.h b/include/mimalloc.h index c125932f..b3f60a34 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -487,7 +487,6 @@ template bool operator!=(const mi_stl_allocator& , const #define MI_HAS_HEAP_STL_ALLOCATOR 1 #include // std::shared_ptr -#include "mimalloc/types.h" // Common base class for STL allocators in a specific heap template struct _mi_heap_stl_allocator_common : public _mi_stl_allocator_common {