dl
dl_memory_pool.h
浏览该文件的文档.
1
16#pragma once
17
18#include <list>
19#include <cassert>
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>
130{
131 friend class MemoryPool;
132private:
133 PoolList() {}
134 //取一个位置
135 template<typename... Args>
136 GC<T> GetGC(Args&& ...args)
137 {
138 void* p = nullptr;
139 //如果没有就新建内存
140 if (_listFree.empty())
141 _add_free();
142
143 //
144 if (_listFree.empty())
145 {
146 log("PoolArray::GetGC: 失败!");
147 return GC<T>();
148 }
149
150 p = _listFree.front();
151 _listFree.pop_front();
152 _listUsed.push_back(p);
153
154
155
156 GC<T> ret;
157 ret._ref = new (p) GcRefInfo;
158 ret._ref->_count = 1;
159 ret._p = new ((GcRefInfo*)p + 1) T(args...);
160
161
162 return ret;
163 }
164
165 ~PoolList()
166 {
167 //旧注释: 此处无法控制析构顺序(GC<T>可能之后析构),所以暂时不释放
168 //由于需要使用vld内存泄漏检测,这里必须释放(但不能使用全局的GC变量)
169
170 //Used列表的内存强行回收
171 for (auto& iter : _listUsed)
172 {
173 //显式析构
174 T* p = (T*)((GcRefInfo*)iter + 1);
175 p->~T();
176
177 free(iter);
178 }
179
180 for (auto& iter : _listFree)
181 {
182 free(iter);
183 }
184 if (OUTPUT_DEBUG)
185 {
186 log_test("释放 {}({}byte)({}/{})内存。",
187 typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), _listUsed.size(), _listFree.size());
188 }
189
190 }
191private:
192 //可使用的节点内存
193 std::list<void*> _listFree;
194 std::list<void*> _listUsed;
195
196 //批量new
197 void _add_free()
198 {
199 //先检查used转移到free
200 size_t count = 0;
201 if (_listUsed.size() > 10000)//100m (sizeof(GcRefInfo) + sizeof(T)) > 1024 * 1024 * 100
202 {
203 for (auto iter = _listUsed.begin(); iter != _listUsed.end();)
204 {
205 if (((GcRefInfo*)*iter)->_count == 0)
206 {
207 //显式析构
208 T* p = (T*)((GcRefInfo*)*iter + 1);
209 p->~T();
210
211 _listFree.push_back(*iter);
212 _listUsed.erase(iter++);
213 ++count;
214 }
215 else
216 ++iter;
217 }
218 if (OUTPUT_DEBUG)
219 {
220 log_test("转移 {}({}byte)({})内存。",
221 typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), count);
222 }
223 }
224
225
226 //转移过少
227 if (count <= _listUsed.size() / 2)
228 {
229 //
230 size_t num = _listUsed.size() + 10;
231 for (size_t i = 0; i != num; ++i)
232 {
233 void* p = malloc(sizeof(GcRefInfo) + sizeof(T));
234 if (p == nullptr)
235 {
236 log("malloc失败!");
237 return;
238 }
239 _listFree.push_back(p);
240 }
241 if (OUTPUT_DEBUG)
242 {
243 log_test("分配 {}({}byte)({})内存。",
244 typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), _listFree.size());
245 }
246 }
247
248 }
249};
250
251
252
255{
256public:
261 template<typename T, typename... Args>
262 static GC<T> GetGC(Args&& ...args)
263 {
264 return FindPoolList<T>().GetGC(args...);
265 }
266
267
268private:
269 MemoryPool() = delete;
270
271 template<typename T>
272 static PoolList<T>& FindPoolList()
273 {
274 static PoolList<T> pool_list;
275 return pool_list;
276 }
277};
278
284template<typename T, typename... Args>
285auto get_gc(Args&& ...args)
286{
287 return MemoryPool::GetGC<T>(args...);
288}
289
290
291}
引用计数对象,通过dnd_gc宏创建
T & operator*()
T * GetPointer()
直接获取指针,注意生命周期
size_t GetRefCount()
GC< T > & operator=(const GC< T > &b)
T * operator->()
GC(const GC< T > &b)
void CancelRef()
取消引用对象
static GC< T > GetGC(Args &&...args)
定义一个GC变量(引用计数内存池)
日志系统
#define log_test(str,...)
Definition: dl_log.h:185
constexpr bool OUTPUT_DEBUG
Definition: dl_array2.h:17
auto get_gc(Args &&...args)
定义一个GC变量(引用计数内存池)
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出
uint32_t _count