7 namespace CompactingStore
12 class BSSmallBlockAllocator;
30 [[nodiscard]]
void*
Allocate(std::size_t a_size, std::int32_t a_alignment,
bool a_alignmentRequired);
33 [[nodiscard]]
void*
Reallocate(
void* a_oldMem, std::size_t a_newSize, std::int32_t a_alignment,
bool a_aligned);
61 static_assert(
sizeof(MemoryManager) == 0x480);
63 void*
malloc(std::size_t a_size);
68 return static_cast<T*
>(
malloc(a_size));
74 return malloc<T>(
sizeof(T));
77 void*
aligned_alloc(std::size_t a_alignment, std::size_t a_size);
88 return aligned_alloc<T>(
alignof(T),
sizeof(T));
91 void*
calloc(std::size_t a_num, std::size_t a_size);
94 inline T*
calloc(std::size_t a_num, std::size_t a_size)
96 return static_cast<T*
>(
calloc(a_num, a_size));
102 return calloc<T>(a_num,
sizeof(T));
105 void*
realloc(
void* a_ptr, std::size_t a_newSize);
108 inline T*
realloc(
void* a_ptr, std::size_t a_newSize)
110 return static_cast<T*
>(
realloc(a_ptr, a_newSize));
113 void*
aligned_realloc(
void* a_ptr, std::size_t a_newSize, std::size_t a_alignment);
116 inline T*
aligned_realloc(
void* a_ptr, std::size_t a_newSize, std::size_t a_alignment)
118 return static_cast<T*
>(
aligned_realloc(a_ptr, a_newSize, a_alignment));
126 #define TES_HEAP_REDEFINE_NEW() \
127 [[nodiscard]] inline void* operator new(std::size_t a_count) \
129 const auto mem = RE::malloc(a_count); \
133 stl::report_and_fail("out of memory"sv); \
137 [[nodiscard]] inline void* operator new[](std::size_t a_count) \
139 const auto mem = RE::malloc(a_count); \
143 stl::report_and_fail("out of memory"sv); \
147 [[nodiscard]] constexpr void* operator new(std::size_t, void* a_ptr) noexcept { return a_ptr; } \
148 [[nodiscard]] constexpr void* operator new[](std::size_t, void* a_ptr) noexcept { return a_ptr; } \
149 [[nodiscard]] constexpr void* operator new(std::size_t, std::align_val_t, void* a_ptr) noexcept { return a_ptr; } \
150 [[nodiscard]] constexpr void* operator new[](std::size_t, std::align_val_t, void* a_ptr) noexcept { return a_ptr; } \
152 inline void operator delete(void* a_ptr) { RE::free(a_ptr); } \
153 inline void operator delete[](void* a_ptr) { RE::free(a_ptr); } \
154 inline void operator delete(void* a_ptr, std::align_val_t) { RE::aligned_free(a_ptr); } \
155 inline void operator delete[](void* a_ptr, std::align_val_t) { RE::aligned_free(a_ptr); } \
156 inline void operator delete(void* a_ptr, std::size_t) { RE::free(a_ptr); } \
157 inline void operator delete[](void* a_ptr, std::size_t) { RE::free(a_ptr); } \
158 inline void operator delete(void* a_ptr, std::size_t, std::align_val_t) { RE::aligned_free(a_ptr); } \
159 inline void operator delete[](void* a_ptr, std::size_t, std::align_val_t) { RE::aligned_free(a_ptr); }
184 static_assert(!std::is_trivially_destructible_v<value_type>,
"there's no allocation overhead for trivially destructible types");
192 assert(a_pos <
size());
198 assert(a_pos <
size());
219 [[nodiscard]]
bool empty() const noexcept {
return size() == 0; }
234 const auto oldSize =
size();
235 if (oldSize == a_count) {
239 const auto newData = [=]() {
241 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
244 bytes +=
sizeof(std::size_t);
247 const auto data = malloc<std::size_t>(bytes);
250 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
257 if (a_count < oldSize) {
258 std::uninitialized_move_n(
data(), a_count, newData);
260 std::uninitialized_move_n(
data(), oldSize, newData);
261 std::uninitialized_default_construct_n(newData + oldSize, a_count - oldSize);
271 assert(
_data !=
nullptr);
272 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
275 return reinterpret_cast<std::size_t*
>(
_data) - 1;
279 [[nodiscard]]
const void*
get_head() const noexcept
281 assert(
_data !=
nullptr);
282 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
285 return reinterpret_cast<const std::size_t*
>(
_data) - 1;
Definition: BSSmallBlockAllocator.h:61
Definition: CompactingStore.h:15
Definition: IMemoryHeap.h:31
Definition: MemoryManager.h:16
std::uint16_t numHeaps
Definition: MemoryManager.h:38
BSSmallBlockAllocator * smallBlockAllocator
Definition: MemoryManager.h:47
std::uint32_t mallocBytes
Definition: MemoryManager.h:53
IMemoryHeap * defaultHeap
Definition: MemoryManager.h:59
std::uint32_t numMemoryProblemPassesRun
Definition: MemoryManager.h:57
std::uint16_t numPhysicalHeaps
Definition: MemoryManager.h:39
bool specialHeaps
Definition: MemoryManager.h:50
CompactingStore::Store * compactingStore
Definition: MemoryManager.h:48
std::uint32_t alignmentForPools
Definition: MemoryManager.h:54
ScrapHeap * GetThreadScrapHeap()
void Deallocate(void *a_mem, bool a_alignmentRequired)
std::size_t failedAllocationSize
Definition: MemoryManager.h:56
ThreadScrapHeap * threadScrapHeap
Definition: MemoryManager.h:43
bool allowPoolUse
Definition: MemoryManager.h:51
static MemoryManager * GetSingleton()
std::uint32_t sysAllocBytes
Definition: MemoryManager.h:52
IMemoryHeap ** physicalHeaps
Definition: MemoryManager.h:44
IMemoryHeap ** heaps
Definition: MemoryManager.h:40
IMemoryHeap * bigAllocHeap
Definition: MemoryManager.h:45
void * Reallocate(void *a_oldMem, std::size_t a_newSize, std::int32_t a_alignment, bool a_aligned)
std::uint32_t mainThreadMemoryProblemPassSignal
Definition: MemoryManager.h:55
IMemoryHeap * emergencyHeap
Definition: MemoryManager.h:46
std::size_t timeOfLastMemoryProblemPass
Definition: MemoryManager.h:58
void RegisterMemoryManager()
void * Allocate(std::size_t a_size, std::int32_t a_alignment, bool a_alignmentRequired)
IMemoryHeap * externalHavokAllocator
Definition: MemoryManager.h:49
IMemoryHeap * heapsByContext[127]
Definition: MemoryManager.h:42
bool * allowOtherContextAllocs
Definition: MemoryManager.h:41
bool initialized
Definition: MemoryManager.h:37
Definition: ScrapHeap.h:8
Definition: MemoryManager.h:166
const_iterator begin() const noexcept
Definition: MemoryManager.h:212
const_iterator end() const noexcept
Definition: MemoryManager.h:216
size_type size() const noexcept
Definition: MemoryManager.h:221
value_type & reference
Definition: MemoryManager.h:171
std::size_t size_type
Definition: MemoryManager.h:169
T value_type
Definition: MemoryManager.h:168
void * get_head() noexcept
Definition: MemoryManager.h:269
const_iterator cend() const noexcept
Definition: MemoryManager.h:217
const void * get_head() const noexcept
Definition: MemoryManager.h:279
pointer data() noexcept
Definition: MemoryManager.h:208
iterator begin() noexcept
Definition: MemoryManager.h:211
void clear()
Definition: MemoryManager.h:223
pointer _data
Definition: MemoryManager.h:290
iterator end() noexcept
Definition: MemoryManager.h:215
const value_type & const_reference
Definition: MemoryManager.h:172
~SimpleArray()
Definition: MemoryManager.h:182
reference back() noexcept
Definition: MemoryManager.h:205
value_type * iterator
Definition: MemoryManager.h:175
reference front() noexcept
Definition: MemoryManager.h:202
void resize(size_type a_count)
Definition: MemoryManager.h:232
value_type * pointer
Definition: MemoryManager.h:173
bool empty() const noexcept
Definition: MemoryManager.h:219
const value_type * const_iterator
Definition: MemoryManager.h:176
constexpr SimpleArray() noexcept=default
const_reference front() const noexcept
Definition: MemoryManager.h:203
const_reference operator[](size_type a_pos) const noexcept
Definition: MemoryManager.h:196
const_pointer data() const noexcept
Definition: MemoryManager.h:209
const_iterator cbegin() const noexcept
Definition: MemoryManager.h:213
const_reference back() const noexcept
Definition: MemoryManager.h:206
std::ptrdiff_t difference_type
Definition: MemoryManager.h:170
const value_type * const_pointer
Definition: MemoryManager.h:174
reference operator[](size_type a_pos) noexcept
Definition: MemoryManager.h:190
Definition: AbsorbEffect.h:6
void * realloc(void *a_ptr, std::size_t a_newSize)
Definition: MemoryManager.h:108
void * calloc(std::size_t a_num, std::size_t a_size)
Definition: MemoryManager.h:94
void * malloc(std::size_t a_size)
Definition: MemoryManager.h:66
void * aligned_realloc(void *a_ptr, std::size_t a_newSize, std::size_t a_alignment)
Definition: MemoryManager.h:116
void * aligned_alloc(std::size_t a_alignment, std::size_t a_size)
Definition: MemoryManager.h:80
void aligned_free(void *a_ptr)
Definition: MemoryManager.h:19
ScrapHeap heap
Definition: MemoryManager.h:22
ThreadScrapHeap * next
Definition: MemoryManager.h:23
std::uint32_t owningThread
Definition: MemoryManager.h:24
std::uint32_t pad
Definition: MemoryManager.h:25