dl
dl_memory_pool.h
浏览该文件的文档.
1
15#pragma once
16
17#include <list>
18#include <cassert>
19#include <functional>
20
21#include "io/dl_log.h"
22
23constexpr bool OUTPUT_DEBUG = false;
24
25namespace dl
26{
28{
29 uint32_t _count;
30};
31
32
34template<typename T>
35class GC
36{
37 template<typename TT>
38 friend class PoolList;
39public:
40 GC() :_p(nullptr), _ref(nullptr) {}
41 GC(const GC<T>& b) : _p(b._p), _ref(b._ref) { if (_ref)++_ref->_count; }
42 operator bool()
43 {
44 return _p != nullptr;
45 }
46
48 {
49 return _p;
50 }
51
53 {
54 return *_p;
55 }
56
58 {
59 //如果相同,则什么都不做
60 if (_p == b._p)
61 {
62 return *this;
63 }
64
65 //不同,且有原对象,减少1次对原对象的引用
66 if (_p)
67 {
68 assert(_ref && _ref->_count > 0);
69 --_ref->_count;
70 }
71
72 //复制,再增加b的一次引用(b可能为空)
73 _p = b._p;
74 _ref = b._ref;
75 if (_ref)
76 ++_ref->_count;
77
78 return *this;
79 }
80
82 void CancelRef()
83 {
84 if (_p)
85 {
86 assert(_ref && _ref->_count > 0);
87 --_ref->_count;
88 _p = nullptr;
89 _ref = nullptr;
90 }
91 }
92
93 size_t GetRefCount()
94 {
95 if (_p)
96 {
97 assert(_ref && _ref->_count > 0);
98 return _ref->_count;
99 }
100 return 0;
101 }
102
105 {
106 return _p;
107 }
108
110 {
111 // 如果退出时崩溃到这里,说明有GC<T>后于PoolList析构,需要使其退出主函数之前析构
112 // 简单说就是 不要使用gc全局变量,因为可能晚于GC系统析构
113
114 // Text包含Sprite,如果Sprite先全局析构,再全局析构Text也会出问题
115 // 此问题已解决,先使用一下gcSprite,PoolList<Sprite>就会先于PoolList<Text>析构
116 if (_p)
117 {
118 assert(_ref && _ref->_count > 0);
119 --_ref->_count;
120 }
121 }
122
123private:
124 GcRefInfo* _ref;
125 T* _p;
126};
127
128template<typename T>
129class PoolList
130{
131 friend class MemoryPool;
132public:
134 void SetCallbackDelete(std::function<void()> func)
135 {
136 _cbDelete = func;
137 }
138private:
139 PoolList() {}
140 // 取一个位置
141 template<typename... Args>
142 GC<T> GetGC(Args&& ...args)
143 {
144 void* p = nullptr;
145 // 如果没有就新建内存
146 if (_listFree.empty())
147 _add_free();
148
149 //
150 if (_listFree.empty())
151 {
152 log("PoolArray::GetGC: 失败!");
153 return GC<T>();
154 }
155
156 p = _listFree.front();
157 _listFree.pop_front();
158 _listUsed.push_back(p);
159
160 GC<T> ret;
161 ret._ref = new (p) GcRefInfo;
162 ret._ref->_count = 1;
163 ret._p = new ((GcRefInfo*)p + 1) T(args...);
164
165 return ret;
166 }
167
168
169 ~PoolList()
170 {
171 // 不能使用全局的GC变量
172
173 // Used列表的内存强行回收
174 for (auto& iter : _listUsed)
175 {
176 //显式析构
177 T* p = (T*)((GcRefInfo*)iter + 1);
178 p->~T();
179
180 free(iter);
181 }
182
183 for (auto& iter : _listFree)
184 {
185 free(iter);
186 }
187 if (OUTPUT_DEBUG)
188 {
189 log_test("释放 {}({}byte)({}/{})内存。",
190 typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), _listUsed.size(), _listFree.size());
191 }
192
193 if(_cbDelete)
194 _cbDelete();
195 }
196private:
197 // 可使用的节点内存
198 std::list<void*> _listFree;
199 std::list<void*> _listUsed;
200 // 回调
201 std::function<void()> _cbDelete;
202
203 // 批量new
204 void _add_free()
205 {
206 //先检查used转移到free
207 size_t count = 0;
208 if (_listUsed.size() > 10000)//100m (sizeof(GcRefInfo) + sizeof(T)) > 1024 * 1024 * 100
209 {
210 for (auto iter = _listUsed.begin(); iter != _listUsed.end();)
211 {
212 if (((GcRefInfo*)*iter)->_count == 0)
213 {
214 //显式析构
215 T* p = (T*)((GcRefInfo*)*iter + 1);
216 p->~T();
217
218 _listFree.push_back(*iter);
219 _listUsed.erase(iter++);
220 ++count;
221 }
222 else
223 ++iter;
224 }
225 if (OUTPUT_DEBUG)
226 {
227 log_test("转移 {}({}byte)({})内存。",
228 typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), count);
229 }
230 }
231
232
233 //转移过少
234 if (count <= _listUsed.size() / 2)
235 {
236 //
237 size_t num = _listUsed.size() + 10;
238 for (size_t i = 0; i != num; ++i)
239 {
240 void* p = malloc(sizeof(GcRefInfo) + sizeof(T));
241 if (p == nullptr)
242 {
243 log("malloc失败!");
244 return;
245 }
246 _listFree.push_back(p);
247 }
248 if (OUTPUT_DEBUG)
249 {
250 log_test("分配 {}({}byte)({})内存。",
251 typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), _listFree.size());
252 }
253 }
254
255 }
256};
257
258
259
261class MemoryPool
262{
263public:
268 template<typename T, typename... Args>
269 static GC<T> GetGC(Args&& ...args)
270 {
271 return GetPoolList<T>().GetGC(args...);
272 }
273
274 template<typename T>
276 {
277 static PoolList<T> pool_list;
278 return pool_list;
279 }
280private:
281 MemoryPool() = delete;
282};
283
289template<typename T, typename... Args>
290auto get_gc(Args&& ...args)
291{
292 return MemoryPool::GetGC<T>(args...);
293}
294
295
296}
引用计数对象,通过dnd_gc宏创建
T * GetPointer()
直接获取指针,注意生命周期
size_t GetRefCount()
GC< T > & operator=(const GC< T > &b)
GC(const GC< T > &b)
void CancelRef()
取消引用对象
static GC< T > GetGC(Args &&...args)
定义一个GC变量(引用计数内存池)
static PoolList< T > & GetPoolList()
friend class MemoryPool
void SetCallbackDelete(std::function< void()> func)
设置删除回调
日志系统
#define log_test(...)
constexpr bool OUTPUT_DEBUG
auto get_gc(Args &&...args)
定义一个GC变量(引用计数内存池)
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出