forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathGameMemoryInit.cpp
More file actions
199 lines (173 loc) · 5.83 KB
/
GameMemoryInit.cpp
File metadata and controls
199 lines (173 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: MemoryInit.cpp
//-----------------------------------------------------------------------------
//
// Westwood Studios Pacific.
//
// Confidential Information
// Copyright (C) 2001 - All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Project: RTS3
//
// File name: MemoryInit.cpp
//
// Created: Steven Johnson, August 2001
//
// Desc: Memory manager
//
// ----------------------------------------------------------------------------
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
// SYSTEM INCLUDES
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
// USER INCLUDES
#include "Lib/BaseType.h"
#include "Common/GameMemory.h"
struct PoolSizeRec
{
const char* name;
Int initial;
Int overflow;
};
#if RTS_GENERALS
#include "GameMemoryInitDMA_Generals.inl"
#include "GameMemoryInitPools_Generals.inl"
#elif RTS_ZEROHOUR
#include "GameMemoryInitDMA_GeneralsMD.inl"
#include "GameMemoryInitPools_GeneralsMD.inl"
#endif
//-----------------------------------------------------------------------------
void userMemoryManagerGetDmaParms(Int *numSubPools, const PoolInitRec **pParms)
{
*numSubPools = ARRAY_SIZE(DefaultDMA);
*pParms = DefaultDMA;
}
//-----------------------------------------------------------------------------
void userMemoryAdjustPoolSize(const char *poolName, Int& initialAllocationCount, Int& overflowAllocationCount)
{
if (initialAllocationCount > 0)
return;
for (const PoolSizeRec* p = PoolSizes; p->name != nullptr; ++p)
{
if (strcmp(p->name, poolName) == 0)
{
initialAllocationCount = p->initial;
overflowAllocationCount = p->overflow;
return;
}
}
DEBUG_CRASH(("Initial size for pool %s not found -- you should add it to MemoryInit.cpp",poolName));
}
//-----------------------------------------------------------------------------
static Int roundUpMemBound(Int i)
{
const int MEM_BOUND_ALIGNMENT = 4;
if (i < MEM_BOUND_ALIGNMENT)
return MEM_BOUND_ALIGNMENT;
else
return (i + (MEM_BOUND_ALIGNMENT-1)) & ~(MEM_BOUND_ALIGNMENT-1);
}
//-----------------------------------------------------------------------------
static Bool parseMemoryPoolOverrideLine(const char* line, char* poolName, size_t poolNameSize, Int* initial, Int* overflow)
{
if (line == nullptr || poolName == nullptr || poolNameSize == 0 || initial == nullptr || overflow == nullptr)
return false;
if (line[0] == ';')
return false;
const char* cursor = line;
while (isspace(static_cast<unsigned char>(*cursor)))
++cursor;
if (*cursor == '\0')
return false;
const char* poolNameBegin = cursor;
while (*cursor != '\0' && !isspace(static_cast<unsigned char>(*cursor)))
++cursor;
const size_t poolNameLength = cursor - poolNameBegin;
if (poolNameLength == 0 || poolNameLength >= poolNameSize)
return false;
char* end = nullptr;
errno = 0;
const long parsedInitial = strtol(cursor, &end, 10);
if (end == cursor || errno == ERANGE)
return false;
cursor = end;
errno = 0;
const long parsedOverflow = strtol(cursor, &end, 10);
if (end == cursor || errno == ERANGE)
return false;
memcpy(poolName, poolNameBegin, poolNameLength);
poolName[poolNameLength] = '\0';
*initial = static_cast<Int>(parsedInitial);
*overflow = static_cast<Int>(parsedOverflow);
return true;
}
#ifdef MEMORY_MANAGER_HARDENING_TESTS
Bool userMemoryManagerParsePoolOverrideLineForTest(const char* line, char* poolName, size_t poolNameSize, Int* initial, Int* overflow)
{
return parseMemoryPoolOverrideLine(line, poolName, poolNameSize, initial, overflow);
}
#endif
//-----------------------------------------------------------------------------
void userMemoryManagerInitPools()
{
// note that we MUST use stdio stuff here, and not the normal game file system
// (with bigfile support, etc), because that relies on memory pools, which
// aren't yet initialized properly! so rely ONLY on straight stdio stuff here.
// (not even AsciiString. thanks.)
// since we're called prior to main, the cur dir might not be what
// we expect. so do it the hard way.
char buf[_MAX_PATH];
::GetModuleFileName(nullptr, buf, sizeof(buf));
if (char* pEnd = strrchr(buf, '\\'))
{
*pEnd = 0;
}
strlcat(buf, "\\Data\\INI\\MemoryPools.ini", ARRAY_SIZE(buf));
FILE* fp = fopen(buf, "r");
if (fp)
{
char poolName[256];
Int initial, overflow;
while (fgets(buf, _MAX_PATH, fp))
{
if (parseMemoryPoolOverrideLine(buf, poolName, ARRAY_SIZE(poolName), &initial, &overflow))
{
for (PoolSizeRec* p = PoolSizes; p->name != nullptr; ++p)
{
if (stricmp(p->name, poolName) == 0)
{
// currently, these must be multiples of 4. so round up.
p->initial = roundUpMemBound(initial);
p->overflow = roundUpMemBound(overflow);
break; // from for-p
}
}
}
}
fclose(fp);
}
}