Debian Codegraph @

src/dmlc-core/include/dmlc/memory.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file memory.h
4 * \brief Additional memory hanlding utilities.
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 * \brief A memory pool that allocate memory of fixed size and alignment.
21 * \tparam size The size of each piece.
22 * \tparam align The alignment requirement of the memory.
23 */
24 template <size_t size, size_t align>
25 class MemoryPool {
26 public:
27 /*! \brief constructor */
28 MemoryPool() {
29 static_assert(align % alignof(LinkedList) == 0, "alignment requirement failed.");
30 curr_page_.reset(new Page());
31 }
32 /*! \brief allocate a new memory of size */
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 * \brief deallocate a piece of memory
51 * \param p The pointer to the memory to be de-allocated.
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 // page size of each member
61 static const int kPageSize = ((1 << 22) / size);
62 // page to be requested.
63 struct Page {
64 typename std::aligned_storage<size, align>::type data[kPageSize];
65 };
66 // internal linked list structure.
67 struct LinkedList {
68 LinkedList *next{nullptr};
69 };
70 // head of free list
71 LinkedList *head_{nullptr};
72 // current free page
73 std::unique_ptr<Page> curr_page_;
74 // pointer to the current free page position.
75 size_t page_ptr_{0};
76 // allocated pages.
77 std::vector<std::unique_ptr<Page>> allocated_;
78 };
79
80 /*!
81 * \brief A thread local allocator that get memory from a threadlocal memory pool.
82 * This is suitable to allocate objects that do not cross thread.
83 * \tparam T the type of the data to be allocated.
84 */
85 template <typename T>
86 class ThreadlocalAllocator {
87 public:
88 /*! \brief pointer type */
89 typedef T *pointer;
90 /*! \brief const pointer type */
91 typedef const T *const_ptr;
92 /*! \brief value type */
93 typedef T value_type;
94 /*! \brief default constructor */
95 ThreadlocalAllocator() {}
96 /*!
97 * \brief constructor from another allocator
98 * \param other another allocator
99 * \tparam U another type
100 */
101 template <typename U>
102 ThreadlocalAllocator(const ThreadlocalAllocator<U> &other) {}
103 /*!
104 * \brief allocate memory
105 * \param n number of blocks
106 * \return an uninitialized memory of type T.
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 * \brief deallocate memory
115 * \param p a memory to be returned.
116 * \param n number of blocks
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 * \brief a shared pointer like type that allocate object
127 * from a threadlocal object pool. This object is not thread-safe
128 * but can be faster than shared_ptr in certain usecases.
129 * \tparam T the data type.
130 */
131 template <typename T>
132 struct ThreadlocalSharedPtr {
133 public:
134 /*! \brief default constructor */
135 ThreadlocalSharedPtr() : block_(nullptr) {}
136 /*!
137 * \brief constructor from nullptr
138 * \param other the nullptr type
139 */
140 ThreadlocalSharedPtr(std::nullptr_t other) : block_(nullptr) {} // NOLINT(*)
141 /*!
142 * \brief copy constructor
143 * \param other another pointer.
144 */
145 ThreadlocalSharedPtr(const ThreadlocalSharedPtr<T> &other) : block_(other.block_) {
146 IncRef(block_);
147 }
148 /*!
149 * \brief move constructor
150 * \param other another pointer.
151 */
152 ThreadlocalSharedPtr(ThreadlocalSharedPtr<T> &&other) : block_(other.block_) {
153 other.block_ = nullptr;
154 }
155 /*!
156 * \brief destructor
157 */
158 ~ThreadlocalSharedPtr() {
159 DecRef(block_);
160 }
161 /*!
162 * \brief move assignment
163 * \param other another object to be assigned.
164 * \return self.
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 * \brief copy assignment
174 * \param other another object to be assigned.
175 * \return self.
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 /*! \brief check if nullptr */
184 inline bool operator==(std::nullptr_t other) const {
185 return block_ == nullptr;
186 }
187 /*!
188 * \return get the pointer content.
189 */
190 inline T *get() const {
191 if (block_ == nullptr) {
192 return nullptr;
193 }
194 return reinterpret_cast<T *>(&(block_->data));
195 }
196 /*!
197 * \brief reset the pointer to nullptr.
198 */
199 inline void reset() {
200 DecRef(block_);
201 block_ = nullptr;
202 }
203 /*! \return if use_count == 1*/
204 inline bool unique() const {
205 if (block_ == nullptr) {
206 return false;
207 }
208 return block_->use_count_ == 1;
209 }
210 /*! \return dereference pointer */
211 inline T *operator*() const {
212 return reinterpret_cast<T *>(&(block_->data));
213 }
214 /*! \return dereference pointer */
215 inline T *operator->() const {
216 return reinterpret_cast<T *>(&(block_->data));
217 }
218 /*!
219 * \brief create a new space from threadlocal storage and return it.
220 * \tparam Args the arguments.
221 * \param args The input argument
222 * \return the allocated pointer.
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 // internal reference block
236 struct RefBlock {
237 typename std::aligned_storage<sizeof(T), alignof(T)>::type data;
238 unsigned use_count_;
239 };
240 // decrease ref counter
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 // increase ref counter
252 inline static void IncRef(RefBlock *block) {
253 if (block != nullptr) {
254 ++block->use_count_;
255 }
256 }
257 // internal block
258 RefBlock *block_;
259 };
260
261 } // namespace dmlc
262
263 #endif // DMLC_MEMORY_H_