C++11 conforming STL allocator

This commit is contained in:
__vic 2020-01-17 11:30:01 +03:00
parent dc94d25890
commit edcbc6afb2
2 changed files with 16 additions and 22 deletions

View file

@ -332,36 +332,25 @@ mi_decl_export void* mi_new_nothrow(size_t n) mi_attr_malloc mi_attr_alloc_size(
mi_decl_export void* mi_new_aligned_nothrow(size_t n, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1); mi_decl_export void* mi_new_aligned_nothrow(size_t n, size_t alignment) mi_attr_malloc mi_attr_alloc_size(1);
#ifdef __cplusplus #ifdef __cplusplus
void* mi_new_n(size_t count, size_t size) mi_attr_malloc mi_attr_alloc_size2(1,2);
} }
#endif #endif
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
// Implement the C++ std::allocator interface for use in STL containers. // Implement the C++11 std::allocator interface for use in STL containers.
// (note: see `mimalloc-new-delete.h` for overriding the new/delete operators globally) // (note: see `mimalloc-new-delete.h` for overriding the new/delete operators globally)
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
#ifdef __cplusplus #if defined(__cplusplus) && (__cplusplus >= 201103L || _MSC_VER > 1900) // C++11
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11
#include <type_traits> // true_type
#endif
template<class T> struct mi_stl_allocator { template<class T> struct mi_stl_allocator {
typedef T value_type; typedef T value_type;
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11 mi_stl_allocator() = default;
using propagate_on_container_copy_assignment = std::true_type; template<class U> mi_stl_allocator(const mi_stl_allocator<U>&) mi_attr_noexcept {}
using propagate_on_container_move_assignment = std::true_type; T* allocate(size_t n) { return static_cast<T*>(mi_new_n(n, sizeof(T))); }
using propagate_on_container_swap = std::true_type; void deallocate(T* p, size_t n) { mi_free_size(p,n); }
using is_always_equal = std::true_type;
#endif
mi_stl_allocator() mi_attr_noexcept {}
mi_stl_allocator(const mi_stl_allocator& other) mi_attr_noexcept { (void)other; }
template<class U> mi_stl_allocator(const mi_stl_allocator<U>& other) mi_attr_noexcept { (void)other; }
T* allocate(size_t n, const void* hint = 0) { (void)hint; return (T*)mi_mallocn(n, sizeof(T)); }
void deallocate(T* p, size_t n) { mi_free_size(p,n); }
}; };
template<class T1,class T2> bool operator==(const mi_stl_allocator<T1>&, const mi_stl_allocator<T2>&) mi_attr_noexcept { return true; }
template<class T1,class T2> bool operator==(const mi_stl_allocator<T1>& lhs, const mi_stl_allocator<T2>& rhs) mi_attr_noexcept { (void)lhs; (void)rhs; return true; } template<class T1,class T2> bool operator!=(const mi_stl_allocator<T1>&, const mi_stl_allocator<T2>&) mi_attr_noexcept { return false; }
template<class T1,class T2> bool operator!=(const mi_stl_allocator<T1>& lhs, const mi_stl_allocator<T2>& rhs) mi_attr_noexcept { (void)lhs; (void)rhs; return false; } #endif // C++11
#endif // __cplusplus
#endif #endif

View file

@ -644,6 +644,11 @@ static bool mi_try_new_handler(bool nothrow) {
return true; return true;
} }
} }
void* mi_new_n(size_t count, size_t size) {
size_t total;
if (mi_mul_overflow(count, size, &total)) throw std::bad_alloc();
return mi_new(total);
}
#else #else
#include <errno.h> #include <errno.h>
#ifndef ENOMEM #ifndef ENOMEM