2018-04-21 21:24:46 +08:00
|
|
|
|
#include "..\e2dbase.h"
|
2018-07-03 20:19:00 +08:00
|
|
|
|
#include "..\e2dtool.h"
|
2018-07-05 01:09:54 +08:00
|
|
|
|
#include "..\e2dmanager.h"
|
2018-07-03 18:16:26 +08:00
|
|
|
|
|
2018-07-07 01:43:41 +08:00
|
|
|
|
using namespace e2d;
|
|
|
|
|
|
|
|
|
|
|
|
e2d::autorelease_t const e2d::autorelease = e2d::autorelease_t();
|
|
|
|
|
|
|
|
|
|
|
|
void * operator new(size_t size, e2d::autorelease_t const &) E2D_NOEXCEPT
|
|
|
|
|
|
{
|
|
|
|
|
|
void* p = ::operator new(size, std::nothrow);
|
|
|
|
|
|
if (p)
|
|
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
GC::getInstance()->autorelease(static_cast<Ref*>(p));
|
2018-07-07 01:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
return p;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void operator delete(void * block, e2d::autorelease_t const &) E2D_NOEXCEPT
|
|
|
|
|
|
{
|
|
|
|
|
|
::operator delete (block, std::nothrow);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GC <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>
|
|
|
|
|
|
GC GC::_instance;
|
2018-07-03 18:16:26 +08:00
|
|
|
|
|
2018-07-06 00:47:50 +08:00
|
|
|
|
e2d::GC::GC()
|
|
|
|
|
|
: _notifyed(false)
|
2018-07-13 01:12:22 +08:00
|
|
|
|
, _cleanup(false)
|
2018-07-06 00:47:50 +08:00
|
|
|
|
, _pool()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-03 18:16:26 +08:00
|
|
|
|
e2d::GC::~GC()
|
|
|
|
|
|
{
|
2018-07-21 22:57:21 +08:00
|
|
|
|
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
|
2018-07-22 21:22:27 +08:00
|
|
|
|
this->clear();
|
2018-08-12 12:06:06 +08:00
|
|
|
|
|
2018-07-21 22:57:21 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC><EFBFBD><EFBFBD>
|
|
|
|
|
|
Image::clearCache();
|
2018-08-12 12:06:06 +08:00
|
|
|
|
|
2018-07-03 18:16:26 +08:00
|
|
|
|
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>
|
|
|
|
|
|
Game::destroyInstance();
|
|
|
|
|
|
Renderer::destroyInstance();
|
2018-07-04 15:34:16 +08:00
|
|
|
|
Input::destroyInstance();
|
2018-07-03 18:16:26 +08:00
|
|
|
|
Window::destroyInstance();
|
2018-07-05 22:05:23 +08:00
|
|
|
|
Timer::destroyInstance();
|
2018-07-05 16:34:53 +08:00
|
|
|
|
SceneManager::destroyInstance();
|
2018-07-05 01:09:54 +08:00
|
|
|
|
ActionManager::destroyInstance();
|
2018-07-08 16:57:12 +08:00
|
|
|
|
CollisionManager::destroyInstance();
|
2018-07-03 18:16:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-07 11:03:03 +08:00
|
|
|
|
void e2d::GC::flush()
|
2017-10-12 23:34:13 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
if (!_notifyed)
|
2018-07-06 00:47:50 +08:00
|
|
|
|
return;
|
2017-10-14 01:07:34 +08:00
|
|
|
|
|
2018-07-22 21:22:27 +08:00
|
|
|
|
_notifyed = false;
|
|
|
|
|
|
for (auto iter = _pool.begin(); iter != _pool.end();)
|
2017-10-12 23:34:13 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
if ((*iter)->getRefCount() <= 0)
|
2017-10-12 23:34:13 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
delete (*iter);
|
|
|
|
|
|
iter = _pool.erase(iter);
|
2017-10-12 23:34:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-05-14 22:51:40 +08:00
|
|
|
|
++iter;
|
2017-10-12 23:34:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-03 18:16:26 +08:00
|
|
|
|
void e2d::GC::clear()
|
2018-03-03 17:02:08 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
_cleanup = true;
|
2018-07-21 22:57:21 +08:00
|
|
|
|
|
|
|
|
|
|
SceneManager::getInstance()->clear();
|
|
|
|
|
|
Timer::getInstance()->clearAllTasks();
|
|
|
|
|
|
ActionManager::getInstance()->clearAll();
|
|
|
|
|
|
|
2018-08-12 14:30:28 +08:00
|
|
|
|
for (const auto& ref : _pool)
|
2018-04-21 00:46:26 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
delete ref;
|
2018-04-21 00:46:26 +08:00
|
|
|
|
}
|
2018-07-22 21:22:27 +08:00
|
|
|
|
_pool.clear();
|
|
|
|
|
|
_cleanup = false;
|
2018-03-03 17:02:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-22 21:22:27 +08:00
|
|
|
|
e2d::GC * e2d::GC::getInstance()
|
2017-10-12 23:34:13 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
return &_instance;
|
2018-07-07 01:43:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-22 21:22:27 +08:00
|
|
|
|
void e2d::GC::autorelease(Ref * ref)
|
2018-07-07 01:43:41 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (ref)
|
|
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
_pool.insert(ref);
|
2017-10-12 23:34:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-22 21:22:27 +08:00
|
|
|
|
void e2d::GC::safeRelease(Ref* ref)
|
2018-07-03 18:16:26 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
if (_cleanup)
|
|
|
|
|
|
return;
|
2018-07-03 18:16:26 +08:00
|
|
|
|
|
2018-07-22 21:22:27 +08:00
|
|
|
|
if (ref)
|
2018-07-07 01:43:41 +08:00
|
|
|
|
{
|
2018-07-28 20:06:27 +08:00
|
|
|
|
ref->release();
|
|
|
|
|
|
_notifyed = true;
|
2018-07-07 01:43:41 +08:00
|
|
|
|
}
|
2017-10-14 01:07:34 +08:00
|
|
|
|
}
|