SpiecsEngine
 
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1/**
2* @file Vector.h.
3* @brief The vector Class Definitions and Implementation.
4* @author Spices.
5*/
6
7#pragma once
8
9namespace scl {
10
11 /**
12 * @brief This Class is similar to std::vector,
13 * the difference between that is this one allocates memory by malloc rather that new.
14 * This is important is ObjectPool.
15 */
16 template<typename T>
17 class vector
18 {
19 public:
20
21 /**
22 * @brief Constructor Function.
23 */
25
26 /**
27 * @brief Destructor Function.
28 */
29 virtual ~vector();
30
31 /**
32 * @brief Get vector Begin Pointer.
33 * @return Return vector Begin Pointer.
34 */
35 T* Begin() const { return m_Begin; }
36
37 /**
38 * @brief Get vector End Pointer.
39 * @return Return vector End Pointer.
40 */
41 T* End() const { return m_End; }
42
43 /**
44 * @brief Get vector size.
45 * @return Returns vector size.
46 */
47 size_t size() const { return m_UseCount; }
48
49 /**
50 * @brief Determine if vector is empty.
51 * @retrun Returns true if empty.
52 */
53 bool empty() const { return m_UseCount == 0; }
54
55 /**
56 * @brief Push a element in the end of vector.
57 * @param[in] element .
58 */
59 void push_back(T& element);
60
61 /**
62 * @brief Get element by index.
63 * @param[in] index .
64 * @return Returns element.
65 */
66 T& get(size_t index);
67
68 private:
69
70 /**
71 * @brief Vector Begin Pointer.
72 */
74
75 /**
76 * @brief Vector End Pointer.
77 */
79
80 /**
81 * @brief vector used size.
82 */
84
85 /**
86 * @brief vector spare size.
87 */
89
90 /**
91 * @brief Vector expand rate.
92 */
94 };
95
96 template<typename T>
97 inline vector<T>::vector()
98 : m_Begin(nullptr)
99 , m_End(nullptr)
100 , m_UseCount(0)
101 , m_SpareCount(0)
102 , m_ExpandRate(2)
103 {}
104
105 template<typename T>
106 inline vector<T>::~vector()
107 {
108 if (m_UseCount == 0) return;
109
110 /**
111 * @brief free this memory.
112 */
113 free(m_Begin);
114 }
115
116 template<typename T>
117 inline void vector<T>::push_back(T& element)
118 {
119 if (m_SpareCount != 0)
120 {
121 *m_End = std::move(element);
122
123 ++m_UseCount;
124 --m_SpareCount;
125 ++m_End;
126 }
127 else
128 {
129 if (m_UseCount == 0)
130 {
131 const size_t bytes = sizeof(T) * m_ExpandRate;
132
133 m_UseCount = 1;
134 m_SpareCount = m_ExpandRate - 1;
135
136 m_Begin = static_cast<T*>(malloc(bytes));
137
138 m_End = m_Begin;
139 *m_End = std::move(element);
140 ++m_End;
141 }
142 else
143 {
144 T* om = m_Begin;
145 const size_t ob = sizeof(T) * m_UseCount;
146 const size_t nb = sizeof(T) * m_UseCount * m_ExpandRate;
147 m_SpareCount = (nb - ob) / sizeof(T);
148
149 m_Begin = static_cast<T*>(malloc(nb));
150 memset(m_Begin, 0, nb);
151 memcpy(m_Begin, om, ob);
152
153 m_End = reinterpret_cast<T*>(reinterpret_cast<char*>(m_Begin) + ob);
154 *m_End = std::move(element);
155
156 ++m_UseCount;
157 --m_SpareCount;
158 ++m_End;
159
160 free(om);
161 }
162 }
163 }
164
165 template<typename T>
166 inline T& vector<T>::get(size_t index)
167 {
168 assert(index < m_UseCount);
169
170 return *(reinterpret_cast<T*>(reinterpret_cast<char*>(m_Begin) + sizeof(T) * index));
171 }
172}
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
vector()
Constructor Function.
Definition Vector.h:97
T * End() const
Get vector End Pointer.
Definition Vector.h:41
size_t size() const
Get vector size.
Definition Vector.h:47
bool empty() const
Determine if vector is empty. @retrun Returns true if empty.
Definition Vector.h:53
virtual ~vector()
Destructor Function.
Definition Vector.h:106
size_t m_UseCount
vector used size.
Definition Vector.h:83
T * m_Begin
Vector Begin Pointer.
Definition Vector.h:73
T * Begin() const
Get vector Begin Pointer.
Definition Vector.h:35
T & get(size_t index)
Get element by index.
Definition Vector.h:166
size_t m_ExpandRate
Vector expand rate.
Definition Vector.h:93
size_t m_SpareCount
vector spare size.
Definition Vector.h:88
T * m_End
Vector End Pointer.
Definition Vector.h:78
void push_back(T &element)
Push a element in the end of vector.
Definition Vector.h:117
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