6 #ifndef EASTL_ALLOCATOR_H
7 #define EASTL_ALLOCATOR_H
10 #include <EASTL/internal/config.h>
11 #include <EABase/nullptr.h>
15 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
54 EASTL_ALLOCATOR_EXPLICIT
allocator(
const char* pName = EASTL_NAME_VAL(EASTL_ALLOCATOR_DEFAULT_NAME));
60 void* allocate(
size_t n,
int flags = 0);
61 void* allocate(
size_t n,
size_t alignment,
size_t offset,
int flags = 0);
62 void deallocate(
void* p,
size_t n);
64 const char* get_name()
const;
65 void set_name(
const char* pName);
68 #if EASTL_NAME_ENABLED
91 void* allocate(
size_t,
int = 0) {
return NULL; }
92 void* allocate(
size_t,
size_t,
size_t,
int = 0) {
return NULL; }
93 void deallocate(
void*,
size_t) { }
95 const char* get_name()
const {
return ""; }
96 void set_name(
const char*) { }
100 inline bool operator!=(
const dummy_allocator&,
const dummy_allocator&) {
return false; }
111 EASTL_API allocator* SetDefaultAllocator(allocator* pAllocator);
131 template <
typename Allocator>
152 template <
typename Allocator>
153 void*
allocate_memory(Allocator& a,
size_t n,
size_t alignment,
size_t alignmentOffset);
163 #ifndef EASTL_USER_DEFINED_ALLOCATOR
165 EA_DISABLE_ALL_VC_WARNINGS()
167 EA_RESTORE_ALL_VC_WARNINGS()
173 inline void*
operator new[](
size_t size,
const char* pName,
int flags,
unsigned debugFlags,
const char* file,
int line) {
return operator new(size); }
174 inline void*
operator new[](
size_t size,
size_t alignment,
size_t alignmentOffset,
const char* pName,
int flags,
unsigned debugFlags,
const char* file,
int line) {
return operator new(size); }
179 inline allocator::allocator(
const char* EASTL_NAME(pName))
181 #if EASTL_NAME_ENABLED
182 mpName = pName ? pName : EASTL_ALLOCATOR_DEFAULT_NAME;
187 inline allocator::allocator(
const allocator& EASTL_NAME(alloc))
189 #if EASTL_NAME_ENABLED
190 mpName = alloc.mpName;
195 inline allocator::allocator(
const allocator&,
const char* EASTL_NAME(pName))
197 #if EASTL_NAME_ENABLED
198 mpName = pName ? pName : EASTL_ALLOCATOR_DEFAULT_NAME;
203 inline allocator& allocator::operator=(
const allocator& EASTL_NAME(alloc))
205 #if EASTL_NAME_ENABLED
206 mpName = alloc.mpName;
212 inline const char* allocator::get_name()
const
214 #if EASTL_NAME_ENABLED
217 return EASTL_ALLOCATOR_DEFAULT_NAME;
222 inline void allocator::set_name(
const char* EASTL_NAME(pName))
224 #if EASTL_NAME_ENABLED
230 inline void* allocator::allocate(
size_t n,
int flags)
232 #if EASTL_NAME_ENABLED
235 #define pName EASTL_ALLOCATOR_DEFAULT_NAME
239 return allocate(n, EASTL_SYSTEM_ALLOCATOR_MIN_ALIGNMENT, 0, flags);
240 #elif (EASTL_DEBUGPARAMS_LEVEL <= 0)
241 return ::new((
char*)0, flags, 0, (
char*)0, 0) char[n];
242 #elif (EASTL_DEBUGPARAMS_LEVEL == 1)
243 return ::new( pName, flags, 0, (
char*)0, 0) char[n];
245 return ::new( pName, flags, 0, __FILE__, __LINE__)
char[n];
250 inline void* allocator::allocate(
size_t n,
size_t alignment,
size_t offset,
int flags)
256 EA_UNUSED(offset); EA_UNUSED(flags);
258 size_t adjustedAlignment = (alignment > EA_PLATFORM_PTR_SIZE) ? alignment : EA_PLATFORM_PTR_SIZE;
260 void* p =
new char[n + adjustedAlignment + EA_PLATFORM_PTR_SIZE];
261 void* pPlusPointerSize = (
void*)((uintptr_t)p + EA_PLATFORM_PTR_SIZE);
262 void* pAligned = (
void*)(((uintptr_t)pPlusPointerSize + adjustedAlignment - 1) & ~(adjustedAlignment - 1));
264 void** pStoredPtr = (
void**)pAligned - 1;
265 EASTL_ASSERT(pStoredPtr >= p);
268 EASTL_ASSERT(((
size_t)pAligned & ~(alignment - 1)) == (
size_t)pAligned);
271 #elif (EASTL_DEBUGPARAMS_LEVEL <= 0)
272 return ::new(alignment, offset, (
char*)0, flags, 0, (
char*)0, 0) char[n];
273 #elif (EASTL_DEBUGPARAMS_LEVEL == 1)
274 return ::new(alignment, offset, pName, flags, 0, (
char*)0, 0) char[n];
276 return ::new(alignment, offset, pName, flags, 0, __FILE__, __LINE__)
char[n];
283 inline void allocator::deallocate(
void* p,
size_t)
288 void* pOriginalAllocation = *((
void**)p - 1);
289 delete[](
char*)pOriginalAllocation;
297 inline bool operator==(
const allocator&,
const allocator&)
303 inline bool operator!=(
const allocator&,
const allocator&)
319 template <
typename Allocator>
328 return EASTLAllocatorDefault();
334 EASTLAllocatorType*
const pAllocator = EASTLAllocatorDefault();
338 EASTLFree(*pAllocator, pBuffer, n);
342 return EASTLAlloc(*pAllocator, n);
353 template <
typename Allocator>
354 inline void*
allocate_memory(Allocator& a,
size_t n,
size_t alignment,
size_t alignmentOffset)
357 if (alignment <= EASTL_ALLOCATOR_MIN_ALIGNMENT)
359 result = EASTLAlloc(a, n);
363 EASTL_ASSERT((
reinterpret_cast<size_t>(result)& ~(alignment - 1)) ==
reinterpret_cast<size_t>(result));
367 result = EASTLAllocAligned(a, n, alignment, alignmentOffset);
369 auto resultMinusOffset = (
char*)result - alignmentOffset;
370 EA_UNUSED(resultMinusOffset);
371 EASTL_ASSERT((
reinterpret_cast<size_t>(resultMinusOffset)& ~(alignment - 1)) ==
reinterpret_cast<size_t>(resultMinusOffset));
Definition: allocator.h:52
Definition: allocator.h:83
EA Standard Template Library.
Definition: algorithm.h:288
alloc_flags
Definition: allocator.h:29
Allocator * get_default_allocator(const Allocator *)
Definition: allocator.h:320
EASTL_API allocator * GetDefaultAllocator()
Definition: allocator_eastl.cpp:30
void * allocate_memory(Allocator &a, size_t n, size_t alignment, size_t alignmentOffset)
Definition: allocator.h:354
void * default_allocfreemethod(size_t n, void *pBuffer, void *)
Definition: allocator.h:332