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
| #ifndef MEMORY_POOL_H_ #define MEMORY_POOL_H_
#include <climits> #include <cstddef> #include <mutex>
template <typename T, size_t BlockSize = 4096, bool ZeroOnDeallocate = true> class MemoryPool { public: typedef T value_type; typedef T* pointer; typedef T& reference; typedef const T* const_pointer; typedef const T& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef std::false_type propagate_on_container_copy_assignment; typedef std::true_type propagate_on_container_move_assignment; typedef std::true_type propagate_on_container_swap;
template <typename U> struct rebind { typedef MemoryPool<U> other; };
MemoryPool() noexcept; MemoryPool(const MemoryPool& memoryPool) noexcept; MemoryPool(MemoryPool&& memoryPool) noexcept; template <class U> MemoryPool(const MemoryPool<U>& memoryPool) noexcept;
~MemoryPool() noexcept;
MemoryPool& operator=(const MemoryPool& memoryPool) = delete; MemoryPool& operator=(MemoryPool&& memoryPool) noexcept;
pointer address(reference x) const noexcept; const_pointer address(const_reference x) const noexcept;
pointer allocate(size_type n = 1, const_pointer hint = 0); void deallocate(pointer p, size_type n = 1);
size_type max_size() const noexcept;
template <class U, class... Args> void construct(U* p, Args&&... args); template <class U> void destroy(U* p);
template <class... Args> pointer newElement(Args&&... args); void deleteElement(pointer p);
private: struct Element_ { Element_* pre; Element_* next; };
typedef char* data_pointer; typedef Element_ element_type; typedef Element_* element_pointer;
element_pointer data_element_; element_pointer free_element_;
std::recursive_mutex m_;
size_type padPointer(data_pointer p, size_type align) const noexcept; void allocateBlock();
static_assert(BlockSize >= 2 * sizeof(element_type), "BlockSize too small."); };
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline typename MemoryPool<T, BlockSize, ZeroOnDeallocate>::size_type MemoryPool<T, BlockSize, ZeroOnDeallocate>::padPointer(data_pointer p, size_type align) const noexcept { uintptr_t result = reinterpret_cast<uintptr_t>(p); return ((align - result) % align); }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> MemoryPool<T, BlockSize, ZeroOnDeallocate>::MemoryPool() noexcept { data_element_ = nullptr; free_element_ = nullptr; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> MemoryPool<T, BlockSize, ZeroOnDeallocate>::MemoryPool(const MemoryPool& memoryPool) noexcept : MemoryPool() { }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> MemoryPool<T, BlockSize, ZeroOnDeallocate>::MemoryPool(MemoryPool&& memoryPool) noexcept { std::lock_guard<std::recursive_mutex> lock(m_);
data_element_ = memoryPool.data_element_; memoryPool.data_element_ = nullptr; free_element_ = memoryPool.free_element_; memoryPool.free_element_ = nullptr; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> template<class U> MemoryPool<T, BlockSize, ZeroOnDeallocate>::MemoryPool(const MemoryPool<U>& memoryPool) noexcept : MemoryPool() { }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> MemoryPool<T, BlockSize, ZeroOnDeallocate>& MemoryPool<T, BlockSize, ZeroOnDeallocate>::operator=(MemoryPool&& memoryPool) noexcept { std::lock_guard<std::recursive_mutex> lock(m_);
if (this != &memoryPool) { std::swap(data_element_, memoryPool.data_element_); std::swap(free_element_, memoryPool.free_element_); } return *this; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> MemoryPool<T, BlockSize, ZeroOnDeallocate>::~MemoryPool() noexcept { std::lock_guard<std::recursive_mutex> lock(m_);
element_pointer curr = data_element_; while (curr != nullptr) { element_pointer prev = curr->next; operator delete(reinterpret_cast<void*>(curr)); curr = prev; }
curr = free_element_; while (curr != nullptr) { element_pointer prev = curr->next; operator delete(reinterpret_cast<void*>(curr)); curr = prev; } }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline typename MemoryPool<T, BlockSize, ZeroOnDeallocate>::pointer MemoryPool<T, BlockSize, ZeroOnDeallocate>::address(reference x) const noexcept { return &x; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline typename MemoryPool<T, BlockSize, ZeroOnDeallocate>::const_pointer MemoryPool<T, BlockSize, ZeroOnDeallocate>::address(const_reference x) const noexcept { return &x; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> void MemoryPool<T, BlockSize, ZeroOnDeallocate>::allocateBlock() { data_pointer new_block = reinterpret_cast<data_pointer> (operator new(BlockSize)); element_pointer new_ele_pointer = reinterpret_cast<element_pointer>(new_block); new_ele_pointer->pre = nullptr; new_ele_pointer->next = nullptr;
if (data_element_) { data_element_->pre = new_ele_pointer; }
new_ele_pointer->next = data_element_; data_element_ = new_ele_pointer; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline typename MemoryPool<T, BlockSize, ZeroOnDeallocate>::pointer MemoryPool<T, BlockSize, ZeroOnDeallocate>::allocate(size_type n, const_pointer hint) { std::lock_guard<std::recursive_mutex> lock(m_);
if (free_element_ != nullptr) { data_pointer body = reinterpret_cast<data_pointer>(reinterpret_cast<data_pointer>(free_element_) + sizeof(element_type));
size_type bodyPadding = padPointer(body, alignof(element_type));
pointer result = reinterpret_cast<pointer>(reinterpret_cast<data_pointer>(body + bodyPadding));
element_pointer tmp = free_element_;
free_element_ = free_element_->next;
if (free_element_) free_element_->pre = nullptr;
tmp->next = data_element_; if (data_element_) data_element_->pre = tmp; tmp->pre = nullptr; data_element_ = tmp;
return result; } else { allocateBlock();
data_pointer body = reinterpret_cast<data_pointer>(reinterpret_cast<data_pointer>(data_element_) + sizeof(element_type));
size_type bodyPadding = padPointer(body, alignof(element_type));
pointer result = reinterpret_cast<pointer>(reinterpret_cast<data_pointer>(body + bodyPadding));
return result; } }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline void MemoryPool<T, BlockSize, ZeroOnDeallocate>::deallocate(pointer p, size_type n) { std::lock_guard<std::recursive_mutex> lock(m_);
if (p != nullptr) { element_pointer ele_p = reinterpret_cast<element_pointer>(reinterpret_cast<data_pointer>(p) - sizeof(element_type));
if (ZeroOnDeallocate) { memset(reinterpret_cast<data_pointer>(p), 0, BlockSize - sizeof(element_type)); }
if (ele_p->pre) { ele_p->pre->next = ele_p->next; }
if (ele_p->next) { ele_p->next->pre = ele_p->pre; }
if (ele_p->pre == nullptr) { data_element_ = ele_p->next; }
ele_p->pre = nullptr; if (free_element_) { ele_p->next = free_element_; free_element_->pre = ele_p; } else { ele_p->next = nullptr; } free_element_ = ele_p; } }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline typename MemoryPool<T, BlockSize, ZeroOnDeallocate>::size_type MemoryPool<T, BlockSize, ZeroOnDeallocate>::max_size() const noexcept { size_type maxBlocks = -1 / BlockSize; return (BlockSize - sizeof(data_pointer)) / sizeof(element_type) * maxBlocks; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> template <class U, class... Args> inline void MemoryPool<T, BlockSize, ZeroOnDeallocate>::construct(U* p, Args&&... args) { new (p) U(std::forward<Args>(args)...); }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> template <class U> inline void MemoryPool<T, BlockSize, ZeroOnDeallocate>::destroy(U* p) { p->~U(); }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> template <class... Args> inline typename MemoryPool<T, BlockSize, ZeroOnDeallocate>::pointer MemoryPool<T, BlockSize, ZeroOnDeallocate>::newElement(Args&&... args) { std::lock_guard<std::recursive_mutex> lock(m_); pointer result = allocate(); construct<value_type>(result, std::forward<Args>(args)...); return result; }
template <typename T, size_t BlockSize, bool ZeroOnDeallocate> inline void MemoryPool<T, BlockSize, ZeroOnDeallocate>::deleteElement(pointer p) { std::lock_guard<std::recursive_mutex> lock(m_); if (p != nullptr) { p->~value_type(); deallocate(p); } }
#endif
|