Skip to content

Commit 3648be6

Browse files
committed
Revert "Replace MallocAllocator with SystemAllocator"
This reverts commit de2f8b5. # Conflicts: # Core/GameEngine/Include/Common/GameMemory.h # Core/GameEngine/Source/Common/System/GameMemoryCommon.cpp # Core/Libraries/Source/WWVegas/WWLib/DbgHelpLoader.cpp # Core/Libraries/Source/WWVegas/WWLib/MallocAllocator.h
1 parent b341518 commit 3648be6

File tree

4 files changed

+90
-143
lines changed

4 files changed

+90
-143
lines changed

Core/GameEngine/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ set(GAMEENGINE_SRC
651651
# Source/Common/System/FunctionLexicon.cpp
652652
# Source/Common/System/GameCommon.cpp
653653
#Source/Common/System/GameMemory.cpp # is conditionally appended
654-
Source/Common/System/GameMemoryCommon.cpp
655654
#Source/Common/System/GameMemoryInit.cpp # is conditionally appended
656655
# Source/Common/System/GameType.cpp
657656
# Source/Common/System/Geometry.cpp

Core/GameEngine/Include/Common/GameMemory.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,8 @@
177177

178178
};
179179

180-
#define USE_FILLER_VALUE
181-
182180
#endif // MEMORYPOOL_DEBUG
183181

184-
extern Int theTotalSystemAllocationInBytes;
185-
extern Int thePeakSystemAllocationInBytes;
186-
187-
// TheSuperHackers @tweak The system allocator functions are now global.
188-
// Use this to bypass Game Memory Pools in exceptional circumstances.
189-
extern void* sysAllocateDoNotZero(size_t numBytes);
190-
extern void sysFree(void* p);
191-
extern void memset32(void* ptr, Int value, size_t bytesToFill);
192-
193-
#ifdef USE_FILLER_VALUE
194-
extern UnsignedInt s_initFillerValue;
195-
extern void calcFillerValue(Int index);
196-
#endif
197-
198182
// TheSuperHackers @build xezon 30/03/2025 Define DISABLE_GAMEMEMORY to use a null implementations for Game Memory.
199183
// Useful for address sanitizer checks and other investigations.
200184
// Is included below the macros so that memory pool debug code can still be used.

Core/GameEngine/Source/Common/System/GameMemory.cpp

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,21 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
114114
#define MEMORYPOOL_SINGLEBLOCK_GETS_STACKTRACE
115115
#endif
116116

117+
#define USE_FILLER_VALUE
118+
117119
const Int MAX_INIT_FILLER_COUNT = 8;
120+
#ifdef USE_FILLER_VALUE
121+
static UnsignedInt s_initFillerValue = 0xf00dcafe; // will be replaced, should never be this value at runtime
122+
static void calcFillerValue(Int index)
123+
{
124+
s_initFillerValue = (index & 3) << 1;
125+
s_initFillerValue |= 0x01;
126+
s_initFillerValue |= (~(s_initFillerValue << 4)) & 0xf0;
127+
s_initFillerValue |= (s_initFillerValue << 8);
128+
s_initFillerValue |= (s_initFillerValue << 16);
129+
//DEBUG_LOG(("Setting MemoryPool initFillerValue to %08x (index %d)",s_initFillerValue,index));
130+
}
131+
#endif
118132

119133
#endif
120134

@@ -155,6 +169,8 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
155169

156170
#ifdef MEMORYPOOL_DEBUG
157171

172+
static Int theTotalSystemAllocationInBytes = 0;
173+
static Int thePeakSystemAllocationInBytes = 0;
158174
static Int theTotalLargeBlocks = 0;
159175
static Int thePeakLargeBlocks = 0;
160176
Int theTotalDMA = 0;
@@ -187,7 +203,9 @@ static Bool theMainInitFlag = false;
187203
#define MEM_BOUND_ALIGNMENT 4
188204

189205
static Int roundUpMemBound(Int i);
190-
206+
static void *sysAllocateDoNotZero(Int numBytes);
207+
static void sysFree(void* p);
208+
static void memset32(void* ptr, Int value, Int bytesToFill);
191209
#ifdef MEMORYPOOL_STACKTRACE
192210
static void doStackDumpOutput(const char* m);
193211
static void doStackDump(void **stacktrace, int size);
@@ -205,6 +223,73 @@ static Int roundUpMemBound(Int i)
205223
return (i + (MEM_BOUND_ALIGNMENT-1)) & ~(MEM_BOUND_ALIGNMENT-1);
206224
}
207225

226+
//-----------------------------------------------------------------------------
227+
/**
228+
this is the low-level allocator that we use to request memory from the OS.
229+
all (repeat, all) memory allocations in this module should ultimately
230+
go thru this routine (or sysAllocate).
231+
232+
note: throws ERROR_OUT_OF_MEMORY on failure; never returns null
233+
*/
234+
static void* sysAllocateDoNotZero(Int numBytes)
235+
{
236+
void* p = ::GlobalAlloc(GMEM_FIXED, numBytes);
237+
if (!p)
238+
throw ERROR_OUT_OF_MEMORY;
239+
#ifdef MEMORYPOOL_DEBUG
240+
{
241+
USE_PERF_TIMER(MemoryPoolDebugging)
242+
#ifdef USE_FILLER_VALUE
243+
{
244+
USE_PERF_TIMER(MemoryPoolInitFilling)
245+
::memset32(p, s_initFillerValue, ::GlobalSize(p));
246+
}
247+
#endif
248+
theTotalSystemAllocationInBytes += ::GlobalSize(p);
249+
if (thePeakSystemAllocationInBytes < theTotalSystemAllocationInBytes)
250+
thePeakSystemAllocationInBytes = theTotalSystemAllocationInBytes;
251+
}
252+
#endif
253+
return p;
254+
}
255+
256+
//-----------------------------------------------------------------------------
257+
/**
258+
the counterpart to sysAllocate / sysAllocateDoNotZero; used to free blocks
259+
allocated by them. it is OK to pass null here (it will just be ignored).
260+
*/
261+
static void sysFree(void* p)
262+
{
263+
if (p)
264+
{
265+
#ifdef MEMORYPOOL_DEBUG
266+
{
267+
USE_PERF_TIMER(MemoryPoolDebugging)
268+
::memset32(p, GARBAGE_FILL_VALUE, ::GlobalSize(p));
269+
theTotalSystemAllocationInBytes -= ::GlobalSize(p);
270+
}
271+
#endif
272+
::GlobalFree(p);
273+
}
274+
}
275+
276+
// ----------------------------------------------------------------------------
277+
/**
278+
fills memory with a 32-bit value (note: assumes the ptr is 4-byte-aligned)
279+
*/
280+
static void memset32(void* ptr, Int value, Int bytesToFill)
281+
{
282+
Int wordsToFill = bytesToFill>>2;
283+
bytesToFill -= (wordsToFill<<2);
284+
285+
Int *p = (Int*)ptr;
286+
for (++wordsToFill; --wordsToFill; )
287+
*p++ = value;
288+
289+
Byte *b = (Byte *)p;
290+
for (++bytesToFill; --bytesToFill; )
291+
*b++ = (Byte)value;
292+
}
208293

209294
#ifdef MEMORYPOOL_STACKTRACE
210295
// ----------------------------------------------------------------------------
@@ -1548,20 +1633,20 @@ void* MemoryPool::allocateBlockDoNotZeroImplementation(DECLARE_LITERALSTRING_ARG
15481633
if (m_firstBlobWithFreeBlocks != NULL && !m_firstBlobWithFreeBlocks->hasAnyFreeBlocks())
15491634
{
15501635
// hmm... the current 'free' blob has nothing available. look and see if there
1551-
// are any other existing blobs with free space.
1636+
// are any other existing blobs with freespace.
15521637
MemoryPoolBlob *blob = m_firstBlob;
15531638
for (; blob != NULL; blob = blob->getNextInList())
15541639
{
15551640
if (blob->hasAnyFreeBlocks())
15561641
break;
15571642
}
15581643

1559-
// note that if we walk through the list without finding anything, this will
1560-
// reset m_firstBlobWithFreeBlocks to null and fall through.
1644+
// note that if we walk thru the list without finding anything, this will
1645+
// reset m_firstBlobWithFreeBlocks to null and fall thru.
15611646
m_firstBlobWithFreeBlocks = blob;
15621647
}
15631648

1564-
// OK, if we are here then we have no blobs with free space... darn.
1649+
// OK, if we are here then we have no blobs with freespace... darn.
15651650
// allocate an overflow block.
15661651
if (m_firstBlobWithFreeBlocks == NULL)
15671652
{

Core/GameEngine/Source/Common/System/GameMemoryCommon.cpp

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)