OpenBarnyard
 
Loading...
Searching...
No Matches
TFreeList.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TFreeList.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12TFreeList::TFreeList( TUINT a_uiItemSize, TINT a_iInitialSize, TINT a_iGrowSize, const TCHAR* a_pchName )
13{
14 m_uiItemSize = a_uiItemSize;
15 m_iCapacity = 0;
16 m_pMemoryHeap = TNULL;
17 TASSERT( m_iGrowSize >= 0 );
18 TASSERT( a_iInitialSize >= 0 );
19 SetGrowSize( a_iGrowSize );
20
21 m_pPrevList = ms_pLastList;
22 ms_pLastList = this;
23}
24
26{
27 TASSERT( a_iNumber > 0 );
28 m_iCapacity += a_iNumber;
29
30 Node* pNewNode = TREINTERPRETCAST( Node*, TMalloc( a_iNumber * a_iSize + sizeof( Node ), m_pMemoryHeap ) );
31
32 pNewNode->pNext = m_RootNode.pNext;
33 m_RootNode.pNext = pNewNode;
34
35 auto pData = pNewNode + 1;
36 Node* pNext = TNULL;
37
38 for ( TINT i = a_iNumber - 1; i != 0; i-- )
39 {
40 pData->pNext = pNext;
41 pNext = pData;
42
43 pData = TREINTERPRETCAST( Node*, TREINTERPRETCAST( uintptr_t, pData ) + a_iSize );
44 }
45
46 m_LastNode.pNext = pNext;
47 return pData;
48}
49
50void TFreeList::SetCapacity( TINT a_iNewCapacity )
51{
52 if ( m_iCapacity < a_iNewCapacity )
53 {
54 auto pNode = Allocate( a_iNewCapacity - m_iCapacity, m_uiItemSize );
55
56 pNode->pNext = m_LastNode.pNext;
57 m_LastNode.pNext = pNode;
58 }
59}
60
61void* TFreeList::New( TUINT a_uiSize )
62{
63 if ( a_uiSize != m_uiItemSize )
64 {
65 return TMalloc( a_uiSize, TNULL );
66 }
67
68 auto pNode = m_LastNode.pNext;
69
70 if ( pNode != TNULL )
71 {
72 m_LastNode.pNext = pNode->pNext;
73 return pNode;
74 }
75 else
76 {
77 return Allocate( m_iGrowSize, a_uiSize );
78 }
79}
80
81void TFreeList::Delete( void* a_Ptr )
82{
83 Node* pNode = TSTATICCAST( Node, a_Ptr );
84
85 if ( m_LastNode.pNext != TNULL )
86 {
87 pNode->pNext = m_LastNode.pNext;
88 m_LastNode.pNext = pNode;
89 }
90 else
91 {
92 m_LastNode.pNext = pNode;
93 pNode->pNext = TNULL;
94 }
95}
96
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 TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TREINTERPRETCAST(TYPE, VALUE)
Definition Defines.h:68
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
Node * Allocate(TINT a_iNumber, TINT a_iSize)
Definition TFreeList.cpp:25
void Delete(void *a_Ptr)
Definition TFreeList.cpp:81
void SetCapacity(TINT a_iNewCapacity)
Definition TFreeList.cpp:50
TFreeList(TUINT a_uiItemSize, TINT a_iInitialSize, TINT a_iGrowSize, const TCHAR *a_pchName)
Definition TFreeList.cpp:12
void SetGrowSize(TINT a_iGrowSize)
Definition TFreeList.h:44
void * New(TUINT a_uiSize)
Definition TFreeList.cpp:61