SpiecsEngine
 
Loading...
Searching...
No Matches
MemoryPool.cpp
Go to the documentation of this file.
1/**
2* @file MemoryPool.h
3* @brief The MemoryPool Class Implementation.
4* @author tcmalloc.
5*/
6
7#include "Pchheader.h"
8#include "MemoryPool.h"
9#include "Core/Library/MemoryLibrary.h"
10#include "PageCache.h"
11#include "ThrealCache.h"
12
13namespace Spices {
14
15 void* MemoryPool::Alloc(size_t size)
16 {
17 /**
18 * @brief allocate from pc.
19 */
20 if (size > MAX_BYTES)
21 {
22 const size_t alignSize = AlignUp(size);
23 size_t k = alignSize >> PAGE_SHIFT;
24
25 scl::span* s = PageCache::Get()->NewSpan(k);
26 s->m_IsUse = true;
27 s->m_BlockSize = alignSize;
28
29 void* ptr = reinterpret_cast<void*>(s->m_PageId << PAGE_SHIFT);
30 return ptr;
31 }
32
33 /**
34 * @brief allocate from tc.
35 */
36 else
37 {
38 return ThreadCacheThreadWapper::GetInst()->Allocate(size);
39 }
40 }
41
42 void MemoryPool::Free(void* ptr)
43 {
44 assert(ptr);
45
47
48 if (!s)
49 {
50 free(ptr);
51 return;
52 }
53
54 const size_t size = s->m_BlockSize;
55
56 /**
57 * @brief release from pc.
58 */
59 if (size > MAX_BYTES)
60 {
62 }
63
64 /**
65 * @brief release from tc.
66 */
67 else
68 {
69 ThreadCacheThreadWapper::GetInst()->Deallocate(ptr, size);
70 }
71 }
72
73 void*& MemoryPool::PointerSpace(void* obj)
74 {
75 return *(static_cast<void**>(obj));
76 }
77
78 size_t MemoryPool::AlignUp(size_t size)
79 {
80 if (size <= static_cast<size_t>( 128)) return MemoryLibrary::align_up<size_t>(size, 8); /* @brief align up to 8B , if size is less than 128B. (16) */
81 else if (size <= static_cast<size_t>(1 * 1024)) return MemoryLibrary::align_up<size_t>(size, 16); /* @brief align up to 16B , if size is less than 1KB. (56) */
82 else if (size <= static_cast<size_t>(8 * 1024)) return MemoryLibrary::align_up<size_t>(size, 128); /* @brief align up to 128B, if size is less than 8KB. (56) */
83 else if (size <= static_cast<size_t>(64 * 1024)) return MemoryLibrary::align_up<size_t>(size, 1024); /* @brief align up to 1KB , if size is less than 64KB. (56) */
84 else if (size <= static_cast<size_t>(256 * 1024)) return MemoryLibrary::align_up<size_t>(size, 8 * 1024); /* @brief align up to 8KB , if size is less than 256KB.(24) */
85 else return MemoryLibrary::align_up<size_t>(size, 1 << PAGE_SHIFT); /* @brief align up to page */
86 }
87
88 size_t MemoryPool::Index(size_t size)
89 {
90 auto _index = [&](size_t bytes, size_t align_shift) -> size_t
91 {
92 return ((bytes + (1 << align_shift) - 1) >> align_shift) - 1;
93 };
94
95 static constexpr int group_array[4] = { 16, 56, 56, 56 };
96
97 if (size <= 128)
98 {
99 return _index(size, 3);
100 }
101 else if (size <= 1024)
102 {
103 return _index(size - 128, 4) +
104 group_array[0];
105 }
106 else if (size <= static_cast<size_t>(8 * 1024))
107 {
108 return _index(size - 1024, 7) +
109 group_array[1] +
110 group_array[0];
111 }
112 else if (size <= static_cast<size_t>(64 * 1024))
113 {
114 return _index(size - static_cast<size_t>(8 * 1024), 10) +
115 group_array[2] +
116 group_array[1] +
117 group_array[0];
118 }
119 else if (size <= static_cast<size_t>(256 * 1024))
120 {
121 return _index(size - static_cast<size_t>(64 * 1024), 13) +
122 group_array[3] +
123 group_array[2] +
124 group_array[1] +
125 group_array[0];
126 }
127 else
128 {
129 assert(false);
130 return -1;
131 }
132 }
133
134 size_t MemoryPool::Bytes(size_t index)
135 {
136 static constexpr int group_array[4] = { 16, 56, 56, 56 };
137
138 // align up to 8 (8 - 128 B)
139 if (index < 16)
140 {
141 return 8 * (index + 1);
142 }
143
144 // align up to 16 (128B - 1024 B)
145 else if(index < 16 + 56)
146 {
147 return 16 * (index + 1 - group_array[0]) + 128;
148 }
149
150 // align up to 128 (1 - 8 KB)
151 else if (index < 16 + 56 + 56)
152 {
153 return 128 * (index + 1 - group_array[0] - group_array[1]) + 1024;
154 }
155
156 // align up to 1024 (8 - 64 KB)
157 else if (index < 16 + 56 + 56 + 56)
158 {
159 return 1024 * (index + 1 - group_array[0] - group_array[1] - group_array[2]) + 8 * 1024;
160 }
161
162 // align up to 8KB (64 - 256 KB)
163 else if (index < 16 + 56 + 56 + 56 + 24)
164 {
165 return 8 * 1024 * (index + 1 - group_array[0] - group_array[1] - group_array[2] - group_array[3]) + 64 * 1024;
166 }
167
168 else
169 {
170 SPICES_CORE_ERROR("Access invalid free_lits index")
171 return 8 * 1024;
172 }
173 }
174
175 size_t MemoryPool::GetNBlocksLimit(size_t size)
176 {
177 assert(size > 0);
178
179 /**
180 * @brief [2 - 512].
181 */
182 size_t num = MAX_BYTES / size;
183 num = std::max(std::min(static_cast<size_t>(512), num), static_cast<size_t>(2));
184
185 return num;
186 }
187
188 size_t MemoryPool::GetPages(size_t size)
189 {
190 /**
191 * @brief get blocks count.
192 */
193 const size_t num = GetNBlocksLimit(size);
194
195 /**
196 * @brief get pages count.
197 */
198 size_t npage = num * size;
199 npage >>= PAGE_SHIFT;
200 npage = std::max(npage, static_cast<size_t>(1));
201
202 return npage;
203 }
204}
static void Free(void *ptr)
Free memory entry point.
static void *& PointerSpace(void *obj)
Get object first 4/8 bytes as a pointer.
MemoryPool Class.
Definition MemoryPool.h:22
scl::span * MapObjectToSpan(void *obj) const
Find span by memory pointer.
Definition PageCache.cpp:21
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
Page memory cache. Third level of memory allocator.
Definition PageCache.h:21
bool m_IsUse
True if in use.
Definition SpanList.h:61
Used for manage multiple page memory.
Definition SpanList.h:15