Skip to content

Commit 7f5f713

Browse files
committed
unify(memory): Merge GameMemory code (#1408)
1 parent 4ecef76 commit 7f5f713

File tree

6 files changed

+91
-41
lines changed

6 files changed

+91
-41
lines changed

Core/GameEngine/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,13 @@ set(GAMEENGINE_SRC
646646
# Source/Common/System/FunctionLexicon.cpp
647647
# Source/Common/System/GameCommon.cpp
648648
#Source/Common/System/GameMemory.cpp # is conditionally appended
649+
#Source/Common/System/GameMemoryInit.cpp # is conditionally appended
649650
# Source/Common/System/GameType.cpp
650651
# Source/Common/System/Geometry.cpp
651652
# Source/Common/System/KindOf.cpp
652653
# Source/Common/System/List.cpp
653654
Source/Common/System/LocalFile.cpp
654655
Source/Common/System/LocalFileSystem.cpp
655-
#Source/Common/System/MemoryInit.cpp # is conditionally appended
656656
# Source/Common/System/ObjectStatusTypes.cpp
657657
# Source/Common/System/QuotedPrintable.cpp
658658
# Source/Common/System/Radar.cpp
@@ -1140,7 +1140,7 @@ if(RTS_GAMEMEMORY_ENABLE)
11401140
# Uses the original Game Memory implementation.
11411141
list(APPEND GAMEENGINE_SRC
11421142
# Source/Common/System/GameMemory.cpp
1143-
# Source/Common/System/MemoryInit.cpp
1143+
# Source/Common/System/GameMemoryInit.cpp
11441144
)
11451145
else()
11461146
# Uses the null implementation when disabled.

Core/GameEngine/Include/Common/GameDefines.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@
5959
#ifndef ENABLE_FILESYSTEM_EXISTENCE_CACHE
6060
#define ENABLE_FILESYSTEM_EXISTENCE_CACHE (1)
6161
#endif
62+
63+
// Enable obsolete code. This mainly refers to code that existed in Generals but was removed in GeneralsMD.
64+
// Disable and remove this when Generals and GeneralsMD are merged.
65+
#if RTS_GENERALS
66+
#ifndef USE_OBSOLETE_GENERALS_CODE
67+
#define USE_OBSOLETE_GENERALS_CODE (1)
68+
#endif
69+
#endif

Generals/Code/GameEngine/Include/Common/GameMemory.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ private: \
730730
public: /* include this line at the end to reset visibility to 'public' */
731731

732732

733-
734733
// ----------------------------------------------------------------------------
735734
/**
736735
This class is provided as a simple and safe way to integrate C++ object allocation
@@ -894,10 +893,9 @@ class STLSpecialAlloc
894893
static void deallocate(void* __p, size_t);
895894
};
896895

897-
#endif //DISABLE_GAMEMEMORY
896+
#endif // DISABLE_GAMEMEMORY
898897

899898

900-
// ----------------------------------------------------------------------------
901899
/**
902900
A simple utility class to ensure exception safety; this holds a MemoryPoolObject
903901
and deletes it in its destructor. Especially useful for iterators!
@@ -916,4 +914,5 @@ class MemoryPoolObjectHolder
916914

917915
#define EMPTY_DTOR(CLASS) inline CLASS::~CLASS() { }
918916

917+
919918
#endif // _GAME_MEMORY_H_

Generals/Code/GameEngine/Source/Common/System/GameMemory.cpp

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
7575
faster to free raw DMA blocks.
7676
@todo verify this speedup is enough to be worth the extra space
7777
*/
78-
#define MPSB_DLINK
78+
#ifndef DISABLE_MEMORYPOOL_MPSB_DLINK
79+
#define MPSB_DLINK
80+
#endif
7981

8082
#ifdef MEMORYPOOL_DEBUG
8183

@@ -201,7 +203,6 @@ static Bool theMainInitFlag = false;
201203
#define MEM_BOUND_ALIGNMENT 4
202204

203205
static Int roundUpMemBound(Int i);
204-
static void *sysAllocate(Int numBytes);
205206
static void *sysAllocateDoNotZero(Int numBytes);
206207
static void sysFree(void* p);
207208
static void memset32(void* ptr, Int value, Int bytesToFill);
@@ -222,27 +223,6 @@ static Int roundUpMemBound(Int i)
222223
return (i + (MEM_BOUND_ALIGNMENT-1)) & ~(MEM_BOUND_ALIGNMENT-1);
223224
}
224225

225-
//-----------------------------------------------------------------------------
226-
/**
227-
identical to sysAllocateDoNotZero, except that the memory block returned
228-
is filled to all-zero-bytes.
229-
*/
230-
static void* sysAllocate(Int numBytes)
231-
{
232-
void* p = ::GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, numBytes);
233-
if (!p)
234-
throw ERROR_OUT_OF_MEMORY;
235-
#ifdef MEMORYPOOL_DEBUG
236-
{
237-
USE_PERF_TIMER(MemoryPoolDebugging)
238-
theTotalSystemAllocationInBytes += ::GlobalSize(p);
239-
if (thePeakSystemAllocationInBytes < theTotalSystemAllocationInBytes)
240-
thePeakSystemAllocationInBytes = theTotalSystemAllocationInBytes;
241-
}
242-
#endif
243-
return p;
244-
}
245-
246226
//-----------------------------------------------------------------------------
247227
/**
248228
this is the low-level allocator that we use to request memory from the OS.
@@ -837,7 +817,7 @@ Bool BlockCheckpointInfo::shouldBeInReport(Int flags, Int startCheckpoint, Int e
837817

838818
BlockCheckpointInfo *freed = NULL;
839819
try {
840-
freed = (BlockCheckpointInfo *)::sysAllocate(sizeof(BlockCheckpointInfo));
820+
freed = (BlockCheckpointInfo *)::sysAllocateDoNotZero(sizeof(BlockCheckpointInfo));
841821
} catch (...) {
842822
freed = NULL;
843823
}
@@ -896,6 +876,7 @@ void MemoryPoolSingleBlock::initBlock(Int logicalSize, MemoryPoolBlob *owningBlo
896876
#endif
897877
}
898878
#endif // MEMORYPOOL_DEBUG
879+
899880
#ifdef MEMORYPOOL_CHECKPOINTING
900881
m_checkpointInfo = NULL;
901882
#endif
@@ -1204,7 +1185,7 @@ void MemoryPoolBlob::initBlob(MemoryPool *owningPool, Int allocationCount)
12041185
m_usedBlocksInBlob = 0;
12051186

12061187
Int rawBlockSize = MemoryPoolSingleBlock::calcRawBlockSize(m_owningPool->getAllocationSize());
1207-
m_blockData = (char *)::sysAllocate(rawBlockSize * m_totalBlocksInBlob); // throws on failure
1188+
m_blockData = (char *)::sysAllocateDoNotZero(rawBlockSize * m_totalBlocksInBlob); // throws on failure
12081189

12091190
// set up the list of free blocks in the blob (namely, all of 'em)
12101191
MemoryPoolSingleBlock *block = (MemoryPoolSingleBlock *)m_blockData;
@@ -1577,7 +1558,7 @@ MemoryPoolBlob* MemoryPool::createBlob(Int allocationCount)
15771558
{
15781559
DEBUG_ASSERTCRASH(allocationCount > 0 && allocationCount%MEM_BOUND_ALIGNMENT==0, ("bad allocationCount (must be >0 and evenly divisible by %d)",MEM_BOUND_ALIGNMENT));
15791560

1580-
MemoryPoolBlob* blob = new (::sysAllocate(sizeof(MemoryPoolBlob))) MemoryPoolBlob; // will throw on failure
1561+
MemoryPoolBlob* blob = new (::sysAllocateDoNotZero(sizeof(MemoryPoolBlob))) MemoryPoolBlob; // will throw on failure
15811562

15821563
blob->initBlob(this, allocationCount); // will throw on failure
15831564

@@ -2175,6 +2156,8 @@ void DynamicMemoryAllocator::debugIgnoreLeaksForThisBlock(void* pBlockPtr)
21752156
allocate a chunk-o-bytes from this DMA and return it, but don't bother zeroing
21762157
out the block. if unable to allocate, throw ERROR_OUT_OF_MEMORY. this
21772158
function will never return null.
2159+
2160+
added code to make sure we're on a DWord boundary, throw exception if not
21782161
*/
21792162
void *DynamicMemoryAllocator::allocateBytesDoNotZeroImplementation(Int numBytes DECLARE_LITERALSTRING_ARG2)
21802163
{
@@ -2262,6 +2245,13 @@ void *DynamicMemoryAllocator::allocateBytesDoNotZeroImplementation(Int numBytes
22622245
}
22632246
#endif
22642247
#endif
2248+
2249+
#if defined(RTS_DEBUG)
2250+
// check alignment
2251+
if (unsigned(result)&3)
2252+
throw ERROR_OUT_OF_MEMORY;
2253+
#endif
2254+
22652255
return result;
22662256
}
22672257

@@ -2673,7 +2663,7 @@ MemoryPool *MemoryPoolFactory::createMemoryPool(const char *poolName, Int alloca
26732663
throw ERROR_OUT_OF_MEMORY;
26742664
}
26752665

2676-
pool = new (::sysAllocate(sizeof(MemoryPool))) MemoryPool; // will throw on failure
2666+
pool = new (::sysAllocateDoNotZero(sizeof(MemoryPool))) MemoryPool; // will throw on failure
26772667
pool->init(this, poolName, allocationSize, initialAllocationCount, overflowAllocationCount); // will throw on failure
26782668

26792669
pool->addToList(&m_firstPoolInFactory);
@@ -2689,8 +2679,7 @@ MemoryPool *MemoryPoolFactory::createMemoryPool(const char *poolName, Int alloca
26892679
*/
26902680
MemoryPool *MemoryPoolFactory::findMemoryPool(const char *poolName)
26912681
{
2692-
MemoryPool *pool = m_firstPoolInFactory;
2693-
for (; pool; pool = pool->getNextPoolInList())
2682+
for (MemoryPool *pool = m_firstPoolInFactory; pool; pool = pool->getNextPoolInList())
26942683
{
26952684
if (!strcmp(poolName, pool->getPoolName()))
26962685
{
@@ -2729,7 +2718,7 @@ DynamicMemoryAllocator *MemoryPoolFactory::createDynamicMemoryAllocator(Int numS
27292718
{
27302719
DynamicMemoryAllocator *dma;
27312720

2732-
dma = new (::sysAllocate(sizeof(DynamicMemoryAllocator))) DynamicMemoryAllocator; // will throw on failure
2721+
dma = new (::sysAllocateDoNotZero(sizeof(DynamicMemoryAllocator))) DynamicMemoryAllocator; // will throw on failure
27332722
dma->init(this, numSubPools, pParms); // will throw on failure
27342723

27352724
dma->addToList(&m_firstDmaInFactory);
@@ -3424,7 +3413,7 @@ void initMemoryManager()
34243413
Int numSubPools;
34253414
const PoolInitRec *pParms;
34263415
userMemoryManagerGetDmaParms(&numSubPools, &pParms);
3427-
TheMemoryPoolFactory = new (::sysAllocate(sizeof(MemoryPoolFactory))) MemoryPoolFactory; // will throw on failure
3416+
TheMemoryPoolFactory = new (::sysAllocateDoNotZero(sizeof(MemoryPoolFactory))) MemoryPoolFactory; // will throw on failure
34283417
TheMemoryPoolFactory->init(); // will throw on failure
34293418
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure
34303419
userMemoryManagerInitPools();
@@ -3499,7 +3488,7 @@ static void preMainInitMemoryManager()
34993488
Int numSubPools;
35003489
const PoolInitRec *pParms;
35013490
userMemoryManagerGetDmaParms(&numSubPools, &pParms);
3502-
TheMemoryPoolFactory = new (::sysAllocate(sizeof(MemoryPoolFactory))) MemoryPoolFactory; // will throw on failure
3491+
TheMemoryPoolFactory = new (::sysAllocateDoNotZero(sizeof(MemoryPoolFactory))) MemoryPoolFactory; // will throw on failure
35033492
TheMemoryPoolFactory->init(); // will throw on failure
35043493

35053494
TheDynamicMemoryAllocator = TheMemoryPoolFactory->createDynamicMemoryAllocator(numSubPools, pParms); // will throw on failure

0 commit comments

Comments
 (0)