| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
JPH_NAMESPACE_BEGIN |
| 8 |
|
| 9 |
|
| 10 |
template <class T> struct AllocatorHasReallocate { static constexpr bool sValue = false; }; |
| 11 |
|
| 12 |
#ifndef JPH_DISABLE_CUSTOM_ALLOCATOR |
| 13 |
|
| 14 |
|
| 15 |
template <typename T> |
| 16 |
class STLAllocator |
| 17 |
{ |
| 18 |
public: |
| 19 |
using value_type = T; |
| 20 |
|
| 21 |
|
| 22 |
using pointer = T *; |
| 23 |
using const_pointer = const T *; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
using reference = T &; |
| 28 |
using const_reference = const T &; |
| 29 |
|
| 30 |
using size_type = size_t; |
| 31 |
using difference_type = ptrdiff_t; |
| 32 |
|
| 33 |
|
| 34 |
using is_always_equal = std::true_type; |
| 35 |
|
| 36 |
|
| 37 |
using propagate_on_container_move_assignment = std::true_type; |
| 38 |
|
| 39 |
|
| 40 |
inline STLAllocator() = default; |
| 41 |
|
| 42 |
|
| 43 |
template <typename T2> |
| 44 |
inline STLAllocator(const STLAllocator<T2> &) { } |
| 45 |
|
| 46 |
|
| 47 |
static constexpr bool needs_aligned_allocate = alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16); |
| 48 |
|
| 49 |
|
| 50 |
inline pointer allocate(size_type inN) |
| 51 |
{ |
| 52 |
if constexpr (needs_aligned_allocate) |
| 53 |
return pointer(AlignedAllocate(inN * sizeof(value_type), alignof(T))); |
| 54 |
else |
| 55 |
return pointer(Allocate(inN * sizeof(value_type))); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
static constexpr bool has_reallocate = std::is_trivially_copyable<T>() && !needs_aligned_allocate; |
| 60 |
|
| 61 |
|
| 62 |
template <bool has_reallocate_v = has_reallocate, typename = std::enable_if_t<has_reallocate_v>> |
| 63 |
inline pointer reallocate(pointer inOldPointer, size_type inOldSize, size_type inNewSize) |
| 64 |
{ |
| 65 |
JPH_ASSERT(inNewSize > 0); |
| 66 |
return pointer(Reallocate(inOldPointer, inOldSize * sizeof(value_type), inNewSize * sizeof(value_type))); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
inline void deallocate(pointer inPointer, size_type) |
| 71 |
{ |
| 72 |
if constexpr (needs_aligned_allocate) |
| 73 |
AlignedFree(inPointer); |
| 74 |
else |
| 75 |
Free(inPointer); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
inline bool operator == (const STLAllocator<T> &) const |
| 80 |
{ |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
inline bool operator != (const STLAllocator<T> &) const |
| 85 |
{ |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
template <typename T2> |
| 91 |
struct rebind |
| 92 |
{ |
| 93 |
using other = STLAllocator<T2>; |
| 94 |
}; |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
template <class T> struct AllocatorHasReallocate<STLAllocator<T>> { static constexpr bool sValue = STLAllocator<T>::has_reallocate; }; |
| 99 |
|
| 100 |
#else |
| 101 |
|
| 102 |
template <typename T> using STLAllocator = std::allocator<T>; |
| 103 |
|
| 104 |
#endif |
| 105 |
|
| 106 |
|
| 107 |
using String = std::basic_string<char, std::char_traits<char>, STLAllocator<char>>; |
| 108 |
using IStringStream = std::basic_istringstream<char, std::char_traits<char>, STLAllocator<char>>; |
| 109 |
|
| 110 |
JPH_NAMESPACE_END |
| 111 |
|
| 112 |
#if (!defined(JPH_PLATFORM_WINDOWS) || defined(JPH_COMPILER_MINGW)) && !defined(JPH_DISABLE_CUSTOM_ALLOCATOR) |
| 113 |
|
| 114 |
namespace std |
| 115 |
{ |
| 116 |
|
| 117 |
template <> |
| 118 |
struct hash<JPH::String> |
| 119 |
{ |
| 120 |
inline size_t operator () (const JPH::String &inRHS) const |
| 121 |
{ |
| 122 |
return hash<string_view> { } (inRHS); |
| 123 |
} |
| 124 |
}; |
| 125 |
} |
| 126 |
|
| 127 |
#endif |