-
Notifications
You must be signed in to change notification settings - Fork 7
/
allocator.h
397 lines (295 loc) · 12.1 KB
/
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
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
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_ALLOCATOR_H
#define EASTL_ALLOCATOR_H
#include <eastl/internal/config.h>
#include <eastl/EABase/nullptr.h>
#include <stddef.h>
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// alloc_flags
///
/// Defines allocation flags.
///
enum alloc_flags
{
MEM_TEMP = 0, // Low memory, not necessarily actually temporary.
MEM_PERM = 1 // High memory, for things that won't be unloaded.
};
/// allocator
///
/// In this allocator class, note that it is not templated on any type and
/// instead it simply allocates blocks of memory much like the C malloc and
/// free functions. It can be thought of as similar to C++ std::allocator<char>.
/// The flags parameter has meaning that is specific to the allocation
///
/// C++11's std::allocator (20.6.9) doesn't have a move constructor or assignment
/// operator. This is possibly because std::allocators are associated with types
/// instead of as instances. The potential non-equivalance of C++ std::allocator
/// instances has been a source of some acknowledged design problems.
/// We don't implement support for move construction or assignment in eastl::allocator,
/// but users can define their own allocators which do have move functions and
/// the eastl containers are compatible with such allocators (i.e. nothing unexpected
/// will happen).
///
class EASTL_API allocator
{
public:
EASTL_ALLOCATOR_EXPLICIT allocator(const char* pName = EASTL_NAME_VAL(EASTL_ALLOCATOR_DEFAULT_NAME));
allocator(const allocator& x);
allocator(const allocator& x, const char* pName);
allocator& operator=(const allocator& x);
void* allocate(size_t n, int flags = 0);
void* allocate(size_t n, size_t alignment, size_t offset, int flags = 0);
void deallocate(void* p, size_t n);
const char* getName() const;
void setName(const char* pName);
protected:
#if EASTL_NAME_ENABLED
const char* mpName; // Debug name, used to track memory.
#endif
};
bool operator==(const allocator& a, const allocator& b);
#if !defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
bool operator!=(const allocator& a, const allocator& b);
#endif
/// dummy_allocator
///
/// Defines an allocator which does nothing. It returns NULL from allocate calls.
///
class EASTL_API dummy_allocator
{
public:
EASTL_ALLOCATOR_EXPLICIT dummy_allocator(const char* = NULL) { }
dummy_allocator(const dummy_allocator&) { }
dummy_allocator(const dummy_allocator&, const char*) { }
dummy_allocator& operator=(const dummy_allocator&) { return *this; }
void* allocate(size_t, int = 0) { return NULL; }
void* allocate(size_t, size_t, size_t, int = 0) { return NULL; }
void deallocate(void*, size_t) { }
const char* getName() const { return ""; }
void setName(const char*) { }
};
inline bool operator==(const dummy_allocator&, const dummy_allocator&) { return true; }
#if !defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
inline bool operator!=(const dummy_allocator&, const dummy_allocator&) { return false; }
#endif
/// Defines a static default allocator which is constant across all types.
/// This is different from getDefaultAllocator, which is is bound at
/// compile-time and expected to differ per allocator type.
/// Currently this Default Allocator applies only to CoreAllocatorAdapter.
/// To consider: This naming of this function is too similar to getDefaultAllocator
/// and instead should be named something like GetStaticDefaultAllocator.
EASTL_API allocator* GetDefaultAllocator();
EASTL_API allocator* SetDefaultAllocator(allocator* pAllocator);
/// getDefaultAllocator
///
/// This templated function allows the user to implement a default allocator
/// retrieval function that any part of EASTL can use. EASTL containers take
/// an Allocator parameter which identifies an Allocator class to use. But
/// different kinds of allocators have different mechanisms for retrieving
/// a default allocator instance, and some don't even intrinsically support
/// such functionality. The user can override this getDefaultAllocator
/// function in order to provide the glue between EASTL and whatever their
/// system's default allocator happens to be.
///
/// Example usage:
/// MyAllocatorType* gpSystemAllocator;
///
/// MyAllocatorType* getDefaultAllocator(const MyAllocatorType*)
/// { return gpSystemAllocator; }
///
template <typename Allocator>
Allocator* getDefaultAllocator(const Allocator*);
EASTLAllocatorType* getDefaultAllocator(const EASTLAllocatorType*);
/// default_allocfreemethod
///
/// Implements a default allocfreemethod which uses the default global allocator.
/// This version supports only default alignment.
///
void* default_allocfreemethod(size_t n, void* pBuffer, void* /*pContext*/);
/// allocate_memory
///
/// This is a memory allocation dispatching function.
/// To do: Make aligned and unaligned specializations.
/// Note that to do this we will need to use a class with a static
/// function instead of a standalone function like below.
///
template <typename Allocator>
void* allocate_memory(Allocator& a, size_t n, size_t alignment, size_t alignmentOffset);
} // namespace eastl
#ifndef EASTL_USER_DEFINED_ALLOCATOR // If the user hasn't declared that he has defined a different allocator implementation elsewhere...
EA_DISABLE_ALL_VC_WARNINGS()
#include <new>
EA_RESTORE_ALL_VC_WARNINGS()
#if !EASTL_DLL // If building a regular library and not building EASTL as a DLL...
// It is expected that the application define the following
// versions of operator new for the application. Either that or the
// user needs to override the implementation of the allocator class.
void* operator new[](size_t size, const char* pName, int flags, unsigned debugFlags, const char* file, int line);
void* operator new[](size_t size, size_t alignment, size_t alignmentOffset, const char* pName, int flags, unsigned debugFlags, const char* file, int line);
#endif
namespace eastl
{
inline allocator::allocator(const char* EASTL_NAME(pName))
{
#if EASTL_NAME_ENABLED
mpName = pName ? pName : EASTL_ALLOCATOR_DEFAULT_NAME;
#endif
}
inline allocator::allocator(const allocator& EASTL_NAME(alloc))
{
#if EASTL_NAME_ENABLED
mpName = alloc.mpName;
#endif
}
inline allocator::allocator(const allocator&, const char* EASTL_NAME(pName))
{
#if EASTL_NAME_ENABLED
mpName = pName ? pName : EASTL_ALLOCATOR_DEFAULT_NAME;
#endif
}
inline allocator& allocator::operator=(const allocator& EASTL_NAME(alloc))
{
#if EASTL_NAME_ENABLED
mpName = alloc.mpName;
#endif
return *this;
}
inline const char* allocator::getName() const
{
#if EASTL_NAME_ENABLED
return mpName;
#else
return EASTL_ALLOCATOR_DEFAULT_NAME;
#endif
}
inline void allocator::setName(const char* EASTL_NAME(pName))
{
#if EASTL_NAME_ENABLED
mpName = pName;
#endif
}
inline void* allocator::allocate(size_t n, int flags)
{
#if EASTL_NAME_ENABLED
#define pName mpName
#else
#define pName EASTL_ALLOCATOR_DEFAULT_NAME
#endif
#if EASTL_DLL
return allocate(n, EASTL_SYSTEM_ALLOCATOR_MIN_ALIGNMENT, 0, flags);
#elif (EASTL_DEBUGPARAMS_LEVEL <= 0)
return ::new((char*)0, flags, 0, (char*)0, 0) char[n];
#elif (EASTL_DEBUGPARAMS_LEVEL == 1)
return ::new( pName, flags, 0, (char*)0, 0) char[n];
#else
return ::new( pName, flags, 0, __FILE__, __LINE__) char[n];
#endif
}
inline void* allocator::allocate(size_t n, size_t alignment, size_t offset, int flags)
{
#if EASTL_DLL
// We currently have no support for implementing flags when
// using the C runtime library operator new function. The user
// can use SetDefaultAllocator to override the default allocator.
EA_UNUSED(offset); EA_UNUSED(flags);
size_t adjustedAlignment = (alignment > EA_PLATFORM_PTR_SIZE) ? alignment : EA_PLATFORM_PTR_SIZE;
void* p = new char[n + adjustedAlignment + EA_PLATFORM_PTR_SIZE];
void* pPlusPointerSize = (void*)((uintptr_t)p + EA_PLATFORM_PTR_SIZE);
void* pAligned = (void*)(((uintptr_t)pPlusPointerSize + adjustedAlignment - 1) & ~(adjustedAlignment - 1));
void** pStoredPtr = (void**)pAligned - 1;
EASTL_ASSERT(pStoredPtr >= p);
*(pStoredPtr) = p;
EASTL_ASSERT(((size_t)pAligned & ~(alignment - 1)) == (size_t)pAligned);
return pAligned;
#elif (EASTL_DEBUGPARAMS_LEVEL <= 0)
return ::new(alignment, offset, (char*)0, flags, 0, (char*)0, 0) char[n];
#elif (EASTL_DEBUGPARAMS_LEVEL == 1)
return ::new(alignment, offset, pName, flags, 0, (char*)0, 0) char[n];
#else
return ::new(alignment, offset, pName, flags, 0, __FILE__, __LINE__) char[n];
#endif
#undef pName // See above for the definition of this.
}
inline void allocator::deallocate(void* p, size_t)
{
#if EASTL_DLL
if (p != nullptr)
{
void* pOriginalAllocation = *((void**)p - 1);
delete[](char*)pOriginalAllocation;
}
#else
delete[](char*)p;
#endif
}
inline bool operator==(const allocator&, const allocator&)
{
return true; // All allocators are considered equal, as they merely use global new/delete.
}
#if !defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
inline bool operator!=(const allocator&, const allocator&)
{
return false; // All allocators are considered equal, as they merely use global new/delete.
}
#endif
} // namespace eastl
#endif // EASTL_USER_DEFINED_ALLOCATOR
namespace eastl
{
template <typename Allocator>
inline Allocator* getDefaultAllocator(const Allocator*)
{
return NULL; // By default we return NULL; the user must make specialization of this function in order to provide their own implementation.
}
inline EASTLAllocatorType* getDefaultAllocator(const EASTLAllocatorType*)
{
return EASTLAllocatorDefault(); // For the built-in allocator EASTLAllocatorType, we happen to already have a function for returning the default allocator instance, so we provide it.
}
inline void* default_allocfreemethod(size_t n, void* pBuffer, void* /*pContext*/)
{
EASTLAllocatorType* const pAllocator = EASTLAllocatorDefault();
if(pBuffer) // If freeing...
{
EASTLFree(*pAllocator, pBuffer, n);
return NULL; // The return value is meaningless for the free.
}
else // allocating
return EASTLAlloc(*pAllocator, n);
}
/// allocate_memory
///
/// This is a memory allocation dispatching function.
/// To do: Make aligned and unaligned specializations.
/// Note that to do this we will need to use a class with a static
/// function instead of a standalone function like below.
///
template <typename Allocator>
inline void* allocate_memory(Allocator& a, size_t n, size_t alignment, size_t alignmentOffset)
{
void *result;
if (alignment <= EASTL_ALLOCATOR_MIN_ALIGNMENT)
{
result = EASTLAlloc(a, n);
// Ensure the result is correctly aligned. An assertion likely indicates a mismatch between EASTL_ALLOCATOR_MIN_ALIGNMENT and the minimum alignment
// of EASTLAlloc. If there is a mismatch it may be necessary to define EASTL_ALLOCATOR_MIN_ALIGNMENT to be the minimum alignment of EASTLAlloc, or
// to increase the alignment of EASTLAlloc to match EASTL_ALLOCATOR_MIN_ALIGNMENT.
EASTL_ASSERT((reinterpret_cast<size_t>(result)& ~(alignment - 1)) == reinterpret_cast<size_t>(result));
}
else
{
result = EASTLAllocAligned(a, n, alignment, alignmentOffset);
// Ensure the result is correctly aligned. An assertion here may indicate a bug in the allocator.
auto resultMinusOffset = (char*)result - alignmentOffset;
EA_UNUSED(resultMinusOffset);
EASTL_ASSERT((reinterpret_cast<size_t>(resultMinusOffset)& ~(alignment - 1)) == reinterpret_cast<size_t>(resultMinusOffset));
}
return result;
}
}
#endif // Header include guard