Honeycomb  0.1
Component-Model Framework
Allocator.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Core/Meta.h"
5 
7 
10 
13 #ifdef DEBUG
14  #define new_ new (__FILE__, __LINE__)
15 #else
16  #define new_ new
17 #endif
18 
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); }
22 
23 namespace honey
24 {
25 
28 
30 template<class T>
31 T* alloc(szt count = 1) { return static_cast<T*>(operator new(sizeof(T)*count)); }
33 template<class T>
34 void free(T*& p) { if (!p) return; operator delete(p); p = nullptr; }
35 template<class T>
36 void free(T* const& p) { if (!p) return; operator delete(p); }
37 
39 template<class T>
40 T* alignFloor(T* p, szt bytes) { return reinterpret_cast<T*>(intptr_t(p) & ~(bytes-1)); }
42 template<class T>
43 T* alignCeil(T* p, szt bytes) { return alignFloor(reinterpret_cast<T*>(intptr_t(p) + bytes-1), bytes); }
44 
46 template<class T, class Alloc>
47 T* allocAligned(szt count, szt align_, Alloc&& a)
48 {
49  int8* base = a.allocate(sizeof(sdt) + align_-1 + sizeof(T)*count);
50  if (!base) return nullptr;
51  int8* p = alignCeil(base+sizeof(sdt), align_);
52  *reinterpret_cast<sdt*>(p-sizeof(sdt)) = p - base;
53  return reinterpret_cast<T*>(p);
54 }
56 template<class T>
57 T* allocAligned(szt count, szt align) { return allocAligned<T>(count, align, std::allocator<int8>()); }
58 
60 template<class T, class Alloc>
61 void freeAligned(T* p, Alloc&& a)
62 {
63  if (!p) return;
64  int8* p_ = reinterpret_cast<int8*>(p);
65  int8* base = p_ - *reinterpret_cast<sdt*>(p_ - sizeof(sdt));
66  a.deallocate(base, 1);
67 }
69 template<class T>
70 void freeAligned(T* p) { freeAligned(p, std::allocator<int8>()); }
71 
72 
74 template<class T>
75 void delete_(T*& p) { delete p; p = nullptr; }
76 template<class T>
77 void delete_(T* const& p) { delete p; }
78 
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); }
84 
86 template<class T>
87 void deleteArray(T*& p) { delete[] p; p = nullptr; }
88 template<class T>
89 void deleteArray(T* const& p) { delete[] p; }
90 
92 
99 template<template<class> class Subclass, class T>
101 {
102 public:
103  typedef T value_type;
104  typedef T* pointer;
105  typedef T& reference;
106  typedef const T* const_pointer;
107  typedef const T& const_reference;
108  typedef szt size_type;
110 
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)...); }
116  template<class U>
117  void destroy(U* p) { p->~U(); }
118  template<class U> struct rebind { typedef Subclass<U> other; };
119  bool operator==(const Subclass<T>&) const { return true; }
120  bool operator!=(const Subclass<T>&) const { return false; }
121 
122 protected:
123  Subclass<T>& subc() { return static_cast<Subclass<T>&>(*this); }
124  const Subclass<T>& subc() const { return static_cast<const Subclass<T>&>(*this); }
125 };
126 
128 template<template<class> class Alloc>
130 {
131 public:
132  template<class T> using Allocator = Alloc<T>;
133 
134  void* operator new(szt size) { return _alloc.allocate(size); }
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); }
137 
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); }
141 
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); }
144 
145 private:
146  static Alloc<int8> _alloc;
147 };
148 template<template<class> class Alloc> Alloc<int8> AllocatorObject<Alloc>::_alloc;
149 
151 template<class T, class = std::true_type>
152 struct DefaultAllocator { typedef std::allocator<T> type; };
153 template<class T>
154 struct DefaultAllocator<T[], std::true_type> { typedef std::allocator<T> type; };
155 template<class T>
156 struct DefaultAllocator<T, typename mt::True<typename T::template Allocator<T>>::type>
157  { typedef typename T::template Allocator<T> type; };
158 
160 template<class T, class Alloc = typename DefaultAllocator<T>::type>
161 struct finalize
162 {
163  finalize(Alloc a = Alloc()) : a(move(a)) {}
164  void operator()(T*& p) { delete_(p,a); }
165  void operator()(T* const& p) { delete_(p,a); }
166  Alloc a;
167 };
169 template<class T>
170 struct finalize<T[], std::allocator<T>>
171 {
172  void operator()(T*& p) { deleteArray(p); }
173  void operator()(T* const& p) { deleteArray(p); }
174 };
176 template<>
177 struct finalize<void, std::allocator<void>>
178 {
179  void operator()(void*& p) { free(p); }
180  void operator()(void* const& p) { free(p); }
181 };
182 
184 
185 }
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
#define mt_unused(Param)
Remove the unused parameter warning.
Definition: Meta.h:20
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
STL namespace.
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
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