mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-04 22:49:32 +03:00
rearrange STL allocator code: remove pragma, ifdef for C++11
This commit is contained in:
parent
2d54553b7a
commit
7a9502973d
4 changed files with 25 additions and 35 deletions
|
@ -111,7 +111,7 @@
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
<ClCompile>
|
<ClCompile>
|
||||||
<WarningLevel>Level2</WarningLevel>
|
<WarningLevel>Level4</WarningLevel>
|
||||||
<Optimization>Disabled</Optimization>
|
<Optimization>Disabled</Optimization>
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
|
|
@ -167,6 +167,11 @@ bool _mi_page_is_valid(mi_page_t* page);
|
||||||
|
|
||||||
// Overflow detecting multiply
|
// Overflow detecting multiply
|
||||||
static inline bool mi_mul_overflow(size_t count, size_t size, size_t* total) {
|
static inline bool mi_mul_overflow(size_t count, size_t size, size_t* total) {
|
||||||
|
// quick check for the case where count is one (common for C++ allocators)
|
||||||
|
if (count==1) {
|
||||||
|
*total = size;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
|
#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
|
||||||
#include <limits.h> // UINT_MAX, ULONG_MAX
|
#include <limits.h> // UINT_MAX, ULONG_MAX
|
||||||
#if (SIZE_MAX == UINT_MAX)
|
#if (SIZE_MAX == UINT_MAX)
|
||||||
|
|
|
@ -73,8 +73,6 @@ terms of the MIT license. A copy of the license can be found in the file
|
||||||
#include <stdbool.h> // bool
|
#include <stdbool.h> // bool
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#include <type_traits> // true_type
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -337,41 +335,33 @@ mi_decl_export void* mi_new_aligned_nothrow(size_t n, size_t alignment) mi_attr_
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
// Implement the C++ std::allocator interface for use in STL containers.
|
||||||
|
// (note: see `mimalloc-new-delete.h` for overriding the new/delete operators globally)
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
// ------------------------------------------------------
|
#if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11
|
||||||
// STL allocator - an extension to hook mimalloc into STL
|
#include <type_traits> // true_type
|
||||||
// containers in place of std::allocator.
|
#endif
|
||||||
// ------------------------------------------------------
|
|
||||||
|
|
||||||
#pragma warning(disable: 4100)
|
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
|
||||||
using propagate_on_container_copy_assignment = std::true_type;
|
using propagate_on_container_copy_assignment = std::true_type;
|
||||||
using propagate_on_container_move_assignment = std::true_type;
|
using propagate_on_container_move_assignment = std::true_type;
|
||||||
using propagate_on_container_swap = std::true_type;
|
using propagate_on_container_swap = std::true_type;
|
||||||
using is_always_equal = std::true_type;
|
using is_always_equal = std::true_type;
|
||||||
|
#endif
|
||||||
mi_stl_allocator() noexcept {}
|
mi_stl_allocator() mi_attr_noexcept {}
|
||||||
mi_stl_allocator(const mi_stl_allocator& other) noexcept {}
|
mi_stl_allocator(const mi_stl_allocator& other) mi_attr_noexcept { (void)other; }
|
||||||
template <class U>
|
template<class U> mi_stl_allocator(const mi_stl_allocator<U>& other) mi_attr_noexcept { (void)other; }
|
||||||
mi_stl_allocator(const mi_stl_allocator<U>& other) noexcept {}
|
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); }
|
||||||
T* allocate(size_t n, const void* hint = 0) {
|
|
||||||
return (T*)mi_mallocn(n, sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deallocate(T* p, size_t n) {
|
|
||||||
mi_free(p);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T1, class T2>
|
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; }
|
||||||
bool operator==(const mi_stl_allocator<T1>& lhs, const mi_stl_allocator<T2>& rhs) 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 false; }
|
||||||
template <class T1, class T2>
|
#endif // __cplusplus
|
||||||
bool operator!=(const mi_stl_allocator<T1>& lhs, const mi_stl_allocator<T2>& rhs) noexcept { return false; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -443,12 +443,7 @@ mi_decl_allocator void* mi_calloc(size_t count, size_t size) mi_attr_noexcept {
|
||||||
// Uninitialized `calloc`
|
// Uninitialized `calloc`
|
||||||
extern mi_decl_allocator void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size) mi_attr_noexcept {
|
extern mi_decl_allocator void* mi_heap_mallocn(mi_heap_t* heap, size_t count, size_t size) mi_attr_noexcept {
|
||||||
size_t total;
|
size_t total;
|
||||||
if (count==1) {
|
if (mi_mul_overflow(count, size, &total)) return NULL;
|
||||||
total = size;
|
|
||||||
}
|
|
||||||
else if (mi_mul_overflow(count, size, &total)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return mi_heap_malloc(heap, total);
|
return mi_heap_malloc(heap, total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue