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

Implementation of the Toshi memory management system. More...

#include "ToshiPCH.h"
#include "TMemory.h"
#include "Thread/TMutex.h"
#include "Thread/TMutexLock.h"
#include "TMemoryDebugOff.h"

Go to the source code of this file.

Namespaces

namespace  Toshi
 

Macros

#define MEM_TO_NODE(PTR)
 
#define PP_CAT(A, B)
 
#define PP_EXPAND(...)
 
#define PP_VA_ARG_SIZE(...)
 
#define PP_ZERO_ARGS_DETECT(...)
 
#define PP_ZERO_ARGS_DETECT_PREFIX__ZERO_ARGS_DETECT_SUFFIX   , , , , , , , , , , , 0
 
#define PP_APPLY_ARG_N(ARGS)
 
#define PP_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, N, ...)
 
#define PP_RSEQ_N   14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
 
#define PP_OVERLOAD_SELECT(NAME, NUM)
 
#define PP_MACRO_OVERLOAD(NAME, ...)
 
#define CALL_THIS_4(ADDR, TYPE, RET_TYPE, THIS)
 
#define CALL_THIS_6(ADDR, TYPE, RET_TYPE, THIS, TYPE1, VALUE1)
 
#define CALL_THIS_8(ADDR, TYPE, RET_TYPE, THIS, TYPE1, VALUE1, TYPE2, VALUE2)
 
#define CALL_THIS_10(ADDR, TYPE, RET_TYPE, THIS, TYPE1, VALUE1, TYPE2, VALUE2, TYPE3, VALUE3)
 
#define CALL_THIS_12(ADDR, TYPE, RET_TYPE, THIS, TYPE1, VALUE1, TYPE2, VALUE2, TYPE3, VALUE3, TYPE4, VALUE4)
 
#define CALL_THIS_14(ADDR, TYPE, RET_TYPE, THIS, TYPE1, VALUE1, TYPE2, VALUE2, TYPE3, VALUE3, TYPE4, VALUE4, TYPE5, VALUE5)
 
#define CALL_THIS(...)
 
#define CALL_2(ADDR, RET_TYPE)
 
#define CALL_4(ADDR, RET_TYPE, TYPE1, VALUE1)
 
#define CALL_6(ADDR, RET_TYPE, TYPE1, VALUE1, TYPE2, VALUE2)
 
#define CALL_8(ADDR, RET_TYPE, TYPE1, VALUE1, TYPE2, VALUE2, TYPE3, VALUE3)
 
#define CALL_10(ADDR, RET_TYPE, TYPE1, VALUE1, TYPE2, VALUE2, TYPE3, VALUE3, TYPE4, VALUE4)
 
#define CALL_12(ADDR, RET_TYPE, TYPE1, VALUE1, TYPE2, VALUE2, TYPE3, VALUE3, TYPE4, VALUE4, TYPE5, VALUE5)
 
#define CALL(...)
 

Functions

void *__CRTDECL operator new (TSIZE size)
 Global new operator implementation.
 
void *__CRTDECL operator new (TSIZE size, ::std::nothrow_t const &) noexcept
 Global new operator with nothrow implementation.
 
void *__CRTDECL operator new[] (TSIZE size)
 Global new[] operator implementation.
 
void *__CRTDECL operator new[] (TSIZE size, ::std::nothrow_t const &) noexcept
 Global new[] operator with nothrow implementation.
 
void __CRTDECL operator delete (void *ptr) noexcept
 Global delete operator implementation.
 
void __CRTDECL operator delete (void *ptr, ::std::nothrow_t const &) noexcept
 Global delete operator with nothrow implementation.
 
void __CRTDECL operator delete[] (void *ptr) noexcept
 Global delete[] operator implementation.
 
void __CRTDECL operator delete[] (void *ptr, ::std::nothrow_t const &) noexcept
 Global delete[] operator with nothrow implementation.
 
void __CRTDECL operator delete[] (void *ptr, TSIZE _Size) noexcept
 Global delete[] operator with size implementation.
 
void * TMalloc (TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock, const TCHAR *a_szFileName, TINT a_iLineNum)
 Allocates memory from a specific memory block.
 
void * TMalloc (TSIZE a_uiSize, const TCHAR *a_szFileName, TINT a_iLineNum)
 Allocates memory with debug information.
 
void * TMalloc (TSIZE a_uiSize)
 Allocates memory with default alignment.
 
void * TMemalign (TSIZE a_uiAlignment, TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock)
 Allocates aligned memory from a specific memory block.
 
void * TMemalign (TSIZE a_uiSize, TSIZE a_uiAlignment)
 Allocates aligned memory.
 
void TFree (void *a_pMem)
 Frees previously allocated memory.
 

Variables

TMemoryToshi::g_pMemory = nullptr
 

Detailed Description

Implementation of the Toshi memory management system.

This file implements the core memory management functionality including:

  • Memory allocation and deallocation
  • Memory block management
  • Memory tracking and debugging
  • System memory operations

Definition in file TMemory.cpp.

Macro Definition Documentation

◆ CALL

#define CALL ( ...)
Value:
PP_MACRO_OVERLOAD( CALL, __VA_ARGS__ )
#define PP_MACRO_OVERLOAD(NAME,...)
Definition TMemory.cpp:178
#define CALL(...)
Definition TMemory.cpp:194

Definition at line 194 of file TMemory.cpp.

◆ CALL_10

#define CALL_10 ( ADDR,
RET_TYPE,
TYPE1,
VALUE1,
TYPE2,
VALUE2,
TYPE3,
VALUE3,
TYPE4,
VALUE4 )
Value:
( ( RET_TYPE( __stdcall* )( TYPE1, TYPE2, TYPE3, TYPE4 ) )( ADDR ) )( VALUE1, VALUE2, VALUE3, VALUE4 )

Definition at line 192 of file TMemory.cpp.

◆ CALL_12

#define CALL_12 ( ADDR,
RET_TYPE,
TYPE1,
VALUE1,
TYPE2,
VALUE2,
TYPE3,
VALUE3,
TYPE4,
VALUE4,
TYPE5,
VALUE5 )
Value:
( ( RET_TYPE( __stdcall* )( TYPE1, TYPE2, TYPE3, TYPE4, TYPE5 ) )( ADDR ) )( VALUE1, VALUE2, VALUE3, VALUE4, VALUE5 )

Definition at line 193 of file TMemory.cpp.

◆ CALL_2

#define CALL_2 ( ADDR,
RET_TYPE )
Value:
( ( RET_TYPE( __stdcall* )() )( ADDR ) )()

Definition at line 188 of file TMemory.cpp.

◆ CALL_4

#define CALL_4 ( ADDR,
RET_TYPE,
TYPE1,
VALUE1 )
Value:
( ( RET_TYPE( __stdcall* )( TYPE1 ) )( ADDR ) )( VALUE1 )

Definition at line 189 of file TMemory.cpp.

◆ CALL_6

#define CALL_6 ( ADDR,
RET_TYPE,
TYPE1,
VALUE1,
TYPE2,
VALUE2 )
Value:
( ( RET_TYPE( __stdcall* )( TYPE1, TYPE2 ) )( ADDR ) )( VALUE1, VALUE2 )

Definition at line 190 of file TMemory.cpp.

◆ CALL_8

#define CALL_8 ( ADDR,
RET_TYPE,
TYPE1,
VALUE1,
TYPE2,
VALUE2,
TYPE3,
VALUE3 )
Value:
( ( RET_TYPE( __stdcall* )( TYPE1, TYPE2, TYPE3 ) )( ADDR ) )( VALUE1, VALUE2, VALUE3 )

Definition at line 191 of file TMemory.cpp.

◆ CALL_THIS

#define CALL_THIS ( ...)
Value:
#define CALL_THIS(...)
Definition TMemory.cpp:186

Definition at line 186 of file TMemory.cpp.

◆ CALL_THIS_10

#define CALL_THIS_10 ( ADDR,
TYPE,
RET_TYPE,
THIS,
TYPE1,
VALUE1,
TYPE2,
VALUE2,
TYPE3,
VALUE3 )
Value:
( ( RET_TYPE( __thiscall* )( TYPE, TYPE1, TYPE2, TYPE3 ) )( ADDR ) )( THIS, VALUE1, VALUE2, VALUE3 )

Definition at line 183 of file TMemory.cpp.

◆ CALL_THIS_12

#define CALL_THIS_12 ( ADDR,
TYPE,
RET_TYPE,
THIS,
TYPE1,
VALUE1,
TYPE2,
VALUE2,
TYPE3,
VALUE3,
TYPE4,
VALUE4 )
Value:
( ( RET_TYPE( __thiscall* )( TYPE, TYPE1, TYPE2, TYPE3, TYPE4 ) )( ADDR ) )( THIS, VALUE1, VALUE2, VALUE3, VALUE4 )

Definition at line 184 of file TMemory.cpp.

◆ CALL_THIS_14

#define CALL_THIS_14 ( ADDR,
TYPE,
RET_TYPE,
THIS,
TYPE1,
VALUE1,
TYPE2,
VALUE2,
TYPE3,
VALUE3,
TYPE4,
VALUE4,
TYPE5,
VALUE5 )
Value:
( ( RET_TYPE( __thiscall* )( TYPE, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5 ) )( ADDR ) )( THIS, VALUE1, VALUE2, VALUE3, VALUE4, VALUE5 )

Definition at line 185 of file TMemory.cpp.

◆ CALL_THIS_4

#define CALL_THIS_4 ( ADDR,
TYPE,
RET_TYPE,
THIS )
Value:
( ( RET_TYPE( __thiscall* )( TYPE pThis ) )( ADDR ) )( THIS )

Definition at line 180 of file TMemory.cpp.

◆ CALL_THIS_6

#define CALL_THIS_6 ( ADDR,
TYPE,
RET_TYPE,
THIS,
TYPE1,
VALUE1 )
Value:
( ( RET_TYPE( __thiscall* )( TYPE, TYPE1 ) )( ADDR ) )( THIS, VALUE1 )

Definition at line 181 of file TMemory.cpp.

◆ CALL_THIS_8

#define CALL_THIS_8 ( ADDR,
TYPE,
RET_TYPE,
THIS,
TYPE1,
VALUE1,
TYPE2,
VALUE2 )
Value:
( ( RET_TYPE( __thiscall* )( TYPE, TYPE1, TYPE2 ) )( ADDR ) )( THIS, VALUE1, VALUE2 )

Definition at line 182 of file TMemory.cpp.

◆ MEM_TO_NODE

#define MEM_TO_NODE ( PTR)
Value:
( ( (MemNode*)( ( (TUINTPTR)PTR ) + sizeof( void* ) ) ) - 1 )
uintptr_t TUINTPTR
Definition Typedefs.h:18

Definition at line 127 of file TMemory.cpp.

◆ PP_APPLY_ARG_N

#define PP_APPLY_ARG_N ( ARGS)
Value:
#define PP_EXPAND(...)
Definition TMemory.cpp:165
#define PP_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, N,...)
Definition TMemory.cpp:174

Definition at line 173 of file TMemory.cpp.

◆ PP_ARG_N

#define PP_ARG_N ( _0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
_10,
_11,
_12,
_13,
N,
... )
Value:
N

Definition at line 174 of file TMemory.cpp.

◆ PP_CAT

#define PP_CAT ( A,
B )
Value:
A##B

Definition at line 164 of file TMemory.cpp.

◆ PP_EXPAND

#define PP_EXPAND ( ...)
Value:
__VA_ARGS__

Definition at line 165 of file TMemory.cpp.

◆ PP_MACRO_OVERLOAD

#define PP_MACRO_OVERLOAD ( NAME,
... )
Value:
PP_OVERLOAD_SELECT( NAME, PP_VA_ARG_SIZE( __VA_ARGS__ ) )( __VA_ARGS__ )
#define PP_VA_ARG_SIZE(...)
Definition TMemory.cpp:168
#define PP_OVERLOAD_SELECT(NAME, NUM)
Definition TMemory.cpp:177

Definition at line 178 of file TMemory.cpp.

◆ PP_OVERLOAD_SELECT

#define PP_OVERLOAD_SELECT ( NAME,
NUM )
Value:
PP_CAT( NAME##_, NUM )
#define PP_CAT(A, B)
Definition TMemory.cpp:164

Definition at line 177 of file TMemory.cpp.

◆ PP_RSEQ_N

#define PP_RSEQ_N   14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0

Definition at line 175 of file TMemory.cpp.

◆ PP_VA_ARG_SIZE

#define PP_VA_ARG_SIZE ( ...)
Value:
#define PP_APPLY_ARG_N(ARGS)
Definition TMemory.cpp:173
#define PP_RSEQ_N
Definition TMemory.cpp:175
#define PP_ZERO_ARGS_DETECT(...)
Definition TMemory.cpp:170

Definition at line 168 of file TMemory.cpp.

◆ PP_ZERO_ARGS_DETECT

#define PP_ZERO_ARGS_DETECT ( ...)
Value:
PP_EXPAND( PP_ZERO_ARGS_DETECT_PREFIX_##__VA_ARGS__##_ZERO_ARGS_DETECT_SUFFIX )

Definition at line 170 of file TMemory.cpp.

◆ PP_ZERO_ARGS_DETECT_PREFIX__ZERO_ARGS_DETECT_SUFFIX

#define PP_ZERO_ARGS_DETECT_PREFIX__ZERO_ARGS_DETECT_SUFFIX   , , , , , , , , , , , 0

Definition at line 171 of file TMemory.cpp.

Function Documentation

◆ operator delete() [1/2]

void __CRTDECL operator delete ( void * ptr)
noexcept

Global delete operator implementation.

Parameters
ptrPointer to memory to free

Definition at line 83 of file TMemory.cpp.

84{
85 TFree( ptr );
86}
void TFree(void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:1054

◆ operator delete() [2/2]

void __CRTDECL operator delete ( void * ptr,
::std::nothrow_t const &  )
noexcept

Global delete operator with nothrow implementation.

Parameters
ptrPointer to memory to free

Definition at line 92 of file TMemory.cpp.

93{
94 TFree( ptr );
95}

◆ operator delete[]() [1/3]

void __CRTDECL operator delete[] ( void * ptr)
noexcept

Global delete[] operator implementation.

Parameters
ptrPointer to memory to free

Definition at line 101 of file TMemory.cpp.

102{
103 TFree( ptr );
104}

◆ operator delete[]() [2/3]

void __CRTDECL operator delete[] ( void * ptr,
::std::nothrow_t const &  )
noexcept

Global delete[] operator with nothrow implementation.

Parameters
ptrPointer to memory to free

Definition at line 110 of file TMemory.cpp.

111{
112 TFree( ptr );
113}

◆ operator delete[]() [3/3]

void __CRTDECL operator delete[] ( void * ptr,
TSIZE _Size )
noexcept

Global delete[] operator with size implementation.

Parameters
ptrPointer to memory to free
_SizeSize of memory (unused)

Definition at line 120 of file TMemory.cpp.

121{
122 TFree( ptr );
123}

◆ operator new() [1/2]

void *__CRTDECL operator new ( TSIZE size)

Global new operator implementation.

Parameters
sizeSize to allocate
Returns
Pointer to allocated memory

Definition at line 28 of file TMemory.cpp.

29{
30#ifdef TOSHI_PROFILER_MEMORY
31 return TMalloc( size, TNULL, TMemory__FILE__, TMemory__LINE__ );
32#else
33 return TMalloc( size, TNULL, TNULL, -1 );
34#endif // TOSHI_PROFILER_MEMORY
35}
void * TMalloc(TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock, const TCHAR *a_szFileName, TINT a_iLineNum)
Allocates memory from a specific memory block.
Definition TMemory.cpp:973
#define TNULL
Definition Typedefs.h:23

◆ operator new() [2/2]

void *__CRTDECL operator new ( TSIZE size,
::std::nothrow_t const &  )
noexcept

Global new operator with nothrow implementation.

Parameters
sizeSize to allocate
Returns
Pointer to allocated memory

Definition at line 42 of file TMemory.cpp.

43{
44#ifdef TOSHI_PROFILER_MEMORY
45 return TMalloc( size, TNULL, TMemory__FILE__, TMemory__LINE__ );
46#else
47 return TMalloc( size, TNULL, TNULL, -1 );
48#endif // TOSHI_PROFILER_MEMORY
49}

◆ operator new[]() [1/2]

void *__CRTDECL operator new[] ( TSIZE size)

Global new[] operator implementation.

Parameters
sizeSize to allocate
Returns
Pointer to allocated memory

Definition at line 56 of file TMemory.cpp.

57{
58#ifdef TOSHI_PROFILER_MEMORY
59 return TMalloc( size, TNULL, TMemory__FILE__, TMemory__LINE__ );
60#else
61 return TMalloc( size, TNULL, TNULL, -1 );
62#endif // TOSHI_PROFILER_MEMORY
63}

◆ operator new[]() [2/2]

void *__CRTDECL operator new[] ( TSIZE size,
::std::nothrow_t const &  )
noexcept

Global new[] operator with nothrow implementation.

Parameters
sizeSize to allocate
Returns
Pointer to allocated memory

Definition at line 70 of file TMemory.cpp.

71{
72#ifdef TOSHI_PROFILER_MEMORY
73 return TMalloc( size, TNULL, TMemory__FILE__, TMemory__LINE__ );
74#else
75 return TMalloc( size, TNULL, TNULL, -1 );
76#endif // TOSHI_PROFILER_MEMORY
77}

◆ 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]

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}