OpenBarnyard
 
Loading...
Searching...
No Matches
TMemory.h
Go to the documentation of this file.
1
10
11#pragma once
12#include "Toshi/TNodeList.h"
13
14#ifdef TOSHI_PROFILER_MEMORY
15# include "TMemoryDebug.h"
16#endif // TOSHI_PROFILER_MEMORY
17
18#ifdef TMEMORY_USE_DLMALLOC
19# include "TMemory_dlmalloc.h"
20#endif // TMEMORY_USE_DLMALLOC
21
22#ifndef TMEMORY_USE_DLMALLOC
23
24// Memory allocation constants
25# define TMEMORY_ROUNDUP 4
26# define TMEMORY_FLAGS_HOLE_PROCESS 1
27# define TMEMORY_FLAGS_MASK ( ( 1 << 2 ) - 1 )
28# define TMEMORY_NUM_FREELISTS 9
29# define TMEMORY_NUM_BLOCK_SLOTS 128
30# define TMEMORY_ALLOC_MEMNODE_SIZE sizeof( Toshi::TMemory::MemNode )
31# define TMEMORY_ALLOC_RESERVED_SIZE ( TMEMORY_ALLOC_MEMNODE_SIZE - sizeof( void* ) )
32
34
35extern class TMemory* g_pMemory;
36
48{
49public:
54 struct MemBlock;
55
60 struct MemBlockSlot;
61
69 class MemNode
70 {
71 public:
76 void* GetDataRegionStart() const { return (void*)( &pPrevHole ); }
77
82 void* GetDataRegionEnd() const { return (void*)( ( (TUINTPTR)&pPrevHole + TAlignNumDown( uiSize ) ) ); }
83
84 public:
87 union
88 {
91 };
92
94 };
95
112
124
129 struct MemBlockSlot : public TNodeList<MemBlockSlot>::TNode
130 {
132 };
133
155
168
169public:
174 TMemory();
175
180 ~TMemory();
181
191 void* Alloc( TSIZE a_uiSize, TSIZE a_uiAlignment, MemBlock* a_pMemBlock, const TCHAR* a_szFileName, TINT a_iLineNum );
192
198 TBOOL Free( const void* a_pMem );
199
205 void* SysAlloc( TSIZE a_uiSize );
206
211 void SysFree( void* a_pMem );
212
221 MemBlock* CreateMemBlock( TSIZE a_uiSize, const TCHAR* a_szName, MemBlock* a_pOwnerBlock, TINT a_iUnused );
222
230 MemBlock* CreateMemBlockInPlace( void* a_pMem, TSIZE a_uiSize, const TCHAR* a_szName );
231
236 void DestroyMemBlock( MemBlock* a_pMemBlock );
237
242 MemBlock* GetGlobalBlock() const;
243
250
251 TUINT GetGlobalFlags() const { return m_uiGlobalFlags; }
252
253 void DumpMemInfo();
254
255private:
256 TBOOL FreeMemBlock( MemBlock* a_pMemBlock );
257 void SetMemBlockUnused( MemBlock* a_pMemBlock );
258
259 static MemNode* GetMemNodeFromAddress( void* a_pMem );
260 static void ExtendNodeSize( MemNode* a_pNode, TSIZE a_uiExtendSize ) { a_pNode->uiSize = a_uiExtendSize | ( a_pNode->uiSize & TMEMORY_FLAGS_MASK ); }
261 static void SetHoleSize( MemNode* a_pNode, TSIZE a_uiHoleSize ) { a_pNode->uiSize = a_uiHoleSize; }
262 static TBOOL IsProcess( MemNode* a_pNode ) { return HASANYFLAG( a_pNode->uiSize, TMEMORY_FLAGS_HOLE_PROCESS ); }
263 static void SetProcess( MemBlock* a_pMemBlock, MemNode* a_pNode, TSIZE a_uiHoleSize )
264 {
265 a_pNode->uiSize = a_uiHoleSize | g_pMemory->GetGlobalFlags() | TMEMORY_FLAGS_HOLE_PROCESS;
266 a_pNode->pMemBlock = a_pMemBlock;
267 }
268 static TSIZE GetNodeSize( MemNode* a_pNode ) { return TAlignNumDown( a_pNode->uiSize ); }
269 static MemBlock* GetProcessMemBlock( MemNode* a_pNode ) { return a_pNode->pMemBlock; }
270 static void ConvertProcessToHole( MemNode* a_pNode ) { a_pNode->uiSize &= ~TMEMORY_FLAGS_MASK; }
271 static int TestMemIntegrity( MemBlock* a_pMemBlock );
272 static int DebugTestMemoryBlock( MemBlock* a_pMemBlock );
273
274public:
275 static void GetMemInfo( MemInfo& a_rMemInfo, MemBlock* a_pMemBlock );
276 static void GetHALMemInfo( HALMemInfo& a_rHALMemInfo );
277 static TBOOL Initialise( TSIZE a_uiHeapSize, TSIZE a_uiReservedSize, TUINT a_uiUnused = 0 );
278 static void Deinitialise();
279 static TUINT MapSizeToFreeList( TSIZE a_uiSize );
280 static void DebugPrintHALMemInfo( const TCHAR* a_szFormat, ... );
281
282private:
283 inline static class TMutex* ms_pGlobalMutex;
284
285private:
286 TSIZE m_TotalAllocatedSize;
287 TSIZE m_ReservedSize;
288 TSIZE m_MainBlockSize;
289 TNodeList<MemBlockSlot> m_UsedBlocks;
290 TNodeList<MemBlockSlot> m_FreeBlocks;
291 MemBlockSlot m_aBlockSlots[ TMEMORY_NUM_BLOCK_SLOTS ];
292 void* m_pMemory;
293 MemBlock* m_pGlobalBlock;
294 TUINT m_uiGlobalFlags;
295 TUINT m_Unknown2;
296 TBOOL m_bFlag1;
297 TBOOL m_bFlag2;
298 HALMemInfo m_HALMemInfo;
299};
300
302
303#else // !TMEMORY_USE_DLMALLOC
304
306
307//-----------------------------------------------------------------------------
308// Purpose: A wrapper around TMemoryDL that makes it's API compatible to the
309// JPOG/Barnyard memory allocator.
310//-----------------------------------------------------------------------------
311class TMemory
312{
313public:
314 using MemBlock = TMemoryDLHeap;
315
316 struct MemInfo
317 {
332 };
333
334 struct HALMemInfo
335 {
336 TUINT m_Unknown1[ 10 ];
338 TUINT m_Unknown2[ 15 ];
339 };
340
341public:
342 TMemory();
343
344 MemBlock* CreateMemBlock( TUINT a_uiSize, const TCHAR* a_szName, MemBlock* a_pOwnerBlock, TINT a_iUnused );
345 MemBlock* CreateMemBlockInPlace( void* a_pMem, TUINT a_uiSize, const TCHAR* a_szName );
346 void DestroyMemBlock( MemBlock* a_pMemBlock );
347
348 MemBlock* GetGlobalBlock() const;
349 MemBlock* SetGlobalBlock( MemBlock* a_pMemBlock );
350
351 void SetMemModule( TMemoryDL* a_pMemModule ) { m_pMemModule = a_pMemModule; }
352 TMemoryDL* GetMemModule() { return m_pMemModule; }
353
354public:
355 static TBOOL Initialise( TUINT a_uiHeapSize, TUINT a_uiReservedSize, TMemoryDL::Flags a_eFlags = TMemoryDL::Flags_Standard );
356 static void Deinitialise();
357
358 static void GetMemInfo( MemInfo& a_rMemInfo, MemBlock* a_pMemBlock );
359 static void GetHALMemInfo( HALMemInfo& a_rHALMemInfo );
360
361public:
362 TMemoryDL* m_pMemModule;
363 TCHAR PADDING[ 2092 ];
364 MemBlock* m_pGlobalBlock;
365};
366
367extern TMemory* g_pMemory;
368
370
371#endif // TMEMORY_USE_DLMALLOC
372
378void* TMalloc( TSIZE a_uiSize );
379
387void* TMalloc( TSIZE a_uiSize, const TCHAR* a_szFileName, TINT a_iLineNum );
388
397void* TMalloc( TSIZE a_uiSize, Toshi::TMemory::MemBlock* a_pMemBlock, const TCHAR* a_szFileName = TNULL, TINT a_iLineNum = -1 );
398
405void* TMemalign( TSIZE a_uiSize, TSIZE a_uiAlignment );
406
414void* TMemalign( TSIZE a_uiAlignment, TSIZE a_uiSize, Toshi::TMemory::MemBlock* a_pMemBlock );
415
420void TFree( void* a_pMem );
421
430template <class T, class... Args>
431TFORCEINLINE T* TConstruct( T* a_pMemory, Args&&... args )
432{
433 return new ( a_pMemory ) T( std::forward<Args>( args )... );
434}
435
445template <class T, class... Args>
446TFORCEINLINE T* TConstructArray( T* a_pMemory, TSIZE a_uiNumTimes, Args&&... args )
447{
448 for ( TSIZE i = 0; i < a_uiNumTimes; i++ )
449 {
450 new ( a_pMemory + i ) T( std::forward<Args>( args )... );
451 }
452
453 return a_pMemory;
454}
455
461template <class T>
462TFORCEINLINE void TDestruct( T* a_pPtr )
463{
464 if ( a_pPtr )
465 {
466 a_pPtr->~T();
467 }
468}
469
476TFORCEINLINE void* __CRTDECL operator new( size_t size, Toshi::TMemory::MemBlock* block )
477{
478#ifdef TOSHI_PROFILER_MEMORY
479 return TMalloc( size, block, TMemory__FILE__, TMemory__LINE__ );
480#else
481 return TMalloc( size, block, TNULL, -1 );
482#endif // TOSHI_PROFILER_MEMORY
483}
484
491TFORCEINLINE void* __CRTDECL operator new[]( size_t size, Toshi::TMemory::MemBlock* block )
492{
493#ifdef TOSHI_PROFILER_MEMORY
494 return TMalloc( size, block, TMemory__FILE__, TMemory__LINE__ );
495#else
496 return TMalloc( size, block, TNULL, -1 );
497#endif // TOSHI_PROFILER_MEMORY
498}
499
505TFORCEINLINE void __CRTDECL operator delete( void* ptr, Toshi::TMemory::MemBlock* block ) noexcept
506{
507 TFree( ptr );
508}
509
515TFORCEINLINE void __CRTDECL operator delete[]( void* ptr, Toshi::TMemory::MemBlock* block ) noexcept
516{
517 TFree( ptr );
518}
#define TMEMORY_NUM_FREELISTS
Number of free list buckets for different sizes.
Definition TMemory.h:28
void * TMemalign(TSIZE a_uiSize, TSIZE a_uiAlignment)
Allocates aligned memory.
Definition TMemory.cpp:1038
TOSHI_NAMESPACE_START class TMemory * g_pMemory
#define TMEMORY_NUM_BLOCK_SLOTS
Maximum number of memory block slots.
Definition TMemory.h:29
TOSHI_NAMESPACE_END void * TMalloc(TSIZE a_uiSize)
Allocates memory with default alignment.
Definition TMemory.cpp:1005
TFORCEINLINE T * TConstructArray(T *a_pMemory, TSIZE a_uiNumTimes, Args &&... args)
Constructs an array of objects in place.
Definition TMemory.h:446
#define TMEMORY_FLAGS_HOLE_PROCESS
Flag indicating a memory block is in use.
Definition TMemory.h:26
TFORCEINLINE void TDestruct(T *a_pPtr)
Destructs an object.
Definition TMemory.h:462
TFORCEINLINE T * TConstruct(T *a_pMemory, Args &&... args)
Constructs an object in place.
Definition TMemory.h:431
void TFree(void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:1054
#define TMEMORY_FLAGS_MASK
Mask for memory block flags.
Definition TMemory.h:27
#define HASANYFLAG(STATE, FLAG)
Definition Defines.h:5
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TFORCEINLINE
Definition Defines.h:74
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
TFORCEINLINE constexpr T TAlignNumDown(T a_iValue, TSIZE a_uiAlignment=4)
Definition Helpers.h:59
unsigned int TUINT
Definition Typedefs.h:8
uintptr_t TUINTPTR
Definition Typedefs.h:18
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
bool TBOOL
Definition Typedefs.h:6
Core memory management class that handles all memory allocations and deallocations.
Definition TMemory.h:48
static TBOOL Initialise(TSIZE a_uiHeapSize, TSIZE a_uiReservedSize, TUINT a_uiUnused=0)
Initializes the memory system.
Definition TMemory.cpp:708
TBOOL Free(const void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:399
static TUINT MapSizeToFreeList(TSIZE a_uiSize)
Definition TMemory.cpp:767
TUINT GetGlobalFlags() const
Definition TMemory.h:251
static void GetMemInfo(MemInfo &a_rMemInfo, MemBlock *a_pMemBlock)
Definition TMemory.cpp:864
static void Deinitialise()
Deinitializes the memory system.
Definition TMemory.cpp:743
MemBlock * GetGlobalBlock() const
Gets the global memory block.
Definition TMemory.cpp:582
void DumpMemInfo()
Definition TMemory.cpp:809
MemBlock * CreateMemBlockInPlace(void *a_pMem, TSIZE a_uiSize, const TCHAR *a_szName)
Creates a memory block at a specific location.
Definition TMemory.cpp:508
static void GetHALMemInfo(HALMemInfo &a_rHALMemInfo)
Definition TMemory.cpp:956
void DestroyMemBlock(MemBlock *a_pMemBlock)
Destroys a memory block.
Definition TMemory.cpp:572
static void DebugPrintHALMemInfo(const TCHAR *a_szFormat,...)
Debug print of HAL memory info.
Definition TMemory.cpp:845
void * Alloc(TSIZE a_uiSize, TSIZE a_uiAlignment, MemBlock *a_pMemBlock, const TCHAR *a_szFileName, TINT a_iLineNum)
Allocates memory with specified size and alignment.
Definition TMemory.cpp:213
void * SysAlloc(TSIZE a_uiSize)
Allocates memory from the system heap.
void SysFree(void *a_pMem)
Frees memory back to the system heap.
TMemory::MemBlock * SetGlobalBlock(MemBlock *a_pMemBlock)
Sets the global memory block.
Definition TMemory.cpp:793
MemBlock * CreateMemBlock(TSIZE a_uiSize, const TCHAR *a_szName, MemBlock *a_pOwnerBlock, TINT a_iUnused)
Creates a new memory block.
Definition TMemory.cpp:488
TMemory()
Constructor for TMemory class Initializes the memory management system.
Definition TMemory.cpp:138
~TMemory()
Destructor for TMemory class Cleans up the memory management system.
Definition TMemory.cpp:158
Represents a node in the memory allocation system.
Definition TMemory.h:70
MemNode * pOwner
Owner node if this is a sub-allocation.
Definition TMemory.h:85
TSIZE uiSize
Size of the memory region.
Definition TMemory.h:86
MemNode * pNextHole
Next free hole in the list.
Definition TMemory.h:89
MemNode * pPrevHole
Previous free hole in the list.
Definition TMemory.h:93
void * GetDataRegionEnd() const
Gets the end address of the data region for this node.
Definition TMemory.h:82
MemBlock * pMemBlock
Memory block this node belongs to.
Definition TMemory.h:90
void * GetDataRegionStart() const
Gets the start address of the data region for this node.
Definition TMemory.h:76
Represents a contiguous block of memory that can be allocated from.
Definition TMemory.h:101
TSIZE m_uiTotalSize2
Duplicate of total size for validation.
Definition TMemory.h:105
MemBlock * m_pNextBlock
Next block in the chain.
Definition TMemory.h:104
MemNode * m_apHoles[9]
Array of free lists by size.
Definition TMemory.h:106
MemNode m_RootHole
Root node for the block.
Definition TMemory.h:110
MemNode * m_pFirstHole
First free hole in the block.
Definition TMemory.h:107
TCHAR m_szSignature[8]
Block signature for validation.
Definition TMemory.h:108
MemBlockSlot * m_pSlot
Slot tracking this block.
Definition TMemory.h:102
TCHAR m_szName[52]
Name of the memory block.
Definition TMemory.h:109
TSIZE m_uiTotalSize1
Total size of the block.
Definition TMemory.h:103
Footer structure at the end of each memory block for validation.
Definition TMemory.h:118
TUINT m_Unk1
Unknown field 1.
Definition TMemory.h:119
TUINT m_Unk2
Unknown field 2.
Definition TMemory.h:120
TUINT m_Unk4
Unknown field 4.
Definition TMemory.h:122
MemBlock * m_pBlockOwner
Pointer to the owning block.
Definition TMemory.h:121
Slot for tracking memory blocks in the block list.
Definition TMemory.h:130
MemBlock * m_pPtr
Pointer to the memory block.
Definition TMemory.h:131
Structure containing memory usage statistics.
Definition TMemory.h:139
TSIZE m_uiTotalFree
Total free memory.
Definition TMemory.h:142
TSIZE m_uiSmallestProcess
Size of smallest allocation.
Definition TMemory.h:151
TINT m_iNumProcesses
Number of allocated regions.
Definition TMemory.h:147
TSIZE m_uiUnk4
Unknown field 4.
Definition TMemory.h:153
TSIZE m_uiLogicTotalSize
Logical total size.
Definition TMemory.h:141
TSIZE m_uiUnk3
Unknown field 3.
Definition TMemory.h:152
TSIZE m_uiLogicTotalUsed
Logical total used memory.
Definition TMemory.h:145
TSIZE m_uiSmallestHole
Size of smallest free hole.
Definition TMemory.h:150
TSIZE m_uiLargestProcess
Size of largest allocation.
Definition TMemory.h:149
TSIZE m_uiLargestHole
Size of largest free hole.
Definition TMemory.h:148
TSIZE m_uiLogicTotalFree
Logical total free memory.
Definition TMemory.h:143
TSIZE m_uiTotalSize
Total size of memory block.
Definition TMemory.h:140
TINT m_iNumHoles
Number of free holes.
Definition TMemory.h:146
TSIZE m_uiTotalUsed
Total used memory.
Definition TMemory.h:144
Hardware abstraction layer memory information.
Definition TMemory.h:161
TUINT m_Unknown1[10]
Unknown fields 1-10.
Definition TMemory.h:164
TUINT m_Unknown2[15]
Unknown fields 11-25.
Definition TMemory.h:166
HALMemInfo()
Constructor for HALMemInfo Initializes all fields to zero.
Definition TMemory.cpp:965
TSIZE m_uiMemUsage
Total memory usage.
Definition TMemory.h:165
constexpr TNode()
Definition TNodeList.h:15