SpiecsEngine
 
Loading...
Searching...
No Matches
ObjectPool.h
Go to the documentation of this file.
1/**
2* @file ObjectPool.h
3* @brief The ObjectPool Class Definitions.
4* @author tcmalloc.
5*/
6
7#pragma once
8#include "Core/Core.h"
9#include "MemoryPool.h"
10#include "Core/Library/MemoryLibrary.h"
11#include "Core/Container/Vector.h"
12
13namespace Spices {
14
15 /**
16 * @brief enum of ObjectPool expand mode
17 */
19 {
20 FixedSize = 0, // use fixed bytes.
21 FixedObjects = 1, // use fixed bytes of given number of object type.
22 };
23
24 /**
25 * @brief ObjectPool Class.
26 * Specific situation(Fixed size of block) of MemoryPool.
27 * @tparam T Specific Object Type.
28 */
29 template<typename T>
31 {
32 public:
33
34 /**
35 * @brief use 128KB as default expand bytes.
36 */
37 static constexpr size_t DefaultExpandKBytes = 128;
38
39 public:
40
41 /**
42 * @brief Constructor Function.
43 * @param[in] mode ObjectPoolSizeMode.
44 * @param[in] size FixedSize: Expanded bytes(KB); FixedObjects: Expanded number of objects.
45 */
47
48 /**
49 * @brief Destructor Function.
50 */
51 virtual ~ObjectPool();
52
53 /**
54 * @brief Copy Constructor Function.
55 * @note This Class not allowed copy behaves.
56 */
57 ObjectPool(const ObjectPool&) = delete;
58
59 /**
60 * @brief Copy Assignment Operation.
61 * @note This Class not allowed copy behaves.
62 */
63 ObjectPool& operator=(const ObjectPool&) = delete;
64
65 /**
66 * @brief Alloc a memory block to store T.
67 * @return Returns T pointer.
68 */
69 T* New();
70
71 /**
72 * @brief Free a obj of T.
73 * @param[in] obj Object to be free.
74 */
75 void Delete(T* obj);
76
77 /**
78 * @brief Get objectPool current memory pointer.
79 * @return Returns objectPool current memory pointer.
80 */
81 void* GetPointer() const { return static_cast<void*>(m_pointer); }
82
83 /**
84 * @brief Get number of memory blocks allocated to this objectPool.
85 * @return Returns number of memory blocks allocated to this objectPool.
86 */
87 size_t GetNMemoryBlocks() const { return m_Memories.size(); }
88
89 /**
90 * @brief Get FreeList.
91 * @return Returns FreeList.
92 */
93 void* GetFreeList() const { return m_FreeList; }
94
95 /**
96 * @brief Get SpareBytes.
97 * @return Returns SpareBytes.
98 */
99 size_t GetSpareBytes() const { return m_SpareBytes; }
100
101 /**
102 * @brief Thread Safe Version of New.
103 * @return Returns T pointer.
104 */
106
107 /**
108 * @brief Thread Safe Version of Delete.
109 * @param[in] obj Object to be free.
110 */
111 void ThreadDelete(T* obj);
112
113 private:
114
115 /**
116 * @brief objectPool current memory pointer.
117 */
119
120 /**
121 * @brief This objectPoll allocated memories.
122 */
124
125 /**
126 * @brief freelist.
127 */
129
130 /**
131 * @brief Spare bytes of this objectPool.
132 */
134
135 /**
136 * @brief Mutex for thread safety.
137 */
139
140 /**
141 * @brief Expand bytes.
142 */
144 };
145
146 template <typename T>
148 : m_pointer(nullptr)
149 , m_FreeList(nullptr)
150 , m_SpareBytes(0)
151 {
153 {
154 m_ExpandBytes = size * 1024;
155 }
156 else
157 {
158 m_ExpandBytes = MemoryLibrary::align_up<size_t>(size * sizeof(T), 8 * 1024);
159 }
160 }
161
162 template <typename T>
164 {
165 if (m_Memories.empty()) return;
166
167 /**
168 * @brief Free all memory blocks.
169 */
170 for (size_t i = 0; i < m_Memories.size(); i++)
171 {
172 SystemFree(m_Memories.get(i));
173 }
174 }
175
176 template <typename T>
178 {
179 T* obj = nullptr;
180
181 /**
182 * @brief Reused space.
183 */
184 if (m_FreeList)
185 {
187 obj = static_cast<T*>(m_FreeList);
188 m_FreeList = next;
189 m_SpareBytes -= sizeof(T);
190 }
191 else
192 {
193 /**
194 * @brief Alloc memory if there is no enough space.
195 */
196 if (m_SpareBytes < sizeof(T))
197 {
198 m_SpareBytes = m_ExpandBytes;
199
200 /**
201 * @brief Alloc 128 KB / 8KB = 16pages memory.
202 */
203 void* memoryBlock = SystemAlloc(m_ExpandBytes >> 13);
204 if (!memoryBlock)
205 {
206 SPICES_CORE_ERROR("Memory alloc failed")
207
208 return nullptr;
209 }
210
211 m_pointer = static_cast<char*>(memoryBlock);
212 m_Memories.push_back(memoryBlock);
213 }
214
215 /**
216 * @brief Pop current memoryblock as T.
217 */
218 obj = reinterpret_cast<T*>(m_pointer);
219
220 m_pointer += sizeof(T);
221 m_SpareBytes -= sizeof(T);
222 }
223
224 /**
225 * @brief Call Construct function of T in place.
226 */
227 new(obj)T;
228
229 return obj;
230 }
231
232 template <typename T>
233 void ObjectPool<T>::Delete(T* obj)
234 {
235 /**
236 * @brief Call Destructor manually.
237 */
238 obj->~T();
239
240 /**
241 * @brief Set memory to 0.
242 */
243 memset(obj, 0, sizeof(T));
244
245 /**
246 * @brief insert to head.
247 */
249 m_FreeList = obj;
250
251 m_SpareBytes += sizeof(T);
252 }
253
254 template<typename T>
255 inline T* ObjectPool<T>::ThreadNew()
256 {
257 std::unique_lock<std::mutex> lock(m_Mutex);
258
259 return New();
260 }
261
262 template<typename T>
263 inline void ObjectPool<T>::ThreadDelete(T* obj)
264 {
265 std::unique_lock<std::mutex> lock(m_Mutex);
266
267 Delete(obj);
268 }
269}
CentralCache()=default
Constructor Function.
static scl::span * GetOneSpan(scl::span_list &list, size_t size)
Get a not empty span.
size_t FetchRange(void *&start, void *&end, size_t batchNum, size_t size)
Fetch range memory to tc.
static CentralCache m_CentralCache
Single instance of this.
CentralCache & operator=(const CentralCache &)=delete
Copy Assignment Operation.
virtual ~CentralCache()=default
Destructor Function.
void ReleaseListToSpans(void *start, size_t size)
Release memory to pc.
std::array< scl::span_list, MemoryPool::FREE_LIST_NUM > m_SpanLists
FreeList Array.
CentralCache(const CentralCache &)=delete
Copy Constructor Function.
static CentralCache * Get()
Get this single Instance.
Central memory cache. Second level of memory allocator.
static void *& PointerSpace(void *obj)
Get object first 4/8 bytes as a pointer.
MemoryPool Class.
Definition MemoryPool.h:22
void * GetFreeList() const
Get FreeList.
Definition ObjectPool.h:93
ObjectPool & operator=(const ObjectPool &)=delete
Copy Assignment Operation.
std::mutex m_Mutex
Mutex for thread safety.
Definition ObjectPool.h:138
void * m_FreeList
freelist.
Definition ObjectPool.h:128
scl::vector< void * > m_Memories
This objectPoll allocated memories.
Definition ObjectPool.h:123
void Delete(T *obj)
Free a obj of T.
Definition ObjectPool.h:233
virtual ~ObjectPool()
Destructor Function.
Definition ObjectPool.h:163
T * New()
Alloc a memory block to store T.
Definition ObjectPool.h:177
size_t GetNMemoryBlocks() const
Get number of memory blocks allocated to this objectPool.
Definition ObjectPool.h:87
size_t m_ExpandBytes
Expand bytes.
Definition ObjectPool.h:143
size_t m_SpareBytes
Spare bytes of this objectPool.
Definition ObjectPool.h:133
void * GetPointer() const
Get objectPool current memory pointer.
Definition ObjectPool.h:81
size_t GetSpareBytes() const
Get SpareBytes.
Definition ObjectPool.h:99
char * m_pointer
objectPool current memory pointer.
Definition ObjectPool.h:118
ObjectPool(ObjectPoolSizeMode mode=ObjectPoolSizeMode::FixedSize, size_t size=DefaultExpandKBytes)
Constructor Function.
Definition ObjectPool.h:147
T * ThreadNew()
Thread Safe Version of New.
Definition ObjectPool.h:255
ObjectPool(const ObjectPool &)=delete
Copy Constructor Function.
void ThreadDelete(T *obj)
Thread Safe Version of Delete.
Definition ObjectPool.h:263
static constexpr size_t DefaultExpandKBytes
use 128KB as default expand bytes.
Definition ObjectPool.h:37
ObjectPool Class. Specific situation(Fixed size of block) of MemoryPool.
Definition ObjectPool.h:31
scl::radix_trie< 64 - MemoryPool::PAGE_SHIFT, 3 > m_IdSpanMap
radix trie for [pageId - span]
Definition PageCache.h:106
virtual ~PageCache()=default
Destructor Function.
PageCache & operator=(const PageCache &)=delete
Copy Assignment Operation.
static PageCache m_PageCache
this single instance.
Definition PageCache.h:86
PageCache()=default
Constructor Function.
scl::span * MapObjectToSpan(void *obj) const
Find span by memory pointer.
Definition PageCache.cpp:21
scl::span * InternalNewSpan(size_t k)
Fetch pages span(internal call).
static PageCache * Get()
Get this single instance.
Definition PageCache.h:50
void ReleaseSpanToPageCache(scl::span *s)
Release span from cc to pc,.
Definition PageCache.cpp:43
std::mutex m_Mutex
mutex for pc.
Definition PageCache.h:101
scl::span * NewSpan(size_t k)
Fetch pages span.
Definition PageCache.cpp:14
ObjectPool< scl::span > m_SpanPool
ObjectPool for span.
Definition PageCache.h:96
PageCache(const PageCache &)=delete
Copy Constructor Function.
std::array< scl::span_list, MemoryPool::PAGE_NUM > m_SpanLists
FreeList Array.
Definition PageCache.h:91
Page memory cache. Third level of memory allocator.
Definition PageCache.h:21
Leaf * m_Root
Root Node.
Definition RadixTrie.h:46
radix_trie()
Constructor Function.
Definition RadixTrie.h:53
void * get(size_t k) const
Get item by key.
Definition RadixTrie.h:74
static constexpr size_t LENGTH
array length.
Definition RadixTrie.h:33
void set(size_t k, void *v)
Set pair of key - value.
Definition RadixTrie.h:89
virtual ~radix_trie()
Deconstruct Function.
Definition RadixTrie.h:61
virtual ~radix_trie()
Deconstruct Function.
Definition RadixTrie.h:166
void * get(size_t k) const
Get item by key.
Definition RadixTrie.h:179
Node * m_Root
Root Node.
Definition RadixTrie.h:145
static constexpr size_t ROOT_BITS
root bits.
Definition RadixTrie.h:109
static constexpr size_t LEAF_BITS
leaf bits.
Definition RadixTrie.h:119
radix_trie()
Constructor Function.
Definition RadixTrie.h:157
void set(size_t k, void *v)
Set pair of key - value.
Definition RadixTrie.h:197
static constexpr size_t LEAF_LENGTH
leaf array length.
Definition RadixTrie.h:124
Spices::ObjectPool< Leaf > m_LeafPool
ObjectPool of Leaf.
Definition RadixTrie.h:150
static constexpr size_t ROOT_LENGTH
root array length.
Definition RadixTrie.h:114
static constexpr size_t LEAF_LENGTH
leaf array length.
Definition RadixTrie.h:243
radix_trie()
Constructor Function.
Definition RadixTrie.h:281
void * get(size_t k) const
Get item by key.
Definition RadixTrie.h:298
void set(size_t k, void *v)
Set pair of key - value.
Definition RadixTrie.h:317
Node * m_Root
Root Node.
Definition RadixTrie.h:264
virtual ~radix_trie()=default
Deconstruct Function.
static constexpr size_t INTERIOR_LENGTH
interior array length.
Definition RadixTrie.h:233
Spices::ObjectPool< Leaf > m_LeafPool
ObjectPool of Leaf.
Definition RadixTrie.h:274
static constexpr size_t INTERIOR_BITS
interior bits.
Definition RadixTrie.h:228
Spices::ObjectPool< Node > m_NodePool
ObjectPool of Node.
Definition RadixTrie.h:269
static constexpr size_t LEAF_BITS
leaf bits.
Definition RadixTrie.h:238
void PushFront(span *s)
Push a span to this list.
Definition SpanList.cpp:40
span * Begin() const
Get begin pointer.
Definition SpanList.cpp:59
span * End() const
Get end pointer.
Definition SpanList.cpp:64
Bidirectional cyclic linked list for span.
Definition SpanList.h:73
span * m_Next
next span.
Definition SpanList.h:41
void * m_FreeList
current pointer.
Definition SpanList.h:51
bool m_IsUse
True if in use.
Definition SpanList.h:61
Used for manage multiple page memory.
Definition SpanList.h:15
This Class is similar to std::vector, the difference between that is this one allocates memory by mal...
Definition Vector.h:18
ObjectPoolSizeMode
enum of ObjectPool expand mode
Definition ObjectPool.h:19
std::array< void *, LENGTH > values
Definition RadixTrie.h:40
std::array< void *, LEAF_LENGTH > values
Definition RadixTrie.h:139
std::array< Node *, ROOT_LENGTH > ptrs
Definition RadixTrie.h:131
std::array< void *, LEAF_LENGTH > values
Definition RadixTrie.h:258
std::array< Node *, INTERIOR_LENGTH > ptrs
Definition RadixTrie.h:250