| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/NonCopyable.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class JPH_EXPORT TempAllocator : public NonCopyable |
| 16 |
{ |
| 17 |
public: |
| 18 |
JPH_OVERRIDE_NEW_DELETE |
| 19 |
|
| 20 |
|
| 21 |
virtual ~TempAllocator() = default; |
| 22 |
|
| 23 |
|
| 24 |
virtual void * Allocate(uint inSize) = 0; |
| 25 |
|
| 26 |
|
| 27 |
virtual void Free(void *inAddress, uint inSize) = 0; |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
class JPH_EXPORT TempAllocatorImpl final : public TempAllocator |
| 32 |
{ |
| 33 |
public: |
| 34 |
JPH_OVERRIDE_NEW_DELETE |
| 35 |
|
| 36 |
|
| 37 |
explicit TempAllocatorImpl(size_t inSize) : |
| 38 |
mBase(static_cast<uint8 *>(AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT))), |
| 39 |
mSize(inSize) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
virtual ~TempAllocatorImpl() override |
| 45 |
{ |
| 46 |
JPH_ASSERT(mTop == 0); |
| 47 |
AlignedFree(mBase); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
virtual void * Allocate(uint inSize) override |
| 52 |
{ |
| 53 |
if (inSize == 0) |
| 54 |
{ |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
size_t new_top = mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT); |
| 60 |
if (new_top > mSize) |
| 61 |
{ |
| 62 |
Trace("TempAllocator: Out of memory trying to allocate %u bytes", inSize); |
| 63 |
std::abort(); |
| 64 |
} |
| 65 |
void *address = mBase + mTop; |
| 66 |
mTop = new_top; |
| 67 |
return address; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
virtual void Free(void *inAddress, uint inSize) override |
| 73 |
{ |
| 74 |
if (inAddress == nullptr) |
| 75 |
{ |
| 76 |
JPH_ASSERT(inSize == 0); |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
mTop -= AlignUp(inSize, JPH_RVECTOR_ALIGNMENT); |
| 81 |
if (mBase + mTop != inAddress) |
| 82 |
{ |
| 83 |
Trace("TempAllocator: Freeing in the wrong order"); |
| 84 |
std::abort(); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
bool IsEmpty() const |
| 91 |
{ |
| 92 |
return mTop == 0; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
size_t GetSize() const |
| 97 |
{ |
| 98 |
return mSize; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
size_t GetUsage() const |
| 103 |
{ |
| 104 |
return mTop; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
bool CanAllocate(uint inSize) const |
| 109 |
{ |
| 110 |
return mTop + AlignUp(inSize, JPH_RVECTOR_ALIGNMENT) <= mSize; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
bool OwnsMemory(const void *inAddress) const |
| 115 |
{ |
| 116 |
return inAddress >= mBase && inAddress < mBase + mSize; |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
uint8 * mBase; |
| 121 |
size_t mSize; |
| 122 |
size_t mTop = 0; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
class JPH_EXPORT TempAllocatorMalloc final : public TempAllocator |
| 128 |
{ |
| 129 |
public: |
| 130 |
JPH_OVERRIDE_NEW_DELETE |
| 131 |
|
| 132 |
|
| 133 |
virtual void * Allocate(uint inSize) override |
| 134 |
{ |
| 135 |
return inSize > 0? AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT) : nullptr; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
virtual void Free(void *inAddress, [[maybe_unused]] uint inSize) override |
| 140 |
{ |
| 141 |
if (inAddress != nullptr) |
| 142 |
AlignedFree(inAddress); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
class JPH_EXPORT TempAllocatorImplWithMallocFallback final : public TempAllocator |
| 148 |
{ |
| 149 |
public: |
| 150 |
JPH_OVERRIDE_NEW_DELETE |
| 151 |
|
| 152 |
|
| 153 |
explicit TempAllocatorImplWithMallocFallback(uint inSize) : |
| 154 |
mAllocator(inSize) |
| 155 |
{ |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
virtual void * Allocate(uint inSize) override |
| 160 |
{ |
| 161 |
if (mAllocator.CanAllocate(inSize)) |
| 162 |
return mAllocator.Allocate(inSize); |
| 163 |
else |
| 164 |
return mFallbackAllocator.Allocate(inSize); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
virtual void Free(void *inAddress, uint inSize) override |
| 169 |
{ |
| 170 |
if (inAddress == nullptr) |
| 171 |
{ |
| 172 |
JPH_ASSERT(inSize == 0); |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
if (mAllocator.OwnsMemory(inAddress)) |
| 177 |
mAllocator.Free(inAddress, inSize); |
| 178 |
else |
| 179 |
mFallbackAllocator.Free(inAddress, inSize); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private: |
| 184 |
TempAllocatorImpl mAllocator; |
| 185 |
TempAllocatorMalloc mFallbackAllocator; |
| 186 |
}; |
| 187 |
|
| 188 |
JPH_NAMESPACE_END |