| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef DMLC_MEMORY_H_ |
| 7 |
#define DMLC_MEMORY_H_ |
| 8 |
|
| 9 |
#include <memory> |
| 10 |
#include <utility> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include "./base.h" |
| 14 |
#include "./logging.h" |
| 15 |
#include "./thread_local.h" |
| 16 |
|
| 17 |
namespace dmlc { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
template <size_t size, size_t align> |
| 25 |
class MemoryPool { |
| 26 |
public: |
| 27 |
|
| 28 |
MemoryPool() { |
| 29 |
static_assert(align % alignof(LinkedList) == 0, "alignment requirement failed."); |
| 30 |
curr_page_.reset(new Page()); |
| 31 |
} |
| 32 |
|
| 33 |
inline void *allocate() { |
| 34 |
if (head_ != nullptr) { |
| 35 |
LinkedList *ret = head_; |
| 36 |
head_ = head_->next; |
| 37 |
return ret; |
| 38 |
} else { |
| 39 |
if (page_ptr_ < kPageSize) { |
| 40 |
return &(curr_page_->data[page_ptr_++]); |
| 41 |
} else { |
| 42 |
allocated_.push_back(std::move(curr_page_)); |
| 43 |
curr_page_.reset(new Page()); |
| 44 |
page_ptr_ = 1; |
| 45 |
return &(curr_page_->data[0]); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
inline void deallocate(void *p) { |
| 54 |
LinkedList *ptr = static_cast<LinkedList *>(p); |
| 55 |
ptr->next = head_; |
| 56 |
head_ = ptr; |
| 57 |
} |
| 58 |
|
| 59 |
private: |
| 60 |
|
| 61 |
static const int kPageSize = ((1 << 22) / size); |
| 62 |
|
| 63 |
struct Page { |
| 64 |
typename std::aligned_storage<size, align>::type data[kPageSize]; |
| 65 |
}; |
| 66 |
|
| 67 |
struct LinkedList { |
| 68 |
LinkedList *next{nullptr}; |
| 69 |
}; |
| 70 |
|
| 71 |
LinkedList *head_{nullptr}; |
| 72 |
|
| 73 |
std::unique_ptr<Page> curr_page_; |
| 74 |
|
| 75 |
size_t page_ptr_{0}; |
| 76 |
|
| 77 |
std::vector<std::unique_ptr<Page>> allocated_; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
template <typename T> |
| 86 |
class ThreadlocalAllocator { |
| 87 |
public: |
| 88 |
|
| 89 |
typedef T *pointer; |
| 90 |
|
| 91 |
typedef const T *const_ptr; |
| 92 |
|
| 93 |
typedef T value_type; |
| 94 |
|
| 95 |
ThreadlocalAllocator() {} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
template <typename U> |
| 102 |
ThreadlocalAllocator(const ThreadlocalAllocator<U> &other) {} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
inline T *allocate(size_t n) { |
| 109 |
CHECK_EQ(n, 1); |
| 110 |
typedef ThreadLocalStore<MemoryPool<sizeof(T), alignof(T)>> Store; |
| 111 |
return static_cast<T *>(Store::Get()->allocate()); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
inline void deallocate(T *p, size_t n) { |
| 119 |
CHECK_EQ(n, 1); |
| 120 |
typedef ThreadLocalStore<MemoryPool<sizeof(T), alignof(T)>> Store; |
| 121 |
Store::Get()->deallocate(p); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
template <typename T> |
| 132 |
struct ThreadlocalSharedPtr { |
| 133 |
public: |
| 134 |
|
| 135 |
ThreadlocalSharedPtr() : block_(nullptr) {} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
ThreadlocalSharedPtr(std::nullptr_t other) : block_(nullptr) {} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
ThreadlocalSharedPtr(const ThreadlocalSharedPtr<T> &other) : block_(other.block_) { |
| 146 |
IncRef(block_); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ThreadlocalSharedPtr(ThreadlocalSharedPtr<T> &&other) : block_(other.block_) { |
| 153 |
other.block_ = nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
~ThreadlocalSharedPtr() { |
| 159 |
DecRef(block_); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
inline ThreadlocalSharedPtr<T> &operator=(ThreadlocalSharedPtr<T> &&other) { |
| 167 |
DecRef(block_); |
| 168 |
block_ = other.block_; |
| 169 |
other.block_ = nullptr; |
| 170 |
return *this; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
inline ThreadlocalSharedPtr<T> &operator=(const ThreadlocalSharedPtr<T> &other) { |
| 178 |
DecRef(block_); |
| 179 |
block_ = other.block_; |
| 180 |
IncRef(block_); |
| 181 |
return *this; |
| 182 |
} |
| 183 |
|
| 184 |
inline bool operator==(std::nullptr_t other) const { |
| 185 |
return block_ == nullptr; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
inline T *get() const { |
| 191 |
if (block_ == nullptr) { |
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
return reinterpret_cast<T *>(&(block_->data)); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
inline void reset() { |
| 200 |
DecRef(block_); |
| 201 |
block_ = nullptr; |
| 202 |
} |
| 203 |
|
| 204 |
inline bool unique() const { |
| 205 |
if (block_ == nullptr) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
return block_->use_count_ == 1; |
| 209 |
} |
| 210 |
|
| 211 |
inline T *operator*() const { |
| 212 |
return reinterpret_cast<T *>(&(block_->data)); |
| 213 |
} |
| 214 |
|
| 215 |
inline T *operator->() const { |
| 216 |
return reinterpret_cast<T *>(&(block_->data)); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
template <typename... Args> |
| 225 |
inline static ThreadlocalSharedPtr<T> Create(Args &&...args) { |
| 226 |
ThreadlocalAllocator<RefBlock> arena; |
| 227 |
ThreadlocalSharedPtr<T> p; |
| 228 |
p.block_ = arena.allocate(1); |
| 229 |
p.block_->use_count_ = 1; |
| 230 |
new (&(p.block_->data)) T(std::forward<Args>(args)...); |
| 231 |
return p; |
| 232 |
} |
| 233 |
|
| 234 |
private: |
| 235 |
|
| 236 |
struct RefBlock { |
| 237 |
typename std::aligned_storage<sizeof(T), alignof(T)>::type data; |
| 238 |
unsigned use_count_; |
| 239 |
}; |
| 240 |
|
| 241 |
inline static void DecRef(RefBlock *block) { |
| 242 |
if (block != nullptr) { |
| 243 |
if (--block->use_count_ == 0) { |
| 244 |
ThreadlocalAllocator<RefBlock> arena; |
| 245 |
T *dptr = reinterpret_cast<T *>(&(block->data)); |
| 246 |
dptr->~T(); |
| 247 |
arena.deallocate(block, 1); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
inline static void IncRef(RefBlock *block) { |
| 253 |
if (block != nullptr) { |
| 254 |
++block->use_count_; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
RefBlock *block_; |
| 259 |
}; |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
#endif |