Go to the documentation of this file.
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());
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;
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) {
244 ThreadlocalAllocator<RefBlock> arena;
245 T* dptr =
reinterpret_cast<T*
>(&(block->data));
247 arena.deallocate(block, 1);
252 inline static void IncRef(RefBlock* block) {
253 if (block !=
nullptr) {
263 #endif // DMLC_MEMORY_H_
ThreadlocalSharedPtr(const ThreadlocalSharedPtr< T > &other)
copy constructor
Definition: memory.h:147
A thread local allocator that get memory from a threadlocal memory pool. This is suitable to allocate...
Definition: memory.h:87
ThreadlocalSharedPtr(ThreadlocalSharedPtr< T > &&other)
move constructor
Definition: memory.h:155
namespace for dmlc
Definition: array_view.h:12
a shared pointer like type that allocate object from a threadlocal object pool. This object is not th...
Definition: memory.h:134
A threadlocal store to store threadlocal variables. Will return a thread local singleton of type T.
Definition: thread_local.h:35
void deallocate(void *p)
deallocate a piece of memory
Definition: memory.h:53
void * allocate()
allocate a new memory of size
Definition: memory.h:33
defines configuration macros
MemoryPool()
constructor
Definition: memory.h:27
static ThreadlocalSharedPtr< T > Create(Args &&... args)
create a new space from threadlocal storage and return it.
Definition: memory.h:225
ThreadlocalAllocator(const ThreadlocalAllocator< U > &other)
constructor from another allocator
Definition: memory.h:103
T * operator*() const
Definition: memory.h:211
T * allocate(size_t n)
allocate memory
Definition: memory.h:109
const typedef T * const_ptr
const pointer type
Definition: memory.h:92
T * get() const
Definition: memory.h:194
ThreadlocalSharedPtr(std::nullptr_t other)
constructor from nullptr
Definition: memory.h:142
void reset()
reset the pointer to nullptr.
Definition: memory.h:201
ThreadlocalAllocator()
default constructor
Definition: memory.h:96
bool unique() const
Definition: memory.h:206
T * pointer
pointer type
Definition: memory.h:90
A memory pool that allocate memory of fixed size and alignment.
Definition: memory.h:24
bool operator==(std::nullptr_t other) const
check if nullptr
Definition: memory.h:188
T value_type
value type
Definition: memory.h:94
~ThreadlocalSharedPtr()
destructor
Definition: memory.h:162
ThreadlocalSharedPtr< T > & operator=(ThreadlocalSharedPtr< T > &&other)
move assignment
Definition: memory.h:170
ThreadlocalSharedPtr< T > & operator=(const ThreadlocalSharedPtr< T > &other)
copy assignment
Definition: memory.h:181
Portable thread local storage.
void deallocate(T *p, size_t n)
deallocate memory
Definition: memory.h:119
ThreadlocalSharedPtr()
default constructor
Definition: memory.h:137
T * operator->() const
Definition: memory.h:215