13 #include "./logging.h" 23 template<
size_t size,
size_t align>
28 static_assert(align %
alignof(LinkedList) == 0,
29 "alignment requirement failed.");
30 curr_page_.reset(
new Page());
34 if (head_ !=
nullptr) {
35 LinkedList* ret = head_;
39 if (page_ptr_ < kPageSize) {
40 return &(curr_page_->data[page_ptr_++]);
42 allocated_.push_back(std::move(curr_page_));
43 curr_page_.reset(
new Page());
45 return &(curr_page_->data[0]);
54 LinkedList* ptr =
static_cast<LinkedList*
>(p);
61 static const int kPageSize = ((1 << 22) / size);
64 typename std::aligned_storage<size, align>::type data[kPageSize];
68 LinkedList* next{
nullptr};
71 LinkedList* head_{
nullptr};
73 std::unique_ptr<Page> curr_page_;
77 std::vector<std::unique_ptr<Page> > allocated_;
112 return static_cast<T*
>(Store::Get()->allocate());
122 Store::Get()->deallocate(p);
148 : block_(other.block_) {
156 : block_(other.block_) {
157 other.block_ =
nullptr;
172 block_ = other.block_;
173 other.block_ =
nullptr;
183 block_ = other.block_;
189 return block_ ==
nullptr;
194 inline T*
get()
const {
195 if (block_ ==
nullptr)
return nullptr;
196 return reinterpret_cast<T*
>(&(block_->data));
207 if (block_ ==
nullptr)
return false;
208 return block_->use_count_ == 1;
212 return reinterpret_cast<T*
>(&(block_->data));
216 return reinterpret_cast<T*
>(&(block_->data));
224 template <
typename... Args>
229 p.block_->use_count_ = 1;
230 new (&(p.block_->data)) T(std::forward<Args>(args)...);
237 typename std::aligned_storage<sizeof(T), alignof(T)>::type data;
241 inline static void DecRef(RefBlock* block) {
242 if (block !=
nullptr) {
243 if (--block->use_count_ == 0) {
245 T* dptr =
reinterpret_cast<T*
>(&(block->data));
252 inline static void IncRef(RefBlock* block) {
253 if (block !=
nullptr) {
263 #endif // DMLC_MEMORY_H_ void deallocate(void *p)
deallocate a piece of memory
Definition: memory.h:53
static ThreadlocalSharedPtr< T > Create(Args &&...args)
create a new space from threadlocal storage and return it.
Definition: memory.h:225
A memory pool that allocate memory of fixed size and alignment.
Definition: memory.h:24
ThreadlocalSharedPtr()
default constructor
Definition: memory.h:137
T * operator*() const
Definition: memory.h:211
bool operator==(std::nullptr_t other) const
check if nullptr
Definition: memory.h:188
T * pointer
pointer type
Definition: memory.h:90
ThreadlocalAllocator()
default constructor
Definition: memory.h:96
void * allocate()
allocate a new memory of size
Definition: memory.h:33
ThreadlocalSharedPtr(ThreadlocalSharedPtr< T > &&other)
move constructor
Definition: memory.h:155
A threadlocal store to store threadlocal variables. Will return a thread local singleton of type T...
Definition: thread_local.h:35
T * operator->() const
Definition: memory.h:215
MemoryPool()
constructor
Definition: memory.h:27
ThreadlocalSharedPtr(std::nullptr_t other)
constructor from nullptr
Definition: memory.h:142
ThreadlocalAllocator(const ThreadlocalAllocator< U > &other)
constructor from another allocator
Definition: memory.h:103
namespace for dmlc
Definition: array_view.h:12
Portable thread local storage.
a shared pointer like type that allocate object from a threadlocal object pool. This object is not th...
Definition: memory.h:134
void reset()
reset the pointer to nullptr.
Definition: memory.h:201
A thread local allocator that get memory from a threadlocal memory pool. This is suitable to allocate...
Definition: memory.h:87
ThreadlocalSharedPtr< T > & operator=(const ThreadlocalSharedPtr< T > &other)
copy assignment
Definition: memory.h:181
bool unique() const
Definition: memory.h:206
void deallocate(T *p, size_t n)
deallocate memory
Definition: memory.h:119
~ThreadlocalSharedPtr()
destructor
Definition: memory.h:162
const T * const_ptr
const pointer type
Definition: memory.h:92
ThreadlocalSharedPtr< T > & operator=(ThreadlocalSharedPtr< T > &&other)
move assignment
Definition: memory.h:170
T * allocate(size_t n)
allocate memory
Definition: memory.h:109
ThreadlocalSharedPtr(const ThreadlocalSharedPtr< T > &other)
copy constructor
Definition: memory.h:147
T value_type
value type
Definition: memory.h:94