SpiecsEngine
 
Loading...
Searching...
No Matches
ThrealCache.cpp
Go to the documentation of this file.
1/**
2* @file ThrealCache.cpp.
3* @brief The ThreadCache Class Implementation.
4* @author tcmalloc.
5*/
6
7#include "Pchheader.h"
8#include "ThrealCache.h"
9#include "CentralCache.h"
10#include "ObjectPool.h"
11
12namespace Spices {
13
15 {
16 /**
17 * @brief Release all memory to cc while thread is destroied.
18 */
19 for (int i = 0; i < MemoryPool::FREE_LIST_NUM; i++)
20 {
21 if (m_FreeLists[i].Size() > 0)
22 {
23 ReleaseToCentralCache(m_FreeLists[i], MemoryPool::Bytes(i), m_FreeLists[i].Size());
24 }
25 }
26 }
27
28 void* ThreadCache::Allocate(size_t size)
29 {
30 /**
31 * @brief Only allowed allocate 258KB one tme.
32 */
33 assert(size <= MemoryPool::MAX_BYTES);
34
35 /**
36 * @brief Determain aligned size and freelist index.
37 */
38 const size_t alignSize = MemoryPool::AlignUp(size);
39 const size_t index = MemoryPool::Index(size);
40
41 /**
42 * @brief Fetch memory from freelist.
43 */
44 if (!m_FreeLists[index].Empty())
45 {
46 return m_FreeLists[index].Pop();
47 }
48
49 /**
50 * @brief Fetch memory from cc;
51 */
52 else
53 {
54 return FetchFromCentralCache(index, alignSize);
55 }
56 }
57
58 void ThreadCache::Deallocate(void* obj, size_t size)
59 {
60 assert(obj);
61 assert(size <= MemoryPool::MAX_BYTES);
62
63 /**
64 * @brief Push object memory to free list.
65 */
66 const size_t index = MemoryPool::Index(size);
67 m_FreeLists[index].Push(obj);
68
69 /**
70 * @brief Release memory to cc.
71 */
72 if (m_FreeLists[index].Size() >= m_FreeLists[index].ApplyforNBlocks())
73 {
74 ReleaseToCentralCache(m_FreeLists[index], size, m_FreeLists[index].ApplyforNBlocks());
75 }
76 }
77
78 void* ThreadCache::FetchFromCentralCache(size_t index, size_t alignSize)
79 {
80 /**
81 * @brief Slow-Start Threshold Dynamic Adjustment Algorithm.
82 */
83 const size_t batchNum = std::min(m_FreeLists[index].ApplyforNBlocks(), MemoryPool::GetNBlocksLimit(alignSize));
84
85 if (batchNum == m_FreeLists[index].ApplyforNBlocks())
86 {
87 m_FreeLists[index].IncreaseInNextApplyFor();
88 }
89
90 void* start = nullptr;
91 void* end = nullptr;
92
93 /**
94 * @brief Obtain actural blocks form cc.
95 */
96 const size_t actualNum = CentralCache::Get()->FetchRange(start, end, batchNum, alignSize);
97
98 assert(actualNum >= 1);
99
100 /**
101 * @brief push other blocks to freelist if obtained more than one block.
102 */
103 if (actualNum > 1)
104 {
105 m_FreeLists[index].PushRange(MemoryPool::PointerSpace(start), end, actualNum - 1);
106 }
107
108 return start;
109 }
110
111 void ThreadCache::ReleaseToCentralCache(scl::free_list& list, size_t size, size_t count)
112 {
113 void* start = nullptr;
114 void* end = nullptr;
115
116 list.PopRange(start, end, count);
117
118 CentralCache::Get()->ReleaseListToSpans(start, size);
119 }
120
121 /**
122 * @brief ThreadCache ObjectPool for all threads.
123 */
125
127 {
128 if (instance)
129 {
130 objectPool.ThreadDelete(instance);
131 }
132 }
133
135 {
136 /**
137 * @brief Thread Unique TCWapper.
138 */
139 static _declspec(thread) ThreadCacheThreadWapper pTLSThreadCache;
140
141 if (!pTLSThreadCache.instance)
142 {
143 pTLSThreadCache.instance = objectPool.ThreadNew();
144 }
145
146 return pTLSThreadCache.instance;
147 }
148}
ObjectPool Class. Specific situation(Fixed size of block) of MemoryPool.
Definition ObjectPool.h:31
virtual ~ThreadCacheThreadWapper()
Destructor Function.
ThreadCache * instance
This thread ThreadCache instance.
static ThreadCache *& GetInst()
Get ThreadCache Instance. @reutrn Returns ThreadCache Instance.
Wapper of Instance/Delete ThreadCache in thread.
Definition ThrealCache.h:88
void Deallocate(void *obj, size_t size)
Recycle object memory.
void * FetchFromCentralCache(size_t index, size_t alignSize)
Fetch memory from central cache if this is run out.
virtual ~ThreadCache()
Destructor Function.
void * Allocate(size_t size)
Allocate memory.
Thread memory cache. First level of memory allocator.
Definition ThrealCache.h:19
Free list for memory pool.
Definition FreeList.h:16
static ObjectPool< ThreadCache > objectPool
ThreadCache ObjectPool for all threads.