From 7da4a34dc1d101310ccfab3acfb76388847f9fc3 Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Mon, 28 Nov 2022 11:55:58 +0100 Subject: [PATCH 1/3] Make "destroy" a compile-time constant + fix const allocator comparisons --- include/mimalloc.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/include/mimalloc.h b/include/mimalloc.h index d70d28ed..81d387c3 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -471,7 +471,7 @@ template bool operator!=(const mi_stl_allocator& , const #include // std::shared_ptr // Common base class for STL allocators in a specific heap -template struct _mi_heap_stl_allocator_common : public _mi_stl_allocator_common { +template struct _mi_heap_stl_allocator_common : public _mi_stl_allocator_common { using typename _mi_stl_allocator_common::size_type; using typename _mi_stl_allocator_common::value_type; using typename _mi_stl_allocator_common::pointer; @@ -490,18 +490,18 @@ template struct _mi_heap_stl_allocator_common : public _mi_stl_allocato #endif void collect(bool force) { mi_heap_collect(this->heap.get(), force); } - template bool is_equal(const _mi_heap_stl_allocator_common& x) { return (this->heap == x.heap); } + template bool is_equal(const _mi_heap_stl_allocator_common& x) const { return (this->heap == x.heap); } protected: std::shared_ptr heap; - template friend struct _mi_heap_stl_allocator_common; + template friend struct _mi_heap_stl_allocator_common; - _mi_heap_stl_allocator_common(bool destroy) { + _mi_heap_stl_allocator_common() { mi_heap_t* hp = mi_heap_new(); this->heap.reset(hp, (destroy ? &heap_destroy : &heap_delete)); /* calls heap_delete/destroy when the refcount drops to zero */ } _mi_heap_stl_allocator_common(const _mi_heap_stl_allocator_common& x) mi_attr_noexcept : heap(x.heap) { } - template _mi_heap_stl_allocator_common(const _mi_heap_stl_allocator_common& x) mi_attr_noexcept : heap(x.heap) { } + template _mi_heap_stl_allocator_common(const _mi_heap_stl_allocator_common& x) mi_attr_noexcept : heap(x.heap) { } private: static void heap_delete(mi_heap_t* hp) { if (hp != NULL) { mi_heap_delete(hp); } } @@ -509,11 +509,10 @@ private: }; // STL allocator allocation in a specific heap -template struct mi_heap_stl_allocator : public _mi_heap_stl_allocator_common { - using typename _mi_heap_stl_allocator_common::size_type; - mi_heap_stl_allocator() : _mi_heap_stl_allocator_common(false) { } /* delete on destruction */ - mi_heap_stl_allocator(mi_heap_t* hp) : _mi_heap_stl_allocator_common(hp) { } /* no delete or destroy on the passed in heap */ - template mi_heap_stl_allocator(const mi_heap_stl_allocator& x) mi_attr_noexcept : _mi_heap_stl_allocator_common(x) { } +template struct mi_heap_stl_allocator : public _mi_heap_stl_allocator_common { + using typename _mi_heap_stl_allocator_common::size_type; + mi_heap_stl_allocator(mi_heap_t* hp) : _mi_heap_stl_allocator_common(hp) { } /* no delete or destroy on the passed in heap */ + template mi_heap_stl_allocator(const mi_heap_stl_allocator& x) mi_attr_noexcept : _mi_heap_stl_allocator_common(x) { } mi_heap_stl_allocator select_on_container_copy_construction() const { return *this; } void deallocate(T* p, size_type) { mi_free(p); } @@ -526,10 +525,9 @@ template bool operator!=(const mi_heap_stl_allocator& x, // STL allocator allocation in a specific heap, where `free` does nothing and // the heap is destroyed in one go on destruction -- use with care! -template struct mi_heap_destroy_stl_allocator : public _mi_heap_stl_allocator_common { - using typename _mi_heap_stl_allocator_common::size_type; - mi_heap_destroy_stl_allocator() : _mi_heap_stl_allocator_common(true) { } /* destroy on destruction */ - template mi_heap_destroy_stl_allocator(const mi_heap_destroy_stl_allocator& x) mi_attr_noexcept : _mi_heap_stl_allocator_common(x) { } +template struct mi_heap_destroy_stl_allocator : public _mi_heap_stl_allocator_common { + using typename _mi_heap_stl_allocator_common::size_type; + template mi_heap_destroy_stl_allocator(const mi_heap_destroy_stl_allocator& x) mi_attr_noexcept : _mi_heap_stl_allocator_common(x) { } mi_heap_destroy_stl_allocator select_on_container_copy_construction() const { return *this; } void deallocate(T* p, size_type) { /* do nothing as we destroy the heap on destruct. */ } From 8c89a77064d68c7a534604ef98f39fff9a8239d6 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 19 Dec 2022 17:25:49 -0800 Subject: [PATCH 2/3] add default constructors to stl heap allocators --- include/mimalloc.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/mimalloc.h b/include/mimalloc.h index a064645f..bce3de93 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -478,7 +478,7 @@ template struct _mi_heap_stl_allocator_common : public _m 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 or destroy the passed in heap */ + _mi_heap_stl_allocator_common(mi_heap_t* hp) : heap(hp) { } /* 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))); } @@ -513,7 +513,8 @@ private: // STL allocator allocation in a specific heap template struct mi_heap_stl_allocator : public _mi_heap_stl_allocator_common { using typename _mi_heap_stl_allocator_common::size_type; - mi_heap_stl_allocator(mi_heap_t* hp) : _mi_heap_stl_allocator_common(hp) { } /* no delete or destroy on the passed in heap */ + mi_heap_stl_allocator() : _mi_heap_stl_allocator_common() { } // creates fresh heap that is deleted when the destructor is called + mi_heap_stl_allocator(mi_heap_t* hp) : _mi_heap_stl_allocator_common(hp) { } // no delete nor destroy on the passed in heap template mi_heap_stl_allocator(const mi_heap_stl_allocator& x) mi_attr_noexcept : _mi_heap_stl_allocator_common(x) { } mi_heap_stl_allocator select_on_container_copy_construction() const { return *this; } @@ -529,6 +530,8 @@ template bool operator!=(const mi_heap_stl_allocator& x, // the heap is destroyed in one go on destruction -- use with care! template struct mi_heap_destroy_stl_allocator : public _mi_heap_stl_allocator_common { using typename _mi_heap_stl_allocator_common::size_type; + mi_heap_destroy_stl_allocator() : _mi_heap_stl_allocator_common() { } // creates fresh heap that is destroyed when the destructor is called + mi_heap_destroy_stl_allocator(mi_heap_t* hp) : _mi_heap_stl_allocator_common(hp) { } // no delete nor destroy on the passed in heap template mi_heap_destroy_stl_allocator(const mi_heap_destroy_stl_allocator& x) mi_attr_noexcept : _mi_heap_stl_allocator_common(x) { } mi_heap_destroy_stl_allocator select_on_container_copy_construction() const { return *this; } From 2b0421a25cbb03c2ce015cb460d04f4766c99219 Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 19 Dec 2022 17:38:39 -0800 Subject: [PATCH 3/3] fix -Wunused-function for mi_strn-cmp; originally by @rui314 in PR #662 but rebased to dev branch --- src/options.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/options.c b/src/options.c index 669f57d0..44319a42 100644 --- a/src/options.c +++ b/src/options.c @@ -494,13 +494,6 @@ static bool mi_getenv(const char* name, char* result, size_t result_size) { return false; } #else -static inline int mi_strnicmp(const char* s, const char* t, size_t n) { - if (n==0) return 0; - for (; *s != 0 && *t != 0 && n > 0; s++, t++, n--) { - if (toupper(*s) != toupper(*t)) break; - } - return (n==0 ? 0 : *s - *t); -} #if defined _WIN32 // On Windows use GetEnvironmentVariable instead of getenv to work // reliably even when this is invoked before the C runtime is initialized. @@ -526,6 +519,13 @@ static char** mi_get_environ(void) { return environ; } #endif +static int mi_strnicmp(const char* s, const char* t, size_t n) { + if (n == 0) return 0; + for (; *s != 0 && *t != 0 && n > 0; s++, t++, n--) { + if (toupper(*s) != toupper(*t)) break; + } + return (n == 0 ? 0 : *s - *t); +} static bool mi_getenv(const char* name, char* result, size_t result_size) { if (name==NULL) return false; const size_t len = strlen(name);