7 namespace CompactingStore
12 class BSSmallBlockAllocator;
36 [[nodiscard]]
void*
Allocate(std::size_t a_size, std::int32_t a_alignment,
bool a_alignmentRequired)
40 return func(
this, a_size, a_alignment, a_alignmentRequired);
47 return func(
this, a_mem, a_alignmentRequired);
57 [[nodiscard]]
void*
Reallocate(
void* a_oldMem, std::size_t a_newSize, std::int32_t a_alignment,
bool a_aligned)
61 return func(
this, a_oldMem, a_newSize, a_alignment, a_aligned);
96 static_assert(
sizeof(MemoryManager) == 0x480);
98 inline void*
malloc(std::size_t a_size)
102 heap->Allocate(a_size, 0,
false) :
109 return static_cast<T*
>(
malloc(a_size));
115 return malloc<T>(
sizeof(T));
122 heap->Allocate(a_size,
static_cast<std::int32_t
>(a_alignment),
true) :
135 return aligned_alloc<T>(
alignof(T),
sizeof(T));
138 inline void*
calloc(std::size_t a_num, std::size_t a_size)
140 const auto ret =
malloc(a_num * a_size);
142 std::memset(ret, 0, a_num * a_size);
148 inline T*
calloc(std::size_t a_num, std::size_t a_size)
150 return static_cast<T*
>(
calloc(a_num, a_size));
156 return calloc<T>(a_num,
sizeof(T));
159 inline void*
realloc(
void* a_ptr, std::size_t a_newSize)
163 heap->Reallocate(a_ptr, a_newSize, 0,
false) :
168 inline T*
realloc(
void* a_ptr, std::size_t a_newSize)
170 return static_cast<T*
>(
realloc(a_ptr, a_newSize));
173 inline void*
aligned_realloc(
void* a_ptr, std::size_t a_newSize, std::size_t a_alignment)
177 heap->Reallocate(a_ptr, a_newSize,
static_cast<std::int32_t
>(a_alignment),
true) :
182 inline T*
aligned_realloc(
void* a_ptr, std::size_t a_newSize, std::size_t a_alignment)
184 return static_cast<T*
>(
aligned_realloc(a_ptr, a_newSize, a_alignment));
191 heap->Deallocate(a_ptr,
false);
199 heap->Deallocate(a_ptr,
true);
204#define TES_HEAP_REDEFINE_NEW() \
205 [[nodiscard]] inline void* operator new(std::size_t a_count) \
207 const auto mem = RE::malloc(a_count); \
211 stl::report_and_fail("out of memory"sv); \
215 [[nodiscard]] inline void* operator new[](std::size_t a_count) \
217 const auto mem = RE::malloc(a_count); \
221 stl::report_and_fail("out of memory"sv); \
225 [[nodiscard]] constexpr void* operator new(std::size_t, void* a_ptr) noexcept { return a_ptr; } \
226 [[nodiscard]] constexpr void* operator new[](std::size_t, void* a_ptr) noexcept { return a_ptr; } \
227 [[nodiscard]] constexpr void* operator new(std::size_t, std::align_val_t, void* a_ptr) noexcept { return a_ptr; } \
228 [[nodiscard]] constexpr void* operator new[](std::size_t, std::align_val_t, void* a_ptr) noexcept { return a_ptr; } \
230 inline void operator delete(void* a_ptr) { RE::free(a_ptr); } \
231 inline void operator delete[](void* a_ptr) { RE::free(a_ptr); } \
232 inline void operator delete(void* a_ptr, std::align_val_t) { RE::aligned_free(a_ptr); } \
233 inline void operator delete[](void* a_ptr, std::align_val_t) { RE::aligned_free(a_ptr); } \
234 inline void operator delete(void* a_ptr, std::size_t) { RE::free(a_ptr); } \
235 inline void operator delete[](void* a_ptr, std::size_t) { RE::free(a_ptr); } \
236 inline void operator delete(void* a_ptr, std::size_t, std::align_val_t) { RE::aligned_free(a_ptr); } \
237 inline void operator delete[](void* a_ptr, std::size_t, std::align_val_t) { RE::aligned_free(a_ptr); }
262 static_assert(!std::is_trivially_destructible_v<value_type>,
"there's no allocation overhead for trivially destructible types");
270 assert(a_pos <
size());
276 assert(a_pos <
size());
297 [[nodiscard]]
bool empty() const noexcept {
return size() == 0; }
312 const auto oldSize =
size();
313 if (oldSize == a_count) {
317 const auto newData = [=]() {
319 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
322 bytes +=
sizeof(std::size_t);
325 const auto data = malloc<std::size_t>(bytes);
328 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
335 if (a_count < oldSize) {
336 std::uninitialized_move_n(
data(), a_count, newData);
338 std::uninitialized_move_n(
data(), oldSize, newData);
339 std::uninitialized_default_construct_n(newData + oldSize, a_count - oldSize);
349 assert(
_data !=
nullptr);
350 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
353 return reinterpret_cast<std::size_t*
>(
_data) - 1;
357 [[nodiscard]]
const void*
get_head() const noexcept
359 assert(
_data !=
nullptr);
360 if constexpr (
alignof(
value_type) >
alignof(std::size_t)) {
363 return reinterpret_cast<const std::size_t*
>(
_data) - 1;
#define RELOCATION_ID(a_se, a_ae)
Definition PCH.h:794
Definition BSSmallBlockAllocator.h:61
Definition CompactingStore.h:15
Definition IMemoryHeap.h:31
Definition MemoryManager.h:16
std::uint16_t numHeaps
Definition MemoryManager.h:73
BSSmallBlockAllocator * smallBlockAllocator
Definition MemoryManager.h:82
std::uint32_t mallocBytes
Definition MemoryManager.h:88
IMemoryHeap * defaultHeap
Definition MemoryManager.h:94
std::uint32_t numMemoryProblemPassesRun
Definition MemoryManager.h:92
std::uint16_t numPhysicalHeaps
Definition MemoryManager.h:74
bool specialHeaps
Definition MemoryManager.h:85
CompactingStore::Store * compactingStore
Definition MemoryManager.h:83
std::uint32_t alignmentForPools
Definition MemoryManager.h:89
void Deallocate(void *a_mem, bool a_alignmentRequired)
Definition MemoryManager.h:43
static MemoryManager * GetSingleton()
Definition MemoryManager.h:29
std::size_t failedAllocationSize
Definition MemoryManager.h:91
void * Reallocate(void *a_oldMem, std::size_t a_newSize, std::int32_t a_alignment, bool a_aligned)
Definition MemoryManager.h:57
ThreadScrapHeap * threadScrapHeap
Definition MemoryManager.h:78
bool allowPoolUse
Definition MemoryManager.h:86
std::uint32_t sysAllocBytes
Definition MemoryManager.h:87
IMemoryHeap ** physicalHeaps
Definition MemoryManager.h:79
IMemoryHeap ** heaps
Definition MemoryManager.h:75
IMemoryHeap * bigAllocHeap
Definition MemoryManager.h:80
ScrapHeap * GetThreadScrapHeap()
Definition MemoryManager.h:50
std::uint32_t mainThreadMemoryProblemPassSignal
Definition MemoryManager.h:90
IMemoryHeap * emergencyHeap
Definition MemoryManager.h:81
std::size_t timeOfLastMemoryProblemPass
Definition MemoryManager.h:93
void RegisterMemoryManager()
Definition MemoryManager.h:64
IMemoryHeap * externalHavokAllocator
Definition MemoryManager.h:84
IMemoryHeap * heapsByContext[127]
Definition MemoryManager.h:77
bool * allowOtherContextAllocs
Definition MemoryManager.h:76
bool initialized
Definition MemoryManager.h:72
void * Allocate(std::size_t a_size, std::int32_t a_alignment, bool a_alignmentRequired)
Definition MemoryManager.h:36
Definition MemoryManager.h:244
const_iterator begin() const noexcept
Definition MemoryManager.h:290
const_iterator end() const noexcept
Definition MemoryManager.h:294
size_type size() const noexcept
Definition MemoryManager.h:299
value_type & reference
Definition MemoryManager.h:249
std::size_t size_type
Definition MemoryManager.h:247
T value_type
Definition MemoryManager.h:246
const_iterator cend() const noexcept
Definition MemoryManager.h:295
pointer data() noexcept
Definition MemoryManager.h:286
iterator begin() noexcept
Definition MemoryManager.h:289
void clear()
Definition MemoryManager.h:301
pointer _data
Definition MemoryManager.h:368
iterator end() noexcept
Definition MemoryManager.h:293
const value_type & const_reference
Definition MemoryManager.h:250
~SimpleArray()
Definition MemoryManager.h:260
reference back() noexcept
Definition MemoryManager.h:283
void * get_head() noexcept
Definition MemoryManager.h:347
value_type * iterator
Definition MemoryManager.h:253
reference front() noexcept
Definition MemoryManager.h:280
void resize(size_type a_count)
Definition MemoryManager.h:310
value_type * pointer
Definition MemoryManager.h:251
bool empty() const noexcept
Definition MemoryManager.h:297
const value_type * const_iterator
Definition MemoryManager.h:254
constexpr SimpleArray() noexcept=default
const_reference front() const noexcept
Definition MemoryManager.h:281
const_reference operator[](size_type a_pos) const noexcept
Definition MemoryManager.h:274
const_pointer data() const noexcept
Definition MemoryManager.h:287
const_iterator cbegin() const noexcept
Definition MemoryManager.h:291
const_reference back() const noexcept
Definition MemoryManager.h:284
std::ptrdiff_t difference_type
Definition MemoryManager.h:248
const void * get_head() const noexcept
Definition MemoryManager.h:357
const value_type * const_pointer
Definition MemoryManager.h:252
reference operator[](size_type a_pos) noexcept
Definition MemoryManager.h:268
Definition Relocation.h:1638
Definition AbsorbEffect.h:6
void * realloc(void *a_ptr, std::size_t a_newSize)
Definition MemoryManager.h:159
T * aligned_alloc()
Definition MemoryManager.h:133
T * malloc()
Definition MemoryManager.h:113
void * calloc(std::size_t a_num, std::size_t a_size)
Definition MemoryManager.h:138
void * aligned_realloc(void *a_ptr, std::size_t a_newSize, std::size_t a_alignment)
Definition MemoryManager.h:173
void free(void *a_ptr)
Definition MemoryManager.h:187
void aligned_free(void *a_ptr)
Definition MemoryManager.h:195
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