40 GC() :_p(nullptr), _ref(nullptr) {}
41 GC(
const GC<T>& b) : _p(b._p), _ref(b._ref) {
if (_ref)++_ref->
_count; }
68 assert(_ref && _ref->
_count > 0);
86 assert(_ref && _ref->
_count > 0);
97 assert(_ref && _ref->
_count > 0);
118 assert(_ref && _ref->
_count > 0);
141 template<
typename... Args>
142 GC<T> GetGC(Args&& ...args)
146 if (_listFree.empty())
150 if (_listFree.empty())
152 log(
"PoolArray::GetGC: 失败!");
156 p = _listFree.front();
157 _listFree.pop_front();
158 _listUsed.push_back(p);
161 ret._ref =
new (p) GcRefInfo;
162 ret._ref->_count = 1;
163 ret._p =
new ((GcRefInfo*)p + 1)
T(args...);
174 for (
auto& iter : _listUsed)
177 T* p = (
T*)((GcRefInfo*)iter + 1);
183 for (
auto& iter : _listFree)
190 typeid(
T).name(),
sizeof(GcRefInfo) +
sizeof(
T), _listUsed.size(), _listFree.size());
198 std::list<void*> _listFree;
199 std::list<void*> _listUsed;
201 std::function<void()> _cbDelete;
208 if (_listUsed.size() > 10000)
210 for (
auto iter = _listUsed.begin(); iter != _listUsed.end();)
212 if (((GcRefInfo*)*iter)->_count == 0)
215 T* p = (
T*)((GcRefInfo*)*iter + 1);
218 _listFree.push_back(*iter);
219 _listUsed.erase(iter++);
228 typeid(
T).name(),
sizeof(GcRefInfo) +
sizeof(
T), count);
234 if (count <= _listUsed.size() / 2)
237 size_t num = _listUsed.size() + 10;
238 for (
size_t i = 0; i != num; ++i)
240 void* p = malloc(
sizeof(GcRefInfo) +
sizeof(
T));
246 _listFree.push_back(p);
251 typeid(
T).name(),
sizeof(GcRefInfo) +
sizeof(
T), _listFree.size());
268 template<
typename T,
typename... Args>
271 return GetPoolList<T>().GetGC(args...);
289template<
typename T,
typename... Args>
292 return MemoryPool::GetGC<T>(args...);
T * GetPointer()
直接获取指针,注意生命周期
GC< T > & operator=(const GC< T > &b)
static GC< T > GetGC(Args &&...args)
定义一个GC变量(引用计数内存池)
static PoolList< T > & GetPoolList()
void SetCallbackDelete(std::function< void()> func)
设置删除回调
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())
以当前设置输出