OpenBarnyard
 
Loading...
Searching...
No Matches
TMemory.h File Reference

Core memory management system for the Toshi engine. More...

#include "Toshi/TNodeList.h"

Go to the source code of this file.

Classes

class  TMemory
 Core memory management class that handles all memory allocations and deallocations. More...
 
class  TMemory::MemNode
 Represents a node in the memory allocation system. More...
 
struct  TMemory::MemBlock
 Represents a contiguous block of memory that can be allocated from. More...
 
struct  TMemory::MemBlockFooter
 Footer structure at the end of each memory block for validation. More...
 
struct  TMemory::MemBlockSlot
 Slot for tracking memory blocks in the block list. More...
 
struct  TMemory::MemInfo
 Structure containing memory usage statistics. More...
 
struct  TMemory::HALMemInfo
 Hardware abstraction layer memory information. More...
 

Macros

#define TMEMORY_ROUNDUP   4
 Minimum alignment for memory blocks.
 
#define TMEMORY_FLAGS_HOLE_PROCESS   1
 Flag indicating a memory block is in use.
 
#define TMEMORY_FLAGS_MASK   ( ( 1 << 2 ) - 1 )
 Mask for memory block flags.
 
#define TMEMORY_NUM_FREELISTS   9
 Number of free list buckets for different sizes.
 
#define TMEMORY_NUM_BLOCK_SLOTS   128
 Maximum number of memory block slots.
 
#define TMEMORY_ALLOC_MEMNODE_SIZE   sizeof( Toshi::TMemory::MemNode )
 Size of memory node structure.
 
#define TMEMORY_ALLOC_RESERVED_SIZE   ( TMEMORY_ALLOC_MEMNODE_SIZE - sizeof( void* ) )
 Reserved space in allocations.
 

Functions

TOSHI_NAMESPACE_END void * TMalloc (TSIZE a_uiSize)
 Allocates memory with default alignment.
 
void * TMalloc (TSIZE a_uiSize, const TCHAR *a_szFileName, TINT a_iLineNum)
 Allocates memory with debug information.
 
void * TMalloc (TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock, const TCHAR *a_szFileName=TNULL, TINT a_iLineNum=-1)
 Allocates memory from a specific memory block.
 
void * TMemalign (TSIZE a_uiSize, TSIZE a_uiAlignment)
 Allocates aligned memory.
 
void * TMemalign (TSIZE a_uiAlignment, TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock)
 Allocates aligned memory from a specific memory block.
 
void TFree (void *a_pMem)
 Frees previously allocated memory.
 
template<class T, class... Args>
TFORCEINLINE T * TConstruct (T *a_pMemory, Args &&... args)
 Constructs an object in place.
 
template<class T, class... Args>
TFORCEINLINE T * TConstructArray (T *a_pMemory, TSIZE a_uiNumTimes, Args &&... args)
 Constructs an array of objects in place.
 
template<class T>
TFORCEINLINE void TDestruct (T *a_pPtr)
 Destructs an object.
 
TFORCEINLINE void *__CRTDECL operator new (size_t size, Toshi::TMemory::MemBlock *block)
 Placement new operator for memory blocks.
 
TFORCEINLINE void *__CRTDECL operator new[] (size_t size, Toshi::TMemory::MemBlock *block)
 Placement new[] operator for memory blocks.
 
TFORCEINLINE void __CRTDECL operator delete (void *ptr, Toshi::TMemory::MemBlock *block) noexcept
 Placement delete operator for memory blocks.
 
TFORCEINLINE void __CRTDECL operator delete[] (void *ptr, Toshi::TMemory::MemBlock *block) noexcept
 Placement delete[] operator for memory blocks.
 

Variables

TOSHI_NAMESPACE_START class TMemoryg_pMemory
 

Detailed Description

Core memory management system for the Toshi engine.

This file implements a custom memory allocator that provides:

  • Memory block management with alignment support
  • Memory tracking and debugging capabilities
  • Support for both custom allocator and dlmalloc implementations

Definition in file TMemory.h.

Macro Definition Documentation

◆ TMEMORY_ALLOC_MEMNODE_SIZE

#define TMEMORY_ALLOC_MEMNODE_SIZE   sizeof( Toshi::TMemory::MemNode )

Size of memory node structure.

Definition at line 30 of file TMemory.h.

◆ TMEMORY_ALLOC_RESERVED_SIZE

#define TMEMORY_ALLOC_RESERVED_SIZE   ( TMEMORY_ALLOC_MEMNODE_SIZE - sizeof( void* ) )

Reserved space in allocations.

Definition at line 31 of file TMemory.h.

◆ TMEMORY_FLAGS_HOLE_PROCESS

#define TMEMORY_FLAGS_HOLE_PROCESS   1

Flag indicating a memory block is in use.

Definition at line 26 of file TMemory.h.

◆ TMEMORY_FLAGS_MASK

#define TMEMORY_FLAGS_MASK   ( ( 1 << 2 ) - 1 )

Mask for memory block flags.

Definition at line 27 of file TMemory.h.

◆ TMEMORY_NUM_BLOCK_SLOTS

#define TMEMORY_NUM_BLOCK_SLOTS   128

Maximum number of memory block slots.

Definition at line 29 of file TMemory.h.

◆ TMEMORY_NUM_FREELISTS

#define TMEMORY_NUM_FREELISTS   9

Number of free list buckets for different sizes.

Definition at line 28 of file TMemory.h.

◆ TMEMORY_ROUNDUP

#define TMEMORY_ROUNDUP   4

Minimum alignment for memory blocks.

Definition at line 25 of file TMemory.h.

Function Documentation

◆ operator delete()

TFORCEINLINE void __CRTDECL operator delete ( void * ptr,
Toshi::TMemory::MemBlock * block )
noexcept

Placement delete operator for memory blocks.

Parameters
ptrPointer to memory to free
blockMemory block (unused)

Definition at line 505 of file TMemory.h.

506{
507 TFree( ptr );
508}
void TFree(void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:1054

◆ operator delete[]()

TFORCEINLINE void __CRTDECL operator delete[] ( void * ptr,
Toshi::TMemory::MemBlock * block )
noexcept

Placement delete[] operator for memory blocks.

Parameters
ptrPointer to memory to free
blockMemory block (unused)

Definition at line 515 of file TMemory.h.

516{
517 TFree( ptr );
518}

◆ operator new()

TFORCEINLINE void *__CRTDECL operator new ( size_t size,
Toshi::TMemory::MemBlock * block )

Placement new operator for memory blocks.

Parameters
sizeSize to allocate
blockMemory block to allocate from
Returns
Pointer to allocated memory

Definition at line 476 of file TMemory.h.

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}
TOSHI_NAMESPACE_END void * TMalloc(TSIZE a_uiSize)
Allocates memory with default alignment.
Definition TMemory.cpp:1005
#define TNULL
Definition Typedefs.h:23

◆ operator new[]()

TFORCEINLINE void *__CRTDECL operator new[] ( size_t size,
Toshi::TMemory::MemBlock * block )

Placement new[] operator for memory blocks.

Parameters
sizeSize to allocate
blockMemory block to allocate from
Returns
Pointer to allocated memory

Definition at line 491 of file TMemory.h.

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}

◆ TConstruct()

template<class T, class... Args>
TFORCEINLINE T * TConstruct ( T * a_pMemory,
Args &&... args )

Constructs an object in place.

Template Parameters
TType of object to construct
ArgsTypes of constructor arguments
Parameters
a_pMemoryMemory location to construct in
argsConstructor arguments
Returns
Pointer to constructed object

Definition at line 431 of file TMemory.h.

432{
433 return new ( a_pMemory ) T( std::forward<Args>( args )... );
434}

◆ TConstructArray()

template<class T, class... Args>
TFORCEINLINE T * TConstructArray ( T * a_pMemory,
TSIZE a_uiNumTimes,
Args &&... args )

Constructs an array of objects in place.

Template Parameters
TType of objects to construct
ArgsTypes of constructor arguments
Parameters
a_pMemoryMemory location to construct in
a_uiNumTimesNumber of objects to construct
argsConstructor arguments
Returns
Pointer to first constructed object

Definition at line 446 of file TMemory.h.

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}
size_t TSIZE
Definition Typedefs.h:9

◆ TDestruct()

template<class T>
TFORCEINLINE void TDestruct ( T * a_pPtr)

Destructs an object.

Template Parameters
TType of object to destruct
Parameters
a_pPtrPointer to object to destruct

Definition at line 462 of file TMemory.h.

463{
464 if ( a_pPtr )
465 {
466 a_pPtr->~T();
467 }
468}

◆ TFree()

void TFree ( void * a_pMem)

Frees previously allocated memory.

Parameters
a_pMemPointer to memory to free

Definition at line 1054 of file TMemory.cpp.

1055{
1056 Toshi::g_pMemory->Free( a_pMem );
1057}
TMemory * g_pMemory
Definition TMemory.cpp:131

◆ TMalloc() [1/3]

TOSHI_NAMESPACE_END void * TMalloc ( TSIZE a_uiSize)

Allocates memory with default alignment.

Parameters
a_uiSizeSize of memory to allocate
Returns
Pointer to allocated memory

Definition at line 1005 of file TMemory.cpp.

1006{
1007 auto pMemBlock = Toshi::g_pMemory->GetGlobalBlock();
1008
1009 auto pMem = Toshi::g_pMemory->Alloc( a_uiSize, 16, pMemBlock, TNULL, -1 );
1010
1011 if ( !pMem )
1012 {
1013 Toshi::TMemory::DebugPrintHALMemInfo( "Out of Toshi Memory on block [%s]\n", pMemBlock->m_szName );
1014 }
1015
1016 return pMem;
1017}

◆ TMalloc() [2/3]

void * TMalloc ( TSIZE a_uiSize,
const TCHAR * a_szFileName,
TINT a_iLineNum )

Allocates memory with debug information.

Parameters
a_uiSizeSize of memory to allocate
a_szFileNameSource file name
a_iLineNumSource line number
Returns
Pointer to allocated memory

Definition at line 990 of file TMemory.cpp.

991{
992 auto pMemBlock = Toshi::g_pMemory->GetGlobalBlock();
993
994 auto pMem = Toshi::g_pMemory->Alloc( a_uiSize, 16, pMemBlock, a_szFileName, a_iLineNum );
995
996 if ( !pMem )
997 {
998 Toshi::TMemory::DebugPrintHALMemInfo( "Out of Toshi Memory on block [%s]\n", pMemBlock->m_szName );
999 }
1000
1001 return pMem;
1002}

◆ TMalloc() [3/3]

void * TMalloc ( TSIZE a_uiSize,
Toshi::TMemory::MemBlock * a_pMemBlock,
const TCHAR * a_szFileName = TNULL,
TINT a_iLineNum = -1 )

Allocates memory from a specific memory block.

Parameters
a_uiSizeSize of memory to allocate
a_pMemBlockMemory block to allocate from
a_szFileNameSource file name (optional)
a_iLineNumSource line number (optional)
Returns
Pointer to allocated memory

Definition at line 973 of file TMemory.cpp.

974{
975 if ( !a_pMemBlock )
976 {
977 a_pMemBlock = Toshi::g_pMemory->GetGlobalBlock();
978 }
979
980 auto pMem = Toshi::g_pMemory->Alloc( a_uiSize, 16, a_pMemBlock, a_szFileName, a_iLineNum );
981
982 if ( !pMem )
983 {
984 Toshi::TMemory::DebugPrintHALMemInfo( "Out of Toshi Memory on block [%s]\n", a_pMemBlock->m_szName );
985 }
986
987 return pMem;
988}

◆ TMemalign() [1/2]

void * TMemalign ( TSIZE a_uiAlignment,
TSIZE a_uiSize,
Toshi::TMemory::MemBlock * a_pMemBlock )

Allocates aligned memory from a specific memory block.

Parameters
a_uiAlignmentAlignment requirement
a_uiSizeSize of memory to allocate
a_pMemBlockMemory block to allocate from
Returns
Pointer to allocated memory

Definition at line 1020 of file TMemory.cpp.

1021{
1022 if ( !a_pMemBlock )
1023 {
1024 a_pMemBlock = Toshi::g_pMemory->GetGlobalBlock();
1025 }
1026
1027 auto pMem = Toshi::g_pMemory->Alloc( a_uiSize, a_uiAlignment, a_pMemBlock, TNULL, -1 );
1028
1029 if ( !pMem )
1030 {
1031 Toshi::TMemory::DebugPrintHALMemInfo( "Out of Toshi Memory on block [%s]\n", a_pMemBlock->m_szName );
1032 }
1033
1034 return pMem;
1035}

◆ TMemalign() [2/2]

void * TMemalign ( TSIZE a_uiSize,
TSIZE a_uiAlignment )

Allocates aligned memory.

Parameters
a_uiSizeSize of memory to allocate
a_uiAlignmentAlignment requirement
Returns
Pointer to allocated memory

Definition at line 1038 of file TMemory.cpp.

1039{
1040 auto pMemBlock = Toshi::g_pMemory->GetGlobalBlock();
1041
1042 auto pMem = Toshi::g_pMemory->Alloc( a_uiSize, a_uiAlignment, pMemBlock, TNULL, -1 );
1043
1044 if ( !pMem )
1045 {
1046 Toshi::TMemory::DebugPrintHALMemInfo( "Out of Toshi Memory on block [%s]\n", pMemBlock->m_szName );
1047 }
1048
1049 return pMem;
1050}

Variable Documentation

◆ g_pMemory

TOSHI_NAMESPACE_START class TMemory* g_pMemory
extern