/
yojimbo_allocator.h
342 lines (222 loc) · 14.5 KB
/
yojimbo_allocator.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
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
/*
Yojimbo Client/Server Network Protocol Library.
Copyright © 2016, The Network Protocol Company, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef YOJIMBO_ALLOCATOR_H
#define YOJIMBO_ALLOCATOR_H
#include <stdint.h>
#include <new>
#if YOJIMBO_DEBUG_MEMORY_LEAKS
#include <map>
#endif // YOJIMBO_DEBUG_MEMORY_LEAKS
/** @file */
typedef void* tlsf_t;
namespace yojimbo
{
/**
Get the default allocator.
Use this allocator when you just want to use malloc/free, but in the form of a yojimbo allocator.
This allocator instance is created inside InitializeYojimbo and destroyed in ShutdownYojimbo.
In debug build, it will automatically check for memory leaks and print them out for you when you shutdown the library.
@returns The default allocator instances backed by malloc and free.
*/
class Allocator & GetDefaultAllocator();
/// Macro for creating a new object instance with a yojimbo allocator.
#define YOJIMBO_NEW( a, T, ... ) ( new ( (a).Allocate( sizeof(T), __FILE__, __LINE__ ) ) T(__VA_ARGS__) )
/// Macro for deleting an object created with a yojimbo allocator.
#define YOJIMBO_DELETE( a, T, p ) do { if (p) { (p)->~T(); (a).Free( p, __FILE__, __LINE__ ); p = NULL; } } while (0)
/// Macro for allocating a block of memory with a yojimbo allocator.
#define YOJIMBO_ALLOCATE( a, bytes ) (a).Allocate( (bytes), __FILE__, __LINE__ )
/// Macro for freeing a block of memory created with a yojimbo allocator.
#define YOJIMBO_FREE( a, p ) do { if ( p ) { (a).Free( p, __FILE__, __LINE__ ); p = NULL; } } while(0)
/// Allocator error level.
enum AllocatorError
{
ALLOCATOR_ERROR_NONE = 0, ///< No error. All is well.
ALLOCATOR_ERROR_FAILED_TO_ALLOCATE ///< Tried to make an allocation but failed because the allocator was out of memory.
};
#if YOJIMBO_DEBUG_MEMORY_LEAKS
/**
Debug structure used to track allocations and find memory leaks.
Active in debug build only. Disabled in release builds for performance reasons.
*/
struct AllocatorEntry
{
size_t size; ///< The size of the allocation in bytes.
const char * file; ///< Filename of the source code file that made the allocation.
int line; ///< Line number in the source code where the allocation was made.
};
#endif // #if YOJIMBO_DEBUG_MEMORY_LEAKS
/**
Functionality common to all allocators.
Extend this class to hook up your own allocator to yojimbo.
IMPORTANT: This allocator is not yet thread safe. Only call it from one thread!
*/
class Allocator
{
public:
/**
Allocator constructor.
Sets the error level to ALLOCATOR_ERROR_NONE.
*/
Allocator();
/**
Allocator destructor.
Make sure all allocations made from this allocator are freed before you destroy this allocator.
In debug build, validates this is true walks the map of allocator entries. Any outstanding entries are considered memory leaks and printed to stdout.
*/
virtual ~Allocator();
/**
Allocate a block of memory.
IMPORTANT: Don't call this directly. Use the YOJIMBO_NEW or YOJIMBO_ALLOCATE macros instead, because they automatically pass in the source filename and line number for you.
@param size The size of the block of memory to allocate (bytes).
@param file The source code filename that is performing the allocation. Used for tracking allocations and reporting on memory leaks.
@param line The line number in the source code file that is performing the allocation.
@returns A block of memory of the requested size, or NULL if the allocation could not be performed. If NULL is returned, the error level is set to ALLOCATION_ERROR_FAILED_TO_ALLOCATE.
@see Allocator::Free
@see Allocator::GetError
*/
virtual void * Allocate( size_t size, const char * file, int line ) = 0;
/**
Free a block of memory.
IMPORTANT: Don't call this directly. Use the YOJIMBO_DELETE or YOJIMBO_FREE macros instead, because they automatically pass in the source filename and line number for you.
@param p Pointer to the block of memory to free. Must be non-NULL block of memory that was allocated with this allocator. Will assert otherwise.
@param file The source code filename that is performing the free. Used for tracking allocations and reporting on memory leaks.
@param line The line number in the source code file that is performing the free.
@see Allocator::Allocate
@see Allocator::GetError
*/
virtual void Free( void * p, const char * file, int line ) = 0;
/**
Get the allocator error level.
Use this function to check if an allocation has failed. This is used in the client/server to disconnect a client with a failed allocation.
@returns The allocator error level.
*/
AllocatorError GetError() const { return m_error; }
/**
Clear the allocator error level back to default.
*/
void ClearError() { m_error = ALLOCATOR_ERROR_NONE; }
protected:
/**
Set the error level.
For correct client/server behavior when an allocation fails, please make sure you call this method to set the error level to ALLOCATOR_ERROR_FAILED_TO_ALLOCATE.
@param error The allocator error level to set.
*/
void SetError( AllocatorError error ) { m_error = error; }
/**
Call this function to track an allocation made by your derived allocator class.
In debug build, tracked allocations are automatically checked for leaks when the allocator is destroyed.
@param p Pointer to the memory that was allocated.
@param size The size of the allocation in bytes.
@param file The source code file that performed the allocation.
@param line The line number in the source file where the allocation was performed.
*/
void TrackAlloc( void * p, size_t size, const char * file, int line );
/**
Call this function to track a free made by your derived allocator class.
In debug build, any allocation tracked without a corresponding free is considered a memory leak when the allocator is destroyed.
@param p Pointer to the memory that was allocated.
@param file The source code file that is calling in to free the memory.
@param line The line number in the source file where the free is being called from.
*/
void TrackFree( void * p, const char * file, int line );
AllocatorError m_error; ///< The allocator error level.
#if YOJIMBO_DEBUG_MEMORY_LEAKS
std::map<void*,AllocatorEntry> m_alloc_map; ///< Debug only data structure used to find and report memory leaks.
#endif // #if YOJIMBO_DEBUG_MEMORY_LEAKS
private:
Allocator( const Allocator & other );
Allocator & operator = ( const Allocator & other );
};
/**
Allocator implementation based on malloc and free.
*/
class DefaultAllocator : public Allocator
{
public:
/**
Default constructor.
*/
DefaultAllocator() {}
/**
Allocates a block of memory using "malloc".
IMPORTANT: Don't call this directly. Use the YOJIMBO_NEW or YOJIMBO_ALLOCATE macros instead, because they automatically pass in the source filename and line number for you.
@param size The size of the block of memory to allocate (bytes).
@param file The source code filename that is performing the allocation. Used for tracking allocations and reporting on memory leaks.
@param line The line number in the source code file that is performing the allocation.
@returns A block of memory of the requested size, or NULL if the allocation could not be performed. If NULL is returned, the error level is set to ALLOCATION_ERROR_FAILED_TO_ALLOCATE.
*/
void * Allocate( size_t size, const char * file, int line );
/**
Free a block of memory by calling "free".
IMPORTANT: Don't call this directly. Use the YOJIMBO_DELETE or YOJIMBO_FREE macros instead, because they automatically pass in the source filename and line number for you.
@param p Pointer to the block of memory to free. Must be non-NULL block of memory that was allocated with this allocator. Will assert otherwise.
@param file The source code filename that is performing the free. Used for tracking allocations and reporting on memory leaks.
@param line The line number in the source code file that is performing the free.
*/
void Free( void * p, const char * file, int line );
private:
DefaultAllocator( const DefaultAllocator & other );
DefaultAllocator & operator = ( const DefaultAllocator & other );
};
/**
Allocator built on the TLSF allocator implementation by Matt Conte. Thanks Matt!
This is a fast allocator that supports multiple heaps. It's used inside the yojimbo server to silo allocations for each client to their own heap.
See https://github.com/mattconte/tlsf for details on this allocator implementation.
*/
class TLSF_Allocator : public Allocator
{
public:
/**
TLSF allocator constructor.
If you want to integrate your own allocator with yojimbo for use with the client and server, this class is a good template to start from.
Make sure your constructor has the same signature as this one, and it will work with the YOJIMBO_SERVER_ALLOCATOR and YOJIMBO_CLIENT_ALLOCATOR helper macros.
@param memory Block of memory in which the allocator will work. This block must remain valid while this allocator exists. The allocator does not assume ownership of it, you must free it elsewhere, if necessary.
@param bytes The size of the block of memory (bytes). The maximum amount of memory you can allocate will be less, due to allocator overhead.
*/
TLSF_Allocator( void * memory, size_t bytes );
/**
TLSF allocator destructor.
Checks for memory leaks in debug build. Free all memory allocated by this allocator before destroying.
*/
~TLSF_Allocator();
/**
Allocates a block of memory using TLSF.
IMPORTANT: Don't call this directly. Use the YOJIMBO_NEW or YOJIMBO_ALLOCATE macros instead, because they automatically pass in the source filename and line number for you.
@param size The size of the block of memory to allocate (bytes).
@param file The source code filename that is performing the allocation. Used for tracking allocations and reporting on memory leaks.
@param line The line number in the source code file that is performing the allocation.
@returns A block of memory of the requested size, or NULL if the allocation could not be performed. If NULL is returned, the error level is set to ALLOCATION_ERROR_FAILED_TO_ALLOCATE.
*/
void * Allocate( size_t size, const char * file, int line );
/**
Free a block of memory using TLSF.
IMPORTANT: Don't call this directly. Use the YOJIMBO_DELETE or YOJIMBO_FREE macros instead, because they automatically pass in the source filename and line number for you.
@param p Pointer to the block of memory to free. Must be non-NULL block of memory that was allocated with this allocator. Will assert otherwise.
@param file The source code filename that is performing the free. Used for tracking allocations and reporting on memory leaks.
@param line The line number in the source code file that is performing the free.
@see Allocator::Allocate
@see Allocator::GetError
*/
void Free( void * p, const char * file, int line );
private:
tlsf_t m_tlsf; ///< The TLSF allocator instance backing this allocator.
TLSF_Allocator( const TLSF_Allocator & other );
TLSF_Allocator & operator = ( const TLSF_Allocator & other );
};
}
#endif