OpenBarnyard
 
Loading...
Searching...
No Matches
TMemory_dlmalloc.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "dlmalloc.h"
3#include "TMemory_dlmalloc.h"
4
6
7#ifdef TMEMORY_USE_DLMALLOC
8# undef CreateMutex
9
10TMemoryDL* g_pMemoryDL = TNULL;
11
12void TMemoryDL::OutOfMem( TMemoryDLHeap* heap, TSIZE size )
13{
14 // 006fc7c0
15 TERROR( "=========================================================================" );
16 TERROR( "MEMORY ALLOCATION FAILED" );
17 TERROR( "------------------------" );
18 TERROR( "HEAP: {0}", heap->m_Name );
19 TERROR( "------------------------" );
20 TERROR( "failed to allocate {0} bytes (~{1}KB, ~{2}MB)", size, size >> 10, size >> 20 );
21 TERROR( "=========================================================================" );
22 // ...
23}
24
25void* TMemoryDL::dlheapmalloc( TMemoryDLHeap* heap, TSIZE size )
26{
27 // 006fc520
28 if ( heap->m_Flags & TMemoryHeapFlags_AllocAsPile )
29 {
30
31 return TMemoryDLHeap::AllocAsPile( heap, size, 4 );
32 }
33
34 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) AcquireMutex();
35
36 void* chunk = mspace_malloc( heap->m_MSpace, size );
37 if ( chunk == TNULL ) TMemoryDL::OutOfMem( heap, size );
38
39 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) ReleaseMutex();
40 return chunk;
41}
42
43void* TMemoryDL::dlheapcalloc( TMemoryDLHeap* heap, TSIZE nitems, TSIZE size )
44{
45 // 006fc580
46 if ( heap->m_Flags & TMemoryHeapFlags_AllocAsPile )
47 {
48 return TMemoryDLHeap::AllocAsPile( heap, nitems * size, 4 );
49 }
50
51 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) AcquireMutex();
52
53 void* chunk = mspace_calloc( heap->m_MSpace, nitems, size );
54 if ( chunk == TNULL ) TMemoryDL::OutOfMem( heap, size );
55
56 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) ReleaseMutex();
57 return chunk;
58}
59
60void* TMemoryDL::dlheapmemalign( TMemoryDLHeap* heap, TSIZE alignment, TSIZE size )
61{
62 // 006fc6f0
63 if ( heap->m_Flags & TMemoryHeapFlags_AllocAsPile )
64 {
65 return TMemoryDLHeap::AllocAsPile( heap, size, 4 );
66 }
67
68 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) AcquireMutex();
69
70 void* chunk = mspace_memalign( heap->m_MSpace, alignment, size );
71 if ( chunk == TNULL ) TMemoryDL::OutOfMem( heap, size );
72
73 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) ReleaseMutex();
74 return chunk;
75}
76
77void* TMemoryDL::dlheaprealloc( TMemoryDLHeap* heap, void* mem, TSIZE newsize )
78{
79 // 006fc5f0
80 TASSERT( ( heap->m_Flags & TMemoryHeapFlags_AllocAsPile ) == 0, "Cannot realloc on pile" );
81
82 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) AcquireMutex();
83
84 void* chunk = mspace_realloc( heap->m_MSpace, mem, newsize );
85 if ( chunk == TNULL ) TMemoryDL::OutOfMem( heap, newsize );
86
87 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) ReleaseMutex();
88 return chunk;
89}
90
91void TMemoryDL::dlheapfree( TMemoryDLHeap* heap, void* mem )
92{
93 TASSERT( ( heap->m_Flags & TMemoryHeapFlags_AllocAsPile ) == 0, "Cannot free pile memory" );
94
95 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) AcquireMutex();
96
97 mspace_free( heap->m_MSpace, mem );
98
99 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) ReleaseMutex();
100}
101
102void TMemoryDL::dlheapdestroy( TMemoryDLHeap* heap )
103{
104 if ( heap != TNULL )
105 {
106 if ( heap->m_Flags & TMemoryHeapFlags_UseMutex ) heap->DestroyMutex();
107 destroy_mspace( heap->m_MSpace );
108 heap->m_MSpace = TNULL;
109
110 if ( heap->m_SubHeapBuffer != TNULL )
111 {
112 GetContext().Free( heap->m_SubHeapBuffer );
113 heap->m_SubHeapBuffer = TNULL;
114 }
115 }
116}
117
118TMemoryDLHeap* TMemoryDL::dlheapcreatesubheap( TMemoryDLHeap* heap, TSIZE size, TMemoryDLHeapFlags flags, const char name[ HEAP_MAXNAME ] )
119{
120 // 006fc320
121 TMemoryDLHeap* subHeap = TNULL;
122 TSIZE subHeapSize = size + sizeof( TMemoryDLHeap );
123 void* mem = heap->Malloc( subHeapSize );
124
125 if ( mem != TNULL )
126 {
127 if ( flags & TMemoryHeapFlags_AllocAsPile )
128 {
129 subHeap = static_cast<TMemoryDLHeap*>( mem );
130
131 subHeap->m_pOwnerBlock = this;
132 subHeap->m_Flags = flags;
133 subHeap->m_MSpace = TNULL;
134 subHeap->m_PileData = reinterpret_cast<char*>( subHeap + 1 );
135 subHeap->m_PileSize = subHeapSize;
136 subHeap->SetName( name );
137
138 if ( flags & TMemoryHeapFlags_UseMutex )
139 {
140 subHeap->CreateMutex();
141 }
142 else
143 {
144 TUtil::MemSet( &subHeap->m_Mutex, 0, sizeof( subHeap->m_Mutex ) );
145 }
146 }
147 else
148 {
149 subHeap = TMemoryDL::CreateHeapInPlace( mem, subHeapSize, flags, name );
150 }
151
152
153 if ( subHeap != TNULL )
154 {
155 subHeap->m_SubHeapBuffer = static_cast<char*>( mem );
156 }
157 else
158 {
159 GetContext().Free( mem );
160 }
161 }
162
163 return subHeap;
164}
165
166TMemoryDLHeap* TMemoryDL::dlheapcreateinplace( void* ptr, TSIZE heapSize, TMemoryDLHeapFlags flags, const char name[ HEAP_MAXNAME ] )
167{
168 TASSERT( heapSize > 0, "Allocation size is zero" );
169 TASSERT( ( heapSize & 3 ) == 0, "Allocation size is not aligned to 4" );
170
171 TMemoryDLHeap* heap = static_cast<TMemoryDLHeap*>( ptr );
172 TUtil::MemClear( heap, sizeof( TMemoryDLHeap ) );
173
174 TSIZE capacity = heapSize - sizeof( TMemoryDLHeap );
175 heap->m_MSpace = create_mspace_with_base( heap + 1, capacity, 1 );
176
177 if ( heap->m_MSpace != TNULL )
178 {
179 heap->m_pOwnerBlock = this;
180 heap->m_Flags = flags;
181 heap->SetName( name );
182
183 if ( flags & TMemoryHeapFlags_UseMutex )
184 {
185 heap->CreateMutex();
186 }
187 else
188 {
189 TUtil::MemSet( &heap->m_Mutex, 0, sizeof( heap->m_Mutex ) );
190 }
191
192 return heap;
193 }
194 else
195 {
196 return TNULL;
197 }
198}
199
200void* TMemoryDLHeap::AllocAsPile( TMemoryDLHeap* heap, TSIZE size, TSIZE alignment )
201{
202 TASSERT( heap->m_Flags & TMemoryHeapFlags_AllocAsPile, "Can't allocate as pile on a non-pile heap" );
203
204 // Align current pointer
205 heap->m_PileData = reinterpret_cast<char*>( ( ( (TSIZE)heap->m_PileData + ( alignment - 1 ) ) / alignment ) * alignment );
206 TSIZE usedMemorySize = heap->m_PileData - heap->m_SubHeapBuffer;
207
208 if ( usedMemorySize + size > heap->m_PileSize )
209 {
210 TERROR( "Out of memory in pile '{0}' when trying to alloc {1} bytes", heap->m_Name, size );
211 TBREAK();
212 }
213
214 void* allocated = heap->m_PileData;
215 heap->m_PileData += size;
216
217 return allocated;
218}
219
220#endif // TMEMORY_USE_DLMALLOC
221
#define TASSERT(X,...)
Definition Defines.h:138
#define TERROR(...)
Definition Defines.h:153
#define TBREAK()
Definition Defines.h:64
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
#define TNULL
Definition Typedefs.h:23
static void MemClear(void *ptr, TSIZE size)
Definition TUtil.h:91
static void MemSet(void *ptr, TINT value, TSIZE size)
Definition TUtil.h:89