Magic_Game/core/modules/GC.cpp

100 lines
1.4 KiB
C++
Raw Normal View History

2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
2018-09-05 13:33:39 +08:00
#include "..\e2dtool.h"
2018-07-05 01:09:54 +08:00
#include "..\e2dmanager.h"
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-09-04 22:42:34 +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);
}
2018-09-04 22:42:34 +08:00
e2d::GC * e2d::GC::GetInstance()
2018-09-02 14:30:48 +08:00
{
2018-09-04 22:42:34 +08:00
static GC instance_;
return &instance_;
2018-09-02 14:30:48 +08:00
}
2018-07-06 00:47:50 +08:00
e2d::GC::GC()
2018-09-04 22:42:34 +08:00
: notifyed_(false)
, cleanup_(false)
, pool_()
2018-07-06 00:47:50 +08:00
{
}
e2d::GC::~GC()
{
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
2018-09-04 22:42:34 +08:00
cleanup_ = true;
for (const auto& ref : pool_)
2018-09-02 14:30:48 +08:00
{
delete ref;
}
2018-09-04 22:42:34 +08:00
pool_.clear();
cleanup_ = false;
2018-08-12 12:06:06 +08:00
2018-09-02 14:30:48 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
Image::ClearCache();
2018-09-02 14:30:48 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
Player::DestroyInstance();
Audio::DestroyInstance();
Renderer::DestroyInstance();
Input::DestroyInstance();
Window::DestroyInstance();
Game::DestroyInstance();
2018-09-02 14:30:48 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::GC::Flush()
{
2018-09-04 22:42:34 +08:00
if (!notifyed_)
2018-07-06 00:47:50 +08:00
return;
2017-10-14 01:07:34 +08:00
2018-09-04 22:42:34 +08:00
notifyed_ = false;
for (auto iter = pool_.begin(); iter != pool_.end();)
{
2018-09-04 22:42:34 +08:00
if ((*iter)->GetRefCount() <= 0)
{
2018-07-22 21:22:27 +08:00
delete (*iter);
2018-09-04 22:42:34 +08:00
iter = pool_.erase(iter);
}
else
{
2018-05-14 22:51:40 +08:00
++iter;
}
}
}
2018-09-04 22:42:34 +08:00
void e2d::GC::AutoRelease(Ref * ref)
2018-07-07 01:43:41 +08:00
{
if (ref)
{
2018-09-04 22:42:34 +08:00
pool_.insert(ref);
}
}
2018-09-04 22:42:34 +08:00
void e2d::GC::SafeRelease(Ref* ref)
{
2018-09-04 22:42:34 +08:00
if (cleanup_)
2018-07-22 21:22:27 +08:00
return;
2018-07-22 21:22:27 +08:00
if (ref)
2018-07-07 01:43:41 +08:00
{
2018-09-04 22:42:34 +08:00
ref->Release();
notifyed_ = true;
2018-07-07 01:43:41 +08:00
}
2017-10-14 01:07:34 +08:00
}