14 #define new_ new (__FILE__, __LINE__)
19 inline void*
operator new(
size_t size,
const char* srcFile,
int srcLine) {
mt_unused(srcFile);
mt_unused(srcLine);
return operator new(
size); }
20 inline void*
operator new[](
size_t size,
const char* srcFile,
int srcLine) {
mt_unused(srcFile);
mt_unused(srcLine);
return operator new(
size); }
31 T*
alloc(
szt count = 1) {
return static_cast<T*
>(
operator new(
sizeof(T)*count)); }
34 void free(T*& p) {
if (!p)
return;
operator delete(p); p =
nullptr; }
36 void free(T*
const& p) {
if (!p)
return;
operator delete(p); }
40 T*
alignFloor(T* p,
szt bytes) {
return reinterpret_cast<T*
>(intptr_t(p) & ~(bytes-1)); }
46 template<
class T,
class Alloc>
49 int8* base = a.allocate(
sizeof(
sdt) + align_-1 +
sizeof(T)*count);
50 if (!base)
return nullptr;
52 *
reinterpret_cast<sdt*
>(p-
sizeof(
sdt)) = p - base;
53 return reinterpret_cast<T*
>(p);
57 T*
allocAligned(
szt count,
szt align) {
return allocAligned<T>(count, align, std::allocator<int8>()); }
60 template<
class T,
class Alloc>
64 int8* p_ =
reinterpret_cast<int8*
>(p);
65 int8* base = p_ - *
reinterpret_cast<sdt*
>(p_ -
sizeof(
sdt));
66 a.deallocate(base, 1);
75 void delete_(T*& p) {
delete p; p =
nullptr; }
80 template<
class T,
class Alloc>
81 void delete_(T*& p, Alloc&& a) {
if (!p)
return; a.destroy(p); a.deallocate(p,1); p =
nullptr; }
82 template<
class T,
class Alloc>
83 void delete_(T*
const& p, Alloc&& a) {
if (!p)
return; a.destroy(p); a.deallocate(p,1); }
99 template<
template<
class>
class Subclass,
class T>
111 pointer
address(reference x)
const {
return &x; }
112 const_pointer
address(const_reference x)
const {
return &x; }
113 size_type
max_size()
const {
return std::numeric_limits<size_type>::max(); }
114 template<
class U,
class... Args>
115 void construct(U* p, Args&&... args) {
new ((
void*)p) U(forward<Args>(args)...); }
123 Subclass<T>&
subc() {
return static_cast<Subclass<T>&
>(*this); }
124 const Subclass<T>&
subc()
const {
return static_cast<const Subclass<T>&
>(*this); }
128 template<
template<
class>
class Alloc>
135 void*
operator new(
szt,
void* ptr) {
return ptr; }
136 void*
operator new(szt
size,
const char* srcFile,
int srcLine) {
return _alloc.allocate(size, srcFile, srcLine); }
138 void*
operator new[](szt
size) {
return _alloc.allocate(size); }
139 void*
operator new[](
szt,
void* ptr) {
return ptr; }
140 void*
operator new[](szt
size,
const char* srcFile,
int srcLine) {
return _alloc.allocate(size, srcFile, srcLine); }
142 void operator delete(
void* p) { _alloc.deallocate(static_cast<int8*>(p), 1); }
143 void operator delete[](
void* p) { _alloc.deallocate(static_cast<int8*>(p), 1); }
146 static Alloc<int8> _alloc;
148 template<
template<
class>
class Alloc> Alloc<int8> AllocatorObject<Alloc>::_alloc;
151 template<
class T,
class = std::true_type>
160 template<class T, class Alloc = typename DefaultAllocator<T>::type>
Definition: Allocator.h:118
void free(T *&p)
Deallocate memory and set pointer to null. Object is not destroyed.
Definition: Allocator.h:34
bool operator==(const Subclass< T > &) const
Definition: Allocator.h:119
const_pointer address(const_reference x) const
Definition: Allocator.h:112
T * alignCeil(T *p, szt bytes)
Align a pointer to the next byte boundary bytes. Does nothing if p is already on boundary. Alignment must be a power of two.
Definition: Allocator.h:43
Subclass< U > other
Definition: Allocator.h:118
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
void delete_(T *&p)
Destruct object, free memory and set pointer to null.
Definition: Allocator.h:75
bool operator!=(const Subclass< T > &) const
Definition: Allocator.h:120
void destroy(U *p)
Definition: Allocator.h:117
T * pointer
Definition: Allocator.h:104
T * alignFloor(T *p, szt bytes)
Align a pointer to the previous byte boundary bytes. Does nothing if p is already on boundary...
Definition: Allocator.h:40
void operator()(T *&p)
Definition: Allocator.h:172
char int8
Definition: Core.h:11
Subclass< T > & subc()
Definition: Allocator.h:123
Alloc< T > Allocator
Definition: Allocator.h:132
const T * const_pointer
Definition: Allocator.h:106
szt size_type
Definition: Allocator.h:108
Objects that inherit from this class will use Alloc for new/delete ops.
Definition: Allocator.h:129
std::allocator compatible allocator
Definition: Allocator.h:100
T * allocAligned(szt count, szt align_, Alloc &&a)
Allocate memory with alignment. Alignment must be a power of two. Allocator element type must be int8...
Definition: Allocator.h:47
void operator()(void *&p)
Definition: Allocator.h:179
std::allocator< T > type
Definition: Allocator.h:154
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
T * alloc(szt count=1)
Allocate memory for count number of T objects. Objects are not constructed.
Definition: Allocator.h:31
Alloc a
Definition: Allocator.h:166
void operator()(T *const &p)
Definition: Allocator.h:165
sdt difference_type
Definition: Allocator.h:109
Functor to delete a pointer.
Definition: Allocator.h:161
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
void operator()(T *const &p)
Definition: Allocator.h:173
T value_type
Definition: Allocator.h:103
void deleteArray(T *&p)
Destruct all array objects, free memory and set pointer to null.
Definition: Allocator.h:87
void construct(U *p, Args &&...args)
Definition: Allocator.h:115
const T & const_reference
Definition: Allocator.h:107
finalize(Alloc a=Alloc())
Definition: Allocator.h:163
pointer address(reference x) const
Definition: Allocator.h:111
void operator()(T *&p)
Definition: Allocator.h:164
Returns T::Allocator if available, otherwise std::allocator
Definition: Allocator.h:152
T::template Allocator< T > type
Definition: Allocator.h:157
void freeAligned(T *p, Alloc &&a)
Deallocate aligned memory. Allocator element type must be int8.
Definition: Allocator.h:61
T & reference
Definition: Allocator.h:105
size_type max_size() const
Definition: Allocator.h:113
std::allocator< T > type
Definition: Allocator.h:152
Global Honeycomb namespace.
void operator()(void *const &p)
Definition: Allocator.h:180
const Subclass< T > & subc() const
Definition: Allocator.h:124