| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/STLAllocator.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
#ifndef JPH_DISABLE_CUSTOM_ALLOCATOR |
| 12 |
|
| 13 |
|
| 14 |
template <typename T, size_t N> |
| 15 |
class STLLocalAllocator : private STLAllocator<T> |
| 16 |
{ |
| 17 |
using Base = STLAllocator<T>; |
| 18 |
|
| 19 |
public: |
| 20 |
|
| 21 |
using value_type = T; |
| 22 |
using pointer = T *; |
| 23 |
using const_pointer = const T *; |
| 24 |
using reference = T &; |
| 25 |
using const_reference = const T &; |
| 26 |
using size_type = size_t; |
| 27 |
using difference_type = ptrdiff_t; |
| 28 |
|
| 29 |
|
| 30 |
using is_always_equal = std::false_type; |
| 31 |
|
| 32 |
|
| 33 |
using propagate_on_container_copy_assignment = std::false_type; |
| 34 |
using propagate_on_container_move_assignment = std::false_type; |
| 35 |
using propagate_on_container_swap = std::false_type; |
| 36 |
|
| 37 |
|
| 38 |
STLLocalAllocator() = default; |
| 39 |
STLLocalAllocator(const STLLocalAllocator &) = delete; |
| 40 |
STLLocalAllocator(STLLocalAllocator &&) = delete; |
| 41 |
STLLocalAllocator & operator = (const STLLocalAllocator &) = delete; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
template <class T2> |
| 47 |
explicit STLLocalAllocator(const STLLocalAllocator<T2, N> &) : mNumElementsUsed(N) { } |
| 48 |
|
| 49 |
|
| 50 |
inline bool is_local(const_pointer inPointer) const |
| 51 |
{ |
| 52 |
ptrdiff_t diff = inPointer - reinterpret_cast<const_pointer>(mElements); |
| 53 |
return diff >= 0 && diff < ptrdiff_t(N); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
inline pointer allocate(size_type inN) |
| 58 |
{ |
| 59 |
|
| 60 |
if (mNumElementsUsed + inN > N) |
| 61 |
return Base::allocate(inN); |
| 62 |
|
| 63 |
|
| 64 |
pointer result = reinterpret_cast<pointer>(mElements) + mNumElementsUsed; |
| 65 |
mNumElementsUsed += inN; |
| 66 |
return result; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static constexpr bool has_reallocate = true; |
| 71 |
|
| 72 |
|
| 73 |
inline pointer reallocate(pointer inOldPointer, size_type inOldSize, size_type inNewSize) |
| 74 |
{ |
| 75 |
JPH_ASSERT(inNewSize > 0); |
| 76 |
|
| 77 |
|
| 78 |
if (inOldPointer == nullptr) |
| 79 |
return allocate(inNewSize); |
| 80 |
|
| 81 |
|
| 82 |
if (!is_local(inOldPointer)) |
| 83 |
{ |
| 84 |
if constexpr (AllocatorHasReallocate<Base>::sValue) |
| 85 |
return Base::reallocate(inOldPointer, inOldSize, inNewSize); |
| 86 |
else |
| 87 |
return ReallocateImpl(inOldPointer, inOldSize, inNewSize); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
pointer base_ptr = reinterpret_cast<pointer>(mElements) + mNumElementsUsed - inOldSize; |
| 92 |
if (inOldPointer == base_ptr |
| 93 |
&& mNumElementsUsed - inOldSize + inNewSize <= N) |
| 94 |
{ |
| 95 |
mNumElementsUsed += inNewSize - inOldSize; |
| 96 |
return base_ptr; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
return ReallocateImpl(inOldPointer, inOldSize, inNewSize); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
inline void deallocate(pointer inPointer, size_type inN) |
| 105 |
{ |
| 106 |
|
| 107 |
if (!is_local(inPointer)) |
| 108 |
return Base::deallocate(inPointer, inN); |
| 109 |
|
| 110 |
|
| 111 |
if (inPointer == reinterpret_cast<pointer>(mElements) + mNumElementsUsed - inN) |
| 112 |
mNumElementsUsed -= inN; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
inline bool operator == (const STLLocalAllocator<T, N> &inRHS) const |
| 117 |
{ |
| 118 |
return this == &inRHS; |
| 119 |
} |
| 120 |
|
| 121 |
inline bool operator != (const STLLocalAllocator<T, N> &inRHS) const |
| 122 |
{ |
| 123 |
return this != &inRHS; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
template <typename T2> |
| 128 |
struct rebind |
| 129 |
{ |
| 130 |
using other = STLLocalAllocator<T2, N>; |
| 131 |
}; |
| 132 |
|
| 133 |
private: |
| 134 |
|
| 135 |
inline pointer ReallocateImpl(pointer inOldPointer, size_type inOldSize, size_type inNewSize) |
| 136 |
{ |
| 137 |
pointer new_pointer = Base::allocate(inNewSize); |
| 138 |
size_type n = min(inOldSize, inNewSize); |
| 139 |
if constexpr (std::is_trivially_copyable<T>()) |
| 140 |
{ |
| 141 |
|
| 142 |
memcpy(new_pointer, inOldPointer, n * sizeof(T)); |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
|
| 147 |
for (size_t i = 0; i < n; ++i) |
| 148 |
{ |
| 149 |
new (new_pointer + i) T(std::move(inOldPointer[i])); |
| 150 |
inOldPointer[i].~T(); |
| 151 |
} |
| 152 |
} |
| 153 |
deallocate(inOldPointer, inOldSize); |
| 154 |
return new_pointer; |
| 155 |
} |
| 156 |
|
| 157 |
alignas(T) uint8 mElements[N * sizeof(T)]; |
| 158 |
size_type mNumElementsUsed = 0; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
template <class T, size_t N> struct AllocatorHasReallocate<STLLocalAllocator<T, N>> { static constexpr bool sValue = STLLocalAllocator<T, N>::has_reallocate; }; |
| 163 |
|
| 164 |
#else |
| 165 |
|
| 166 |
template <typename T, size_t N> using STLLocalAllocator = std::allocator<T>; |
| 167 |
|
| 168 |
#endif |
| 169 |
|
| 170 |
JPH_NAMESPACE_END |