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);
135 template<
typename... Args>
136 GC<T> GetGC(Args&& ...args)
140 if (_listFree.empty())
144 if (_listFree.empty())
146 log(
"PoolArray::GetGC: 失败!");
150 p = _listFree.front();
151 _listFree.pop_front();
152 _listUsed.push_back(p);
157 ret._ref =
new (p) GcRefInfo;
158 ret._ref->_count = 1;
159 ret._p =
new ((GcRefInfo*)p + 1)
T(args...);
171 for (
auto& iter : _listUsed)
174 T* p = (
T*)((GcRefInfo*)iter + 1);
180 for (
auto& iter : _listFree)
187 typeid(
T).name(),
sizeof(GcRefInfo) +
sizeof(
T), _listUsed.size(), _listFree.size());
193 std::list<void*> _listFree;
194 std::list<void*> _listUsed;
201 if (_listUsed.size() > 10000)
203 for (
auto iter = _listUsed.begin(); iter != _listUsed.end();)
205 if (((GcRefInfo*)*iter)->_count == 0)
208 T* p = (
T*)((GcRefInfo*)*iter + 1);
211 _listFree.push_back(*iter);
212 _listUsed.erase(iter++);
221 typeid(
T).name(),
sizeof(GcRefInfo) +
sizeof(
T), count);
227 if (count <= _listUsed.size() / 2)
230 size_t num = _listUsed.size() + 10;
231 for (
size_t i = 0; i != num; ++i)
233 void* p = malloc(
sizeof(GcRefInfo) +
sizeof(
T));
239 _listFree.push_back(p);
244 typeid(
T).name(),
sizeof(GcRefInfo) +
sizeof(
T), _listFree.size());
261 template<
typename T,
typename... Args>
264 return FindPoolList<T>().GetGC(args...);
284template<
typename T,
typename... Args>
287 return MemoryPool::GetGC<T>(args...);
T * GetPointer()
直接获取指针,注意生命周期
GC< T > & operator=(const GC< T > &b)
static GC< T > GetGC(Args &&...args)
定义一个GC变量(引用计数内存池)
#define log_test(str,...)
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())
以当前设置输出