forked from ValveSoftware/GameNetworkingSockets
-
Notifications
You must be signed in to change notification settings - Fork 15
/
utlmemory.cpp
384 lines (312 loc) · 9.74 KB
/
utlmemory.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//========= Copyright 1996-2010, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include <utlmemory.h>
CUtlMemoryBase::CUtlMemoryBase( int nSizeOfType, int nGrowSize, int nInitAllocationCount ) : m_pMemory(0),
m_nAllocationCount( nInitAllocationCount ), m_nGrowSize( nGrowSize ), m_unSizeOfElements( nSizeOfType )
{
Assert( m_unSizeOfElements > 0 );
Assert( nGrowSize >= 0 );
if (m_nAllocationCount)
{
UTLMEMORY_TRACK_ALLOC();
m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements );
}
}
CUtlMemoryBase::CUtlMemoryBase( int nSizeOfType, void * pMemory, int numElements ) : m_pMemory(pMemory),
m_nAllocationCount( numElements ), m_unSizeOfElements( nSizeOfType )
{
Assert( m_unSizeOfElements > 0 );
// Special marker indicating externally supplied modifyable memory
m_nGrowSize = EXTERNAL_BUFFER_MARKER;
}
CUtlMemoryBase::CUtlMemoryBase( int nSizeOfType, const void * pMemory, int numElements ) : m_pMemory( (void*)pMemory ),
m_nAllocationCount( numElements ), m_unSizeOfElements( nSizeOfType )
{
Assert( m_unSizeOfElements > 0 );
// Special marker indicating externally supplied modifyable memory
m_nGrowSize = EXTERNAL_CONST_BUFFER_MARKER;
}
CUtlMemoryBase::~CUtlMemoryBase()
{
Purge();
}
//-----------------------------------------------------------------------------
// Fast swap
//-----------------------------------------------------------------------------
void CUtlMemoryBase::Swap( CUtlMemoryBase &mem )
{
// Shouldn't really be swapping if types didn't match, thus sizes should match
Assert( m_unSizeOfElements == mem.m_unSizeOfElements );
SWAP( m_nGrowSize, mem.m_nGrowSize );
SWAP( m_pMemory, mem.m_pMemory );
SWAP( m_nAllocationCount, mem.m_nAllocationCount );
SWAP( m_unSizeOfElements, mem.m_unSizeOfElements );
}
//-----------------------------------------------------------------------------
// Fast swap
//-----------------------------------------------------------------------------
void *CUtlMemoryBase::Detach()
{
m_nAllocationCount = 0;
void *pMemory = m_pMemory;
m_pMemory = NULL;
return pMemory;
}
//-----------------------------------------------------------------------------
// Switches the buffer from an external memory buffer to a reallocatable buffer
//-----------------------------------------------------------------------------
void CUtlMemoryBase::ConvertToGrowableMemory( int nGrowSize )
{
if ( !IsExternallyAllocated() )
return;
m_nGrowSize = nGrowSize;
if (m_nAllocationCount)
{
UTLMEMORY_TRACK_ALLOC();
MEM_ALLOC_CREDIT_CLASS();
int nNumBytes = m_nAllocationCount * m_unSizeOfElements;
void *pMemory = PvAlloc( nNumBytes );
memcpy( pMemory, m_pMemory, nNumBytes );
m_pMemory = pMemory;
}
else
{
m_pMemory = NULL;
}
}
//-----------------------------------------------------------------------------
// Attaches the buffer to external memory....
//-----------------------------------------------------------------------------
void CUtlMemoryBase::SetExternalBuffer( void * pMemory, int numElements )
{
// Blow away any existing allocated memory
Purge();
m_pMemory = pMemory;
m_nAllocationCount = numElements;
// Indicate that we don't own the memory
m_nGrowSize = EXTERNAL_BUFFER_MARKER;
}
void CUtlMemoryBase::SetExternalBuffer( const void* pMemory, int numElements )
{
// Blow away any existing allocated memory
Purge();
m_pMemory = const_cast<void*>( pMemory );
m_nAllocationCount = numElements;
// Indicate that we don't own the memory
m_nGrowSize = EXTERNAL_CONST_BUFFER_MARKER;
}
//-----------------------------------------------------------------------------
// is the memory externally allocated?
//-----------------------------------------------------------------------------
bool CUtlMemoryBase::IsExternallyAllocated() const
{
return (m_nGrowSize < 0);
}
//-----------------------------------------------------------------------------
// is the memory read only?
//-----------------------------------------------------------------------------
bool CUtlMemoryBase::IsReadOnly() const
{
return (m_nGrowSize == EXTERNAL_CONST_BUFFER_MARKER);
}
void CUtlMemoryBase::SetGrowSize( int nSize )
{
Assert( !IsExternallyAllocated() );
Assert( nSize >= 0 );
m_nGrowSize = nSize;
}
//-----------------------------------------------------------------------------
// Size
//-----------------------------------------------------------------------------
int CUtlMemoryBase::NumAllocated() const
{
return m_nAllocationCount;
}
int CUtlMemoryBase::Count() const
{
return m_nAllocationCount;
}
//-----------------------------------------------------------------------------
// Is element index valid?
//-----------------------------------------------------------------------------
bool CUtlMemoryBase::IsIdxValid( int i ) const
{
return (i >= 0) && (i < m_nAllocationCount);
}
//-----------------------------------------------------------------------------
// Grows the memory
//-----------------------------------------------------------------------------
int UtlMemory_CalcNewAllocationCount( int nAllocationCount, int nGrowSize, int nNewSize, int nBytesItem )
{
if ( nGrowSize )
{
nAllocationCount = ((1 + ((nNewSize - 1) / nGrowSize)) * nGrowSize);
}
else
{
if ( !nAllocationCount )
{
if ( nBytesItem > 0 )
{
// Compute an allocation which is at least as big as a cache line...
nAllocationCount = (31 + nBytesItem) / nBytesItem;
}
else
{
// Should be impossible, but if hit try to grow an amount that may be large
// enough for most cases and thus avoid both divide by zero above as well as
// likely memory corruption afterwards.
AssertMsg1( false, "nBytesItem is %d in UtlMemory_CalcNewAllocationCount", nBytesItem );
nAllocationCount = 256;
}
}
// Cap growth to avoid high-end doubling insanity (1 GB -> 2 GB -> overflow)
int nMaxGrowStep = Max( 1, 256*1024*1024 / ( nBytesItem > 0 ? nBytesItem : 1 ) );
while (nAllocationCount < nNewSize)
{
#ifndef _XBOX
// Grow by doubling, but at most 256 MB at a time.
nAllocationCount += Min( nAllocationCount, nMaxGrowStep );
#else
int nNewAllocationCount = ( nAllocationCount * 9) / 8; // 12.5 %
if ( nNewAllocationCount > nAllocationCount )
nAllocationCount = nNewAllocationCount;
else
nAllocationCount *= 2;
#endif
}
}
return nAllocationCount;
}
void CUtlMemoryBase::Grow( int num )
{
Assert( num > 0 );
if ( IsExternallyAllocated() )
{
// Can't grow a buffer whose memory was externally allocated
Assert(0);
return;
}
// Make sure we have at least numallocated + num allocations.
// Use the grow rules specified for this memory (in m_nGrowSize)
int nAllocationRequested = m_nAllocationCount + num;
UTLMEMORY_TRACK_FREE();
m_nAllocationCount = UtlMemory_CalcNewAllocationCount( m_nAllocationCount, m_nGrowSize, nAllocationRequested, m_unSizeOfElements );
UTLMEMORY_TRACK_ALLOC();
if (m_pMemory)
{
m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements );
}
else
{
m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements );
}
}
//-----------------------------------------------------------------------------
// Makes sure we've got at least this much memory
//-----------------------------------------------------------------------------
void CUtlMemoryBase::EnsureCapacity( int num )
{
if (m_nAllocationCount >= num)
return;
if ( IsExternallyAllocated() )
{
// Can't grow a buffer whose memory was externally allocated
Assert(0);
return;
}
UTLMEMORY_TRACK_FREE();
m_nAllocationCount = num;
UTLMEMORY_TRACK_ALLOC();
if (m_pMemory)
{
m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements );
}
else
{
m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements );
}
}
//-----------------------------------------------------------------------------
// Memory deallocation
//-----------------------------------------------------------------------------
void CUtlMemoryBase::Purge()
{
if ( !IsExternallyAllocated() )
{
if (m_pMemory)
{
UTLMEMORY_TRACK_FREE();
FreePv( m_pMemory );
m_pMemory = 0;
}
m_nAllocationCount = 0;
}
}
void CUtlMemoryBase::Purge( int numElements, bool bRealloc )
{
Assert( numElements >= 0 );
if( numElements > m_nAllocationCount )
{
// Ensure this isn't a grow request in disguise.
Assert( numElements <= m_nAllocationCount );
return;
}
// If we have zero elements, simply do a purge:
if( numElements == 0 )
{
Purge();
return;
}
if ( IsExternallyAllocated() )
{
// Can't shrink a buffer whose memory was externally allocated, fail silently like purge
return;
}
// If the number of elements is the same as the allocation count, we are done.
if( numElements == m_nAllocationCount )
{
return;
}
if( !m_pMemory )
{
// Allocation count is non zero, but memory is null.
Assert( m_pMemory );
return;
}
if ( bRealloc )
{
UTLMEMORY_TRACK_FREE();
m_nAllocationCount = numElements;
UTLMEMORY_TRACK_ALLOC();
// Allocation count > 0, shrink it down.
MEM_ALLOC_CREDIT_CLASS();
m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements );
}
else
{
// Some of the tracking may be wrong as we are changing the size but are not reallocating.
m_nAllocationCount = numElements;
}
}
//-----------------------------------------------------------------------------
// Data and memory validation
//-----------------------------------------------------------------------------
#ifdef DBGFLAG_VALIDATE
void CUtlMemoryBase::Validate( CValidator &validator, const char *pchName )
{
#ifdef _WIN32
validator.Push( typeid(*this).raw_name(), this, pchName );
#else
validator.Push( typeid(*this).name(), this, pchName );
#endif
if ( NULL != m_pMemory )
validator.ClaimMemory( m_pMemory );
validator.Pop();
}
#endif // DBGFLAG_VALIDATE