/** * @file dl_memory_pool.h * @brief 基于引用计数的内存池 * * * @version 1.0 * @author lveyou * @date 21-08-30 * * @note 内存池修改为维护1个free列表和1个used列表 * @note 定期将used列表引用计数为0的移动回free列表 * * @note 注意gc的类之间不要存在引用关系,不过也可以如下处理: * @note gcText包含gcSprite,我们使PoolList先释放即可 */ #pragma once #include #include #include "io/dl_log.h" constexpr bool OUTPUT_DEBUG = false; namespace dl { struct GcRefInfo { uint32_t _count; }; //! 引用计数对象,通过dnd_gc宏创建 template class GC { template friend class PoolList; public: GC() :_p(nullptr), _ref(nullptr) {} GC(const GC& b) : _p(b._p), _ref(b._ref) { if (_ref)++_ref->_count; } operator bool() { return _p != nullptr; } T* operator->() { return _p; } T& operator*() { return *_p; } GC& operator=(const GC& b) { //如果相同,则什么都不做 if (_p == b._p) { return *this; } //不同,且有原对象,减少1次对原对象的引用 if (_p) { assert(_ref && _ref->_count > 0); --_ref->_count; } //复制,再增加b的一次引用(b可能为空) _p = b._p; _ref = b._ref; if (_ref) ++_ref->_count; return *this; } //! 取消引用对象 void CancelRef() { if (_p) { assert(_ref && _ref->_count > 0); --_ref->_count; _p = nullptr; _ref = nullptr; } } size_t GetRefCount() { if (_p) { assert(_ref && _ref->_count > 0); return _ref->_count; } return 0; } //! 直接获取指针,注意生命周期 T* GetPointer() { return _p; } ~GC() { // 如果退出时崩溃到这里,说明有GC后于PoolList析构,需要使其退出主函数之前析构 // 简单说就是 不要使用gc全局变量,因为可能晚于GC系统析构 // Text包含Sprite,如果Sprite先全局析构,再全局析构Text也会出问题( // 此问题已解决,先使用一下gcSprite,PoolList就会先于PoolList析构 if (_p) { assert(_ref && _ref->_count > 0); --_ref->_count; } } private: GcRefInfo* _ref; T* _p; }; template class PoolList { friend class MemoryPool; private: PoolList() {} //取一个位置 template GC GetGC(Args&& ...args) { void* p = nullptr; //如果没有就新建内存 if (_listFree.empty()) _add_free(); // if (_listFree.empty()) { log("PoolArray::GetGC: 失败!"); return GC(); } p = _listFree.front(); _listFree.pop_front(); _listUsed.push_back(p); GC ret; ret._ref = new (p) GcRefInfo; ret._ref->_count = 1; ret._p = new ((GcRefInfo*)p + 1) T(args...); return ret; } ~PoolList() { //旧注释: 此处无法控制析构顺序(GC可能之后析构),所以暂时不释放 //由于需要使用vld内存泄漏检测,这里必须释放(但不能使用全局的GC变量) //Used列表的内存强行回收 for (auto& iter : _listUsed) { //显式析构 T* p = (T*)((GcRefInfo*)iter + 1); p->~T(); free(iter); } for (auto& iter : _listFree) { free(iter); } if (OUTPUT_DEBUG) { log_test("释放 {}({}byte)({}/{})内存。", typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), _listUsed.size(), _listFree.size()); } } private: //可使用的节点内存 std::list _listFree; std::list _listUsed; //批量new void _add_free() { //先检查used转移到free size_t count = 0; if (_listUsed.size() > 10000)//100m (sizeof(GcRefInfo) + sizeof(T)) > 1024 * 1024 * 100 { for (auto iter = _listUsed.begin(); iter != _listUsed.end();) { if (((GcRefInfo*)*iter)->_count == 0) { //显式析构 T* p = (T*)((GcRefInfo*)*iter + 1); p->~T(); _listFree.push_back(*iter); _listUsed.erase(iter++); ++count; } else ++iter; } if (OUTPUT_DEBUG) { log_test("转移 {}({}byte)({})内存。", typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), count); } } //转移过少 if (count <= _listUsed.size() / 2) { // size_t num = _listUsed.size() + 10; for (size_t i = 0; i != num; ++i) { void* p = malloc(sizeof(GcRefInfo) + sizeof(T)); if (p == nullptr) { log("malloc失败!"); return; } _listFree.push_back(p); } if (OUTPUT_DEBUG) { log_test("分配 {}({}byte)({})内存。", typeid(T).name(), sizeof(GcRefInfo) + sizeof(T), _listFree.size()); } } } }; //! 内存池 class MemoryPool { public: /** * @brief 定义一个GC变量(引用计数内存池) * @param[in] args 构造参数 */ template static GC GetGC(Args&& ...args) { return FindPoolList().GetGC(args...); } private: MemoryPool() = delete; template static PoolList& FindPoolList() { static PoolList pool_list; return pool_list; } }; /** * @brief 定义一个GC变量(引用计数内存池) * @param[in] T 类型名 * @param[in] args 构造参数 */ template auto get_gc(Args&& ...args) { return MemoryPool::GetGC(args...); } }