Skip to content

Commit e1626c6

Browse files
committed
[GEN][ZH] Add cmake configurability for MemoryPool features and debugs, disable slow stack trace (TheSuperHackers#542)
1 parent 202b4a6 commit e1626c6

File tree

5 files changed

+102
-11
lines changed

5 files changed

+102
-11
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
#define _GAME_MEMORY_H_
5050

5151
// Turn off memory pool checkpointing for now.
52-
#define DISABLE_MEMORYPOOL_CHECKPOINTING 1
52+
#ifndef DISABLE_MEMORYPOOL_CHECKPOINTING
53+
#define DISABLE_MEMORYPOOL_CHECKPOINTING 1
54+
#endif
5355

5456
#if (defined(_DEBUG) || defined(_INTERNAL)) && !defined(MEMORYPOOL_DEBUG_CUSTOM_NEW) && !defined(DISABLE_MEMORYPOOL_DEBUG_CUSTOM_NEW)
5557
#define MEMORYPOOL_DEBUG_CUSTOM_NEW
@@ -79,7 +81,7 @@
7981
#ifdef MEMORYPOOL_DEBUG
8082

8183
// by default, enable free-block-retention for checkpointing in debug mode
82-
#ifndef DISABLE_MEMORYPOOL_CHECKPOINTING
84+
#if !defined(DISABLE_MEMORYPOOL_CHECKPOINTING) || DISABLE_MEMORYPOOL_CHECKPOINTING == 0
8385
#define MEMORYPOOL_CHECKPOINTING
8486
#endif
8587

@@ -127,7 +129,7 @@
127129
// ------------------------------------------------------
128130
#endif // MEMORYPOOL_CHECKPOINTING
129131

130-
#ifdef MEMORYPOOL_STACKTRACE
132+
#ifdef MEMORYPOOL_CHECKPOINTING
131133
/** display the stacktrace for allocation location for all blocks found.
132134
this bit may be mixed-n-matched with any other flag.
133135
*/

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
181181
Int theWastedDMA = 0;
182182
Int thePeakWastedDMA = 0;
183183

184-
#define NO_INTENSE_DMA_BOOKKEEPING
185184
#ifdef INTENSE_DMA_BOOKKEEPING
186185
struct UsedNPeak
187186
{
@@ -2298,6 +2297,9 @@ void DynamicMemoryAllocator::freeBytes(void* pBlockPtr)
22982297
MemoryPoolSingleBlock *block = MemoryPoolSingleBlock::recoverBlockFromUserData(pBlockPtr);
22992298
#ifdef MEMORYPOOL_DEBUG
23002299
Int waste = 0, used = 0;
2300+
#ifdef INTENSE_DMA_BOOKKEEPING
2301+
const char* tagString;
2302+
#endif
23012303
{
23022304
USE_PERF_TIMER(MemoryPoolDebugging)
23032305
waste = 0;
@@ -2306,7 +2308,7 @@ void DynamicMemoryAllocator::freeBytes(void* pBlockPtr)
23062308
if (thePeakDMA < theTotalDMA)
23072309
thePeakDMA = theTotalDMA;
23082310
#ifdef INTENSE_DMA_BOOKKEEPING
2309-
const char* tagString = block->debugGetLiteralTagString();
2311+
tagString = block->debugGetLiteralTagString();
23102312
#endif
23112313
}
23122314
#endif MEMORYPOOL_DEBUG

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
#define _GAME_MEMORY_H_
5050

5151
// Turn off memory pool checkpointing for now.
52-
#define DISABLE_MEMORYPOOL_CHECKPOINTING 1
52+
#ifndef DISABLE_MEMORYPOOL_CHECKPOINTING
53+
#define DISABLE_MEMORYPOOL_CHECKPOINTING 1
54+
#endif
5355

5456
#if (defined(_DEBUG) || defined(_INTERNAL)) && !defined(MEMORYPOOL_DEBUG_CUSTOM_NEW) && !defined(DISABLE_MEMORYPOOL_DEBUG_CUSTOM_NEW)
5557
#define MEMORYPOOL_DEBUG_CUSTOM_NEW
@@ -79,7 +81,7 @@
7981
#ifdef MEMORYPOOL_DEBUG
8082

8183
// by default, enable free-block-retention for checkpointing in debug mode
82-
#ifndef DISABLE_MEMORYPOOL_CHECKPOINTING
84+
#if !defined(DISABLE_MEMORYPOOL_CHECKPOINTING) || DISABLE_MEMORYPOOL_CHECKPOINTING == 0
8385
#define MEMORYPOOL_CHECKPOINTING
8486
#endif
8587

@@ -127,7 +129,7 @@
127129
// ------------------------------------------------------
128130
#endif // MEMORYPOOL_CHECKPOINTING
129131

130-
#ifdef MEMORYPOOL_STACKTRACE
132+
#ifdef MEMORYPOOL_CHECKPOINTING
131133
/** display the stacktrace for allocation location for all blocks found.
132134
this bit may be mixed-n-matched with any other flag.
133135
*/

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
8080
faster to free raw DMA blocks.
8181
@todo verify this speedup is enough to be worth the extra space
8282
*/
83-
#define MPSB_DLINK
83+
#ifndef DISABLE_MEMORYPOOL_MPSB_DLINK
84+
#define MPSB_DLINK
85+
#endif
8486

8587
#ifdef MEMORYPOOL_DEBUG
8688

@@ -181,7 +183,6 @@ DECLARE_PERF_TIMER(MemoryPoolInitFilling)
181183
Int theWastedDMA = 0;
182184
Int thePeakWastedDMA = 0;
183185

184-
#define NO_INTENSE_DMA_BOOKKEEPING
185186
#ifdef INTENSE_DMA_BOOKKEEPING
186187
struct UsedNPeak
187188
{
@@ -2287,6 +2288,9 @@ void DynamicMemoryAllocator::freeBytes(void* pBlockPtr)
22872288
MemoryPoolSingleBlock *block = MemoryPoolSingleBlock::recoverBlockFromUserData(pBlockPtr);
22882289
#ifdef MEMORYPOOL_DEBUG
22892290
Int waste = 0, used = 0;
2291+
#ifdef INTENSE_DMA_BOOKKEEPING
2292+
const char* tagString;
2293+
#endif
22902294
{
22912295
USE_PERF_TIMER(MemoryPoolDebugging)
22922296
waste = 0;
@@ -2295,7 +2299,7 @@ void DynamicMemoryAllocator::freeBytes(void* pBlockPtr)
22952299
if (thePeakDMA < theTotalDMA)
22962300
thePeakDMA = theTotalDMA;
22972301
#ifdef INTENSE_DMA_BOOKKEEPING
2298-
const char* tagString = block->debugGetLiteralTagString();
2302+
tagString = block->debugGetLiteralTagString();
22992303
#endif
23002304
}
23012305
#endif MEMORYPOOL_DEBUG

cmake/config.cmake

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### BUILD OPTIONS ###
2+
13
# Do we want to build extra SDK stuff or just the game binary?
24
option(GENZH_BUILD_ZEROHOUR "Build Zero Hour code." ON)
35
option(GENZH_BUILD_GENERALS "Build Generals code." ON)
@@ -16,6 +18,40 @@ add_feature_info(InternalBuild GENZH_BUILD_INTERNAL "Building as an \"Internal\"
1618
add_feature_info(ProfileBuild GENZH_BUILD_PROFILE "Building as a \"Profile\" build")
1719
add_feature_info(DebugBuild GENZH_BUILD_DEBUG "Building as a \"Debug\" build")
1820

21+
22+
### MEMORY POOL OPTIONS ###
23+
24+
# Memory pool features
25+
option(GENZH_MEMORYPOOL_OVERRIDE_MALLOC "Enables the Dynamic Memory Allocator for malloc calls." OFF)
26+
option(GENZH_MEMORYPOOL_MPSB_DLINK "Adds a backlink to MemoryPoolSingleBlock. Makes it faster to free raw DMA blocks, but increases memory consumption." ON)
27+
28+
# Memory pool debugs
29+
option(GENZH_MEMORYPOOL_DEBUG "Enables Memory Pool debug." ON)
30+
option(GENZH_MEMORYPOOL_DEBUG_CUSTOM_NEW "Enables a custom new operator for the Memory Pool." ON)
31+
option(GENZH_MEMORYPOOL_DEBUG_CHECKPOINTING "Records checkpoint information about the history of memory allocations." OFF)
32+
option(GENZH_MEMORYPOOL_DEBUG_BOUNDINGWALL "Enables bounding wall checks around memory chunks to find memory trampling." ON)
33+
option(GENZH_MEMORYPOOL_DEBUG_STACKTRACE "Enables stack trace collection for allocations. Reduces runtime performance significantly." OFF)
34+
option(GENZH_MEMORYPOOL_DEBUG_INTENSE_VERIFY "Enables intensive verifications after nearly every memory operation. OFF by default, since it slows down things a lot, but is worth turning on for really obscure memory corruption issues." OFF)
35+
option(GENZH_MEMORYPOOL_DEBUG_CHECK_BLOCK_OWNERSHIP "Enables debug to verify that a block actually belongs to the pool it is called with. This is great for debugging, but can be realllly slow, so is OFF by default." OFF)
36+
option(GENZH_MEMORYPOOL_DEBUG_INTENSE_DMA_BOOKKEEPING "Prints statistics for memory usage of Memory Pools." OFF)
37+
38+
# Memory pool features
39+
add_feature_info(MemoryPoolOverrideMalloc GENZH_MEMORYPOOL_OVERRIDE_MALLOC "Build with Memory Pool malloc")
40+
add_feature_info(MemoryPoolMpsbDlink GENZH_MEMORYPOOL_MPSB_DLINK "Build with Memory Pool backlink")
41+
42+
# Memory pool debugs
43+
add_feature_info(MemoryPoolDebug GENZH_MEMORYPOOL_DEBUG "Build with Memory Pool debug")
44+
add_feature_info(MemoryPoolDebugCustomNew GENZH_MEMORYPOOL_DEBUG_CUSTOM_NEW "Build with Memory Pool custom new")
45+
add_feature_info(MemoryPoolDebugCheckpointing GENZH_MEMORYPOOL_DEBUG_CHECKPOINTING "Build with Memory Pool checkpointing")
46+
add_feature_info(MemoryPoolDebugBoundingwall GENZH_MEMORYPOOL_DEBUG_BOUNDINGWALL "Build with Memory Pool Bounding Wall")
47+
add_feature_info(MemoryPoolDebugStacktrace GENZH_MEMORYPOOL_DEBUG_STACKTRACE "Build with Memory Pool Stacktrace")
48+
add_feature_info(MemoryPoolDebugIntenseVerify GENZH_MEMORYPOOL_DEBUG_INTENSE_VERIFY "Build with Memory Pool intense verify")
49+
add_feature_info(MemoryPoolDebugCheckBlockOwnership GENZH_MEMORYPOOL_DEBUG_CHECK_BLOCK_OWNERSHIP "Build with Memory Pool block ownership checks")
50+
add_feature_info(MemoryPoolDebugIntenseDmaBookkeeping GENZH_MEMORYPOOL_DEBUG_INTENSE_DMA_BOOKKEEPING "Build with Memory Pool intense DMA bookkeeping")
51+
52+
53+
### INTERFACE LIBRARY SETUP ###
54+
1955
add_library(gz_config INTERFACE)
2056

2157
if(NOT IS_VS6_BUILD)
@@ -42,3 +78,48 @@ else()
4278
target_compile_definitions(gz_config INTERFACE _PROFILE)
4379
endif()
4480
endif()
81+
82+
# Memory pool features
83+
if(GENZH_MEMORYPOOL_OVERRIDE_MALLOC)
84+
target_compile_definitions(gz_config INTERFACE MEMORYPOOL_OVERRIDE_MALLOC=1)
85+
endif()
86+
87+
if(NOT GENZH_MEMORYPOOL_MPSB_DLINK)
88+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_MPSB_DLINK=1)
89+
endif()
90+
91+
# Memory pool debugs
92+
if(NOT GENZH_MEMORYPOOL_DEBUG)
93+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_DEBUG=1)
94+
else()
95+
if(NOT GENZH_MEMORYPOOL_DEBUG_CUSTOM_NEW)
96+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_DEBUG_CUSTOM_NEW=1)
97+
endif()
98+
99+
if(GENZH_MEMORYPOOL_DEBUG_CHECKPOINTING)
100+
# Set to 0 to override the default setting in code
101+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_CHECKPOINTING=0)
102+
else()
103+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_CHECKPOINTING=1)
104+
endif()
105+
106+
if(NOT GENZH_MEMORYPOOL_DEBUG_BOUNDINGWALL)
107+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_BOUNDINGWALL=1)
108+
endif()
109+
110+
if(NOT GENZH_MEMORYPOOL_DEBUG_STACKTRACE)
111+
target_compile_definitions(gz_config INTERFACE DISABLE_MEMORYPOOL_STACKTRACE=1)
112+
endif()
113+
114+
if(GENZH_MEMORYPOOL_DEBUG_INTENSE_VERIFY)
115+
target_compile_definitions(gz_config INTERFACE MEMORYPOOL_INTENSE_VERIFY=1)
116+
endif()
117+
118+
if(GENZH_MEMORYPOOL_DEBUG_CHECK_BLOCK_OWNERSHIP)
119+
target_compile_definitions(gz_config INTERFACE MEMORYPOOL_CHECK_BLOCK_OWNERSHIP=1)
120+
endif()
121+
122+
if(GENZH_MEMORYPOOL_DEBUG_INTENSE_DMA_BOOKKEEPING)
123+
target_compile_definitions(gz_config INTERFACE INTENSE_DMA_BOOKKEEPING=1)
124+
endif()
125+
endif()

0 commit comments

Comments
 (0)