2
3
4
5
10#include "Core/Library/MemoryLibrary.h"
11#include "Core/Container/Vector.h"
16
17
25
26
27
28
35
36
42
43
44
45
49
50
54
55
56
60
61
62
66
67
68
72
73
74
78
79
80
84
85
86
90
91
92
96
97
98
102
103
104
108
109
110
116
117
121
122
126
127
131
132
136
137
141
142
146 template <
typename T>
154 m_ExpandBytes = size * 1024;
158 m_ExpandBytes = MemoryLibrary::align_up<size_t>(size *
sizeof(T), 8 * 1024);
162 template <
typename T>
165 if (m_Memories.empty())
return;
168
169
170 for (size_t i = 0; i < m_Memories.size(); i++)
172 SystemFree(m_Memories.get(i));
176 template <
typename T>
182
183
189 m_SpareBytes -=
sizeof(T);
194
195
196 if (m_SpareBytes <
sizeof(T))
198 m_SpareBytes = m_ExpandBytes;
201
202
203 void* memoryBlock = SystemAlloc(m_ExpandBytes >> 13);
206 SPICES_CORE_ERROR(
"Memory alloc failed")
211 m_pointer =
static_cast<
char*>(memoryBlock);
212 m_Memories.push_back(memoryBlock);
216
217
221 m_SpareBytes -=
sizeof(T);
225
226
232 template <
typename T>
236
237
241
242
243 memset(obj, 0,
sizeof(T));
246
247
251 m_SpareBytes +=
sizeof(T);
257 std::unique_lock<std::mutex> lock(m_Mutex);
265 std::unique_lock<std::mutex> lock(m_Mutex);
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.
void * GetFreeList() const
Get FreeList.
ObjectPool & operator=(const ObjectPool &)=delete
Copy Assignment Operation.
std::mutex m_Mutex
Mutex for thread safety.
void * m_FreeList
freelist.
scl::vector< void * > m_Memories
This objectPoll allocated memories.
void Delete(T *obj)
Free a obj of T.
virtual ~ObjectPool()
Destructor Function.
T * New()
Alloc a memory block to store T.
size_t GetNMemoryBlocks() const
Get number of memory blocks allocated to this objectPool.
size_t m_ExpandBytes
Expand bytes.
size_t m_SpareBytes
Spare bytes of this objectPool.
void * GetPointer() const
Get objectPool current memory pointer.
size_t GetSpareBytes() const
Get SpareBytes.
char * m_pointer
objectPool current memory pointer.
ObjectPool(ObjectPoolSizeMode mode=ObjectPoolSizeMode::FixedSize, size_t size=DefaultExpandKBytes)
Constructor Function.
T * ThreadNew()
Thread Safe Version of New.
ObjectPool(const ObjectPool &)=delete
Copy Constructor Function.
void ThreadDelete(T *obj)
Thread Safe Version of Delete.
static constexpr size_t DefaultExpandKBytes
use 128KB as default expand bytes.
ObjectPool Class. Specific situation(Fixed size of block) of MemoryPool.
scl::radix_trie< 64 - MemoryPool::PAGE_SHIFT, 3 > m_IdSpanMap
radix trie for [pageId - span]
virtual ~PageCache()=default
Destructor Function.
PageCache & operator=(const PageCache &)=delete
Copy Assignment Operation.
static PageCache m_PageCache
this single instance.
PageCache()=default
Constructor Function.
scl::span * MapObjectToSpan(void *obj) const
Find span by memory pointer.
scl::span * InternalNewSpan(size_t k)
Fetch pages span(internal call).
static PageCache * Get()
Get this single instance.
void ReleaseSpanToPageCache(scl::span *s)
Release span from cc to pc,.
std::mutex m_Mutex
mutex for pc.
scl::span * NewSpan(size_t k)
Fetch pages span.
ObjectPool< scl::span > m_SpanPool
ObjectPool for span.
PageCache(const PageCache &)=delete
Copy Constructor Function.
std::array< scl::span_list, MemoryPool::PAGE_NUM > m_SpanLists
FreeList Array.
Page memory cache. Third level of memory allocator.
radix_trie()
Constructor Function.
void * get(size_t k) const
Get item by key.
static constexpr size_t LENGTH
array length.
void set(size_t k, void *v)
Set pair of key - value.
virtual ~radix_trie()
Deconstruct Function.
virtual ~radix_trie()
Deconstruct Function.
void * get(size_t k) const
Get item by key.
static constexpr size_t ROOT_BITS
root bits.
static constexpr size_t LEAF_BITS
leaf bits.
radix_trie()
Constructor Function.
void set(size_t k, void *v)
Set pair of key - value.
static constexpr size_t LEAF_LENGTH
leaf array length.
Spices::ObjectPool< Leaf > m_LeafPool
ObjectPool of Leaf.
static constexpr size_t ROOT_LENGTH
root array length.
static constexpr size_t LEAF_LENGTH
leaf array length.
radix_trie()
Constructor Function.
void * get(size_t k) const
Get item by key.
void set(size_t k, void *v)
Set pair of key - value.
virtual ~radix_trie()=default
Deconstruct Function.
static constexpr size_t INTERIOR_LENGTH
interior array length.
Spices::ObjectPool< Leaf > m_LeafPool
ObjectPool of Leaf.
static constexpr size_t INTERIOR_BITS
interior bits.
Spices::ObjectPool< Node > m_NodePool
ObjectPool of Node.
static constexpr size_t LEAF_BITS
leaf bits.
void PushFront(span *s)
Push a span to this list.
span * Begin() const
Get begin pointer.
span * End() const
Get end pointer.
Bidirectional cyclic linked list for span.
void * m_FreeList
current pointer.
bool m_IsUse
True if in use.
Used for manage multiple page memory.
This Class is similar to std::vector, the difference between that is this one allocates memory by mal...
ObjectPoolSizeMode
enum of ObjectPool expand mode
std::array< void *, LENGTH > values
std::array< void *, LEAF_LENGTH > values
std::array< Node *, ROOT_LENGTH > ptrs
std::array< void *, LEAF_LENGTH > values
std::array< Node *, INTERIOR_LENGTH > ptrs