-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathmemstack.cpp
297 lines (240 loc) · 6.72 KB
/
memstack.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#if defined( _WIN32 ) && !defined( _X360 )
#define WIN_32_LEAN_AND_MEAN
#include <windows.h>
#define VA_COMMIT_FLAGS MEM_COMMIT
#define VA_RESERVE_FLAGS MEM_RESERVE
#elif defined( _X360 )
#define VA_COMMIT_FLAGS (MEM_COMMIT|MEM_NOZERO|MEM_LARGE_PAGES)
#define VA_RESERVE_FLAGS (MEM_RESERVE|MEM_LARGE_PAGES)
#endif
#include "tier0/dbg.h"
#include "memstack.h"
#include "utlmap.h"
#include "tier0/memdbgon.h"
#ifdef _WIN32
#pragma warning(disable:4073)
#pragma init_seg(lib)
#endif
//-----------------------------------------------------------------------------
MEMALLOC_DEFINE_EXTERNAL_TRACKING(CMemoryStack);
//-----------------------------------------------------------------------------
CMemoryStack::CMemoryStack()
: m_pBase( NULL ),
m_pNextAlloc( NULL ),
m_pAllocLimit( NULL ),
m_pCommitLimit( NULL ),
m_alignment( 16 ),
#if defined(_WIN32)
m_commitSize( 0 ),
m_minCommit( 0 ),
#endif
m_maxSize( 0 )
{
}
//-------------------------------------
CMemoryStack::~CMemoryStack()
{
if ( m_pBase )
Term();
}
//-------------------------------------
bool CMemoryStack::Init( unsigned maxSize, unsigned commitSize, unsigned initialCommit, unsigned alignment )
{
Assert( !m_pBase );
#ifdef _X360
m_bPhysical = false;
#endif
m_maxSize = maxSize;
m_alignment = AlignValue( alignment, 4 );
Assert( m_alignment == alignment );
Assert( m_maxSize > 0 );
#if defined(_WIN32)
if ( commitSize != 0 )
{
m_commitSize = commitSize;
}
unsigned pageSize;
#ifndef _X360
SYSTEM_INFO sysInfo;
GetSystemInfo( &sysInfo );
Assert( !( sysInfo.dwPageSize & (sysInfo.dwPageSize-1)) );
pageSize = sysInfo.dwPageSize;
#else
pageSize = 64*1024;
#endif
if ( m_commitSize == 0 )
{
m_commitSize = pageSize;
}
else
{
m_commitSize = AlignValue( m_commitSize, pageSize );
}
m_maxSize = AlignValue( m_maxSize, m_commitSize );
Assert( m_maxSize % pageSize == 0 && m_commitSize % pageSize == 0 && m_commitSize <= m_maxSize );
m_pBase = (unsigned char *)VirtualAlloc( NULL, m_maxSize, VA_RESERVE_FLAGS, PAGE_NOACCESS );
Assert( m_pBase );
m_pCommitLimit = m_pNextAlloc = m_pBase;
if ( initialCommit )
{
initialCommit = AlignValue( initialCommit, m_commitSize );
Assert( initialCommit < m_maxSize );
if ( !VirtualAlloc( m_pCommitLimit, initialCommit, VA_COMMIT_FLAGS, PAGE_READWRITE ) )
return false;
m_minCommit = initialCommit;
m_pCommitLimit += initialCommit;
MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() );
}
#else
m_pBase = (byte *)MemAlloc_AllocAligned( m_maxSize, alignment ? alignment : 1 );
m_pNextAlloc = m_pBase;
m_pCommitLimit = m_pBase + m_maxSize;
#endif
m_pAllocLimit = m_pBase + m_maxSize;
return ( m_pBase != NULL );
}
//-------------------------------------
#ifdef _X360
bool CMemoryStack::InitPhysical( unsigned size, unsigned alignment )
{
m_bPhysical = true;
m_maxSize = m_commitSize = size;
m_alignment = AlignValue( alignment, 4 );
int flags = PAGE_READWRITE;
if ( size >= 16*1024*1024 )
{
flags |= MEM_16MB_PAGES;
}
else
{
flags |= MEM_LARGE_PAGES;
}
m_pBase = (unsigned char *)XPhysicalAlloc( m_maxSize, MAXULONG_PTR, 4096, flags );
Assert( m_pBase );
m_pNextAlloc = m_pBase;
m_pCommitLimit = m_pBase + m_maxSize;
m_pAllocLimit = m_pBase + m_maxSize;
MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() );
return ( m_pBase != NULL );
}
#endif
//-------------------------------------
void CMemoryStack::Term()
{
FreeAll();
if ( m_pBase )
{
#if defined(_WIN32)
VirtualFree( m_pBase, 0, MEM_RELEASE );
#else
MemAlloc_FreeAligned( m_pBase );
#endif
m_pBase = NULL;
}
}
//-------------------------------------
int CMemoryStack::GetSize()
{
#ifdef _WIN32
return m_pCommitLimit - m_pBase;
#else
return m_maxSize;
#endif
}
//-------------------------------------
bool CMemoryStack::CommitTo( byte *pNextAlloc ) RESTRICT
{
#ifdef _X360
if ( m_bPhysical )
{
return NULL;
}
#endif
#if defined(_WIN32)
unsigned char * pNewCommitLimit = AlignValue( pNextAlloc, m_commitSize );
unsigned commitSize = pNewCommitLimit - m_pCommitLimit;
if ( GetSize() )
MemAlloc_RegisterExternalDeallocation( CMemoryStack, GetBase(), GetSize() );
if( m_pCommitLimit + commitSize > m_pAllocLimit )
{
return false;
}
if ( !VirtualAlloc( m_pCommitLimit, commitSize, VA_COMMIT_FLAGS, PAGE_READWRITE ) )
{
Assert( 0 );
return false;
}
m_pCommitLimit = pNewCommitLimit;
if ( GetSize() )
MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() );
return true;
#else
Assert( 0 );
return false;
#endif
}
//-------------------------------------
void CMemoryStack::FreeToAllocPoint( MemoryStackMark_t mark, bool bDecommit )
{
void *pAllocPoint = m_pBase + mark;
Assert( pAllocPoint >= m_pBase && pAllocPoint <= m_pNextAlloc );
if ( pAllocPoint >= m_pBase && pAllocPoint < m_pNextAlloc )
{
if ( bDecommit )
{
#if defined(_WIN32)
unsigned char *pDecommitPoint = AlignValue( (unsigned char *)pAllocPoint, m_commitSize );
if ( pDecommitPoint < m_pBase + m_minCommit )
{
pDecommitPoint = m_pBase + m_minCommit;
}
unsigned decommitSize = m_pCommitLimit - pDecommitPoint;
if ( decommitSize > 0 )
{
MemAlloc_RegisterExternalDeallocation( CMemoryStack, GetBase(), GetSize() );
VirtualFree( pDecommitPoint, decommitSize, MEM_DECOMMIT );
m_pCommitLimit = pDecommitPoint;
if ( mark > 0 )
{
MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() );
}
}
#endif
}
m_pNextAlloc = (unsigned char *)pAllocPoint;
}
}
//-------------------------------------
void CMemoryStack::FreeAll( bool bDecommit )
{
if ( m_pBase && m_pCommitLimit - m_pBase > 0 )
{
if ( bDecommit )
{
#if defined(_WIN32)
MemAlloc_RegisterExternalDeallocation( CMemoryStack, GetBase(), GetSize() );
VirtualFree( m_pBase, m_pCommitLimit - m_pBase, MEM_DECOMMIT );
m_pCommitLimit = m_pBase;
#endif
}
m_pNextAlloc = m_pBase;
}
}
//-------------------------------------
void CMemoryStack::Access( void **ppRegion, unsigned *pBytes )
{
*ppRegion = m_pBase;
*pBytes = ( m_pNextAlloc - m_pBase);
}
//-------------------------------------
void CMemoryStack::PrintContents()
{
Msg( "Total used memory: %d\n", GetUsed() );
Msg( "Total committed memory: %d\n", GetSize() );
}
//-----------------------------------------------------------------------------