SpiecsEngine
 
Loading...
Searching...
No Matches
CentralCache.cpp
Go to the documentation of this file.
1/**
2* @file CentralCache.cpp.
3* @brief The CentralCache Class Implementation.
4* @author tcmalloc.
5*/
6
7#include "Pchheader.h"
8#include "CentralCache.h"
9#include "PageCache.h"
10
11namespace Spices {
12
14
16 {
17 const size_t index = MemoryPool::Index(size);
18 size_t actualNum = 1;
19
20 {
21 std::unique_lock<std::mutex> lock(m_SpanLists[index].GetMutex());
22
23 scl::span* s = GetOneSpan(m_SpanLists[index], size);
24 assert(s);
25 assert(s->m_FreeList);
26
27 start = end = s->m_FreeList;
28
29 /**
30 * @brief fetch spare memory.
31 */
32 size_t i = 0;
33 while (i < batchNum - 1 && MemoryPool::PointerSpace(end) != nullptr)
34 {
35 end = MemoryPool::PointerSpace(end);
36 ++actualNum;
37 ++i;
38 }
39
41 s->m_UseCount += actualNum;
42 MemoryPool::PointerSpace(end) = nullptr;
43 }
44
45 return actualNum;
46 }
47
48 scl::span* CentralCache::GetOneSpan(scl::span_list& list, size_t size)
49 {
50 /**
51 * @brief Find span in cc.
52 */
53 scl::span* it = list.Begin();
54 while (it != list.End())
55 {
56 if (it->m_FreeList != nullptr)
57 {
58 return it;
59 }
60 else
61 {
62 it = it->m_Next;
63 }
64 }
65
66 /**
67 * @brief Release mutex.
68 */
69 list.GetMutex().unlock();
70
71 /**
72 * @brief get pages count.
73 */
74 size_t k = MemoryPool::GetPages(size);
75
76 /**
77 * @brief get a new span.
78 */
79 scl::span* s = PageCache::Get()->NewSpan(k);
80 s->m_IsUse = true;
81 s->m_BlockSize = size;
82
83 /**
84 * @brief get start/end pointer.
85 */
86 char* start = reinterpret_cast<char*>( s->m_PageId << MemoryPool::PAGE_SHIFT );
87 char* end = reinterpret_cast<char*>(start + (s->m_NPages << MemoryPool::PAGE_SHIFT));
88
89 s->m_FreeList = start;
90
91 /**
92 * @brief split pages to blocks.
93 */
94 {
95 void* tail = start;
96 start += size;
97
98 int i = 0;
99 while (start + size <= end)
100 {
101 ++i;
102 MemoryPool::PointerSpace(tail) = start;
103 start += size;
104 tail = MemoryPool::PointerSpace(tail);
105 }
106 MemoryPool::PointerSpace(tail) = nullptr;
107 }
108
109 /**
110 * @brief Get mutex.
111 */
112 list.GetMutex().lock();
113
114 /**
115 * @brief Push to list.
116 */
117 list.PushFront(s);
118
119 return s;
120 }
121
122 void CentralCache::ReleaseListToSpans(void* start, size_t size)
123 {
124 const size_t index = MemoryPool::Index(size);
125
126 {
127 std::unique_lock<std::mutex> lock(m_SpanLists[index].GetMutex());
128
129 /**
130 * @brief Iter all blocks and insert to span.
131 */
132 while (start)
133 {
134 void* next = MemoryPool::PointerSpace(start);
135
136 /**
137 * @brief Find span.
138 */
140
141 /**
142 * @brief Insert from head to span.
143 */
145 s->m_FreeList = start;
146 s->m_UseCount--;
147
148 /**
149 * @brief if span is not in use, than release to pc.
150 */
151 if (s->m_UseCount == 0)
152 {
153 m_SpanLists[index].Erase(s);
154 s->m_FreeList = nullptr;
155
156 /**
157 * @brief unlock span mutex.
158 */
159 m_SpanLists[index].GetMutex().unlock();
160
161 /**
162 * @brief release to pc.
163 */
165
166 /**
167 * @brief get span mutex.
168 */
169 m_SpanLists[index].GetMutex().lock();
170 }
171
172 start = next;
173 }
174 }
175 }
176}
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.
void ReleaseListToSpans(void *start, size_t size)
Release memory to pc.
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
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
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