forked from ValveSoftware/GameNetworkingSockets
-
Notifications
You must be signed in to change notification settings - Fork 15
/
utlmemory.h
262 lines (203 loc) · 7.35 KB
/
utlmemory.h
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
//===== Copyright (C) 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $NoKeywords: $
//
// A growable memory class.
//===========================================================================//
#pragma once
#ifndef UTLMEMORY_H
#define UTLMEMORY_H
#include <string.h>
#include "miniutl.h"
//-----------------------------------------------------------------------------
#ifdef UTLMEMORY_TRACK
#define UTLMEMORY_TRACK_ALLOC() MemAlloc_RegisterAllocation( "Sum of all UtlMemory", 0, m_nAllocationCount * sizeof(T), m_nAllocationCount * sizeof(T), 0 )
#define UTLMEMORY_TRACK_FREE() if ( !m_pMemory ) ; else MemAlloc_RegisterDeallocation( "Sum of all UtlMemory", 0, m_nAllocationCount * sizeof(T), m_nAllocationCount * sizeof(T), 0 )
#else
#define UTLMEMORY_TRACK_ALLOC() ((void)0)
#define UTLMEMORY_TRACK_FREE() ((void)0)
#endif
//-----------------------------------------------------------------------------
// The CUtlMemory class:
// A growable memory class which doubles in size by default.
//-----------------------------------------------------------------------------
class CUtlMemoryBase
{
public:
// constructor, destructor
CUtlMemoryBase( int nSizeOfType, int nGrowSize = 0, int nInitSize = 0 );
CUtlMemoryBase( int nSizeOfType, void* pMemory, int numElements );
CUtlMemoryBase( int nSizeOfType, const void* pMemory, int numElements );
~CUtlMemoryBase();
// Can we use this index?
bool IsIdxValid( int i ) const;
// Attaches the buffer to external memory....
void SetExternalBuffer( void * pMemory, int numElements );
void SetExternalBuffer( const void * pMemory, int numElements );
// Fast swap
void Swap( CUtlMemoryBase &mem );
void *Detach();
// Switches the buffer from an external memory buffer to a reallocatable buffer
// Will copy the current contents of the external buffer to the reallocatable buffer
void ConvertToGrowableMemory( int nGrowSize );
// Size
int NumAllocated() const;
int Count() const;
int CubAllocated() const { return m_nAllocationCount * m_unSizeOfElements; }
// Grows the memory, so that at least allocated + num elements are allocated
void Grow( int num = 1 );
// Makes sure we've got at least this much memory
void EnsureCapacity( int num );
// Memory deallocation
void Purge();
// Purge all but the given number of elements
void Purge( int numElements, bool bRealloc = true );
// is the memory externally allocated?
bool IsExternallyAllocated() const;
// is the memory read only?
bool IsReadOnly() const;
// Set the size by which the memory grows
void SetGrowSize( int size );
protected:
// Copy construction and assignment are not valid
CUtlMemoryBase(const CUtlMemoryBase& rhs);
const CUtlMemoryBase& operator=(const CUtlMemoryBase& rhs);
enum
{
EXTERNAL_BUFFER_MARKER = -1,
EXTERNAL_CONST_BUFFER_MARKER = -2,
};
uint32_t m_unSizeOfElements;
void * m_pMemory;
int m_nAllocationCount;
int m_nGrowSize;
};
template< class T >
class CUtlMemory : public CUtlMemoryBase
{
public:
// constructor, destructor
CUtlMemory( int nGrowSize = 0, int nInitSize = 0 );
CUtlMemory( T* pMemory, int numElements );
CUtlMemory( const T* pMemory, int numElements );
// element access
T& operator[]( int i );
const T& operator[]( int i ) const;
T& Element( int i );
const T& Element( int i ) const;
// Gets the base address (can change when adding elements!)
T* Base();
const T* Base() const;
private:
// Copy construction and assignment are not valid
CUtlMemory(const CUtlMemory& rhs);
const CUtlMemory& operator=(const CUtlMemory& rhs);
};
//-----------------------------------------------------------------------------
// The CUtlMemoryFixed class:
// A fixed memory class
//-----------------------------------------------------------------------------
template< typename T, size_t SIZE >
class CUtlMemoryFixed
{
public:
// constructor, destructor
CUtlMemoryFixed( int nGrowSize = 0, int nInitSize = 0 ) { Assert( nInitSize == 0 || nInitSize == SIZE ); }
CUtlMemoryFixed( T* pMemory, int numElements ) { Assert( 0 ); }
// Can we use this index?
bool IsIdxValid( int i ) const { return (i >= 0) && (i < SIZE); }
// Gets the base address
T* Base() { return (T*)(&m_Memory[0]); }
const T* Base() const { return (const T*)(&m_Memory[0]); }
// element access
T& operator[]( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
const T& operator[]( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
T& Element( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
const T& Element( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
// Attaches the buffer to external memory....
void SetExternalBuffer( T* pMemory, int numElements ) { Assert( 0 ); }
// Size
int NumAllocated() const { return SIZE; }
int Count() const { return SIZE; }
// Grows the memory, so that at least allocated + num elements are allocated
void Grow( int num = 1 ) { Assert( 0 ); }
// Makes sure we've got at least this much memory
void EnsureCapacity( int num ) { Assert( num <= SIZE ); }
// Memory deallocation
void Purge() {}
// Purge all but the given number of elements (NOT IMPLEMENTED IN CUtlMemoryFixed)
void Purge( int numElements, bool bRealloc = true ) { Assert( 0 ); }
// is the memory externally allocated?
bool IsExternallyAllocated() const { return false; }
// Set the size by which the memory grows
void SetGrowSize( int size ) {}
private:
uint8_t m_Memory[SIZE*sizeof(T)];
};
//-----------------------------------------------------------------------------
// constructor, destructor
//-----------------------------------------------------------------------------
template< class T >
inline CUtlMemory<T>::CUtlMemory( int nGrowSize, int nInitAllocationCount ) : CUtlMemoryBase( sizeof( T ), nGrowSize, nInitAllocationCount )
{
}
template< class T >
inline CUtlMemory<T>::CUtlMemory( T* pMemory, int numElements ) : CUtlMemoryBase( sizeof( T ), (void*)pMemory, numElements )
{
}
template< class T >
inline CUtlMemory<T>::CUtlMemory( const T* pMemory, int numElements ) : CUtlMemoryBase( sizeof( T ), pMemory, numElements )
{
}
template< class T >
void SWAP( T &a, T &b )
{
T tmp( a );
a = b;
b = tmp;
}
//-----------------------------------------------------------------------------
// element access
//-----------------------------------------------------------------------------
template< class T >
inline T& CUtlMemory<T>::operator[]( int i )
{
DbgAssert( !IsReadOnly() );
DbgAssert( IsIdxValid(i) );
return ((T*)m_pMemory)[i];
}
template< class T >
inline const T& CUtlMemory<T>::operator[]( int i ) const
{
DbgAssert( IsIdxValid(i) );
return ((T*)m_pMemory)[i];
}
template< class T >
inline T& CUtlMemory<T>::Element( int i )
{
DbgAssert( !IsReadOnly() );
DbgAssert( IsIdxValid(i) );
return ((T*)m_pMemory)[i];
}
template< class T >
inline const T& CUtlMemory<T>::Element( int i ) const
{
DbgAssert( IsIdxValid(i) );
return ((T*)m_pMemory)[i];
}
//-----------------------------------------------------------------------------
// Gets the base address (can change when adding elements!)
//-----------------------------------------------------------------------------
template< class T >
inline T* CUtlMemory<T>::Base()
{
return (T*)m_pMemory;
}
template< class T >
inline const T *CUtlMemory<T>::Base() const
{
return (const T*)m_pMemory;
}
#endif // UTLMEMORY_H