2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCAutoreleasePool.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
static PoolManager* s_pPoolManager = NULL;
|
|
|
|
|
|
|
|
AutoreleasePool::AutoreleasePool()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
_managedObjectArray.reserve(150);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoreleasePool::~AutoreleasePool()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing AutoreleasePool: %p", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoreleasePool::addObject(Object* object)
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
_managedObjectArray.pushBack(object);
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
CCASSERT(object->_reference > 1, "reference count should be greater than 1");
|
|
|
|
++(object->_autoReleaseCount);
|
|
|
|
object->release(); // no ref count, in this case autorelease pool added.
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoreleasePool::removeObject(Object* object)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < object->_autoReleaseCount; ++i)
|
|
|
|
{
|
2013-12-12 14:45:30 +08:00
|
|
|
_managedObjectArray.eraseObject(object, false);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoreleasePool::clear()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
if (!_managedObjectArray.empty())
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
//CCAutoreleasePool* pReleasePool;
|
|
|
|
#ifdef _DEBUG
|
2013-12-07 14:23:29 +08:00
|
|
|
int nIndex = _managedObjectArray.size() - 1;
|
2013-10-12 15:25:45 +08:00
|
|
|
#endif
|
|
|
|
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &obj : _managedObjectArray) {
|
2013-12-07 14:23:29 +08:00
|
|
|
--(obj->_autoReleaseCount);
|
2013-10-12 15:25:45 +08:00
|
|
|
//(*it)->release();
|
|
|
|
//delete (*it);
|
|
|
|
#ifdef _DEBUG
|
|
|
|
nIndex--;
|
|
|
|
#endif
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-07 14:23:29 +08:00
|
|
|
_managedObjectArray.clear();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PoolManager
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
PoolManager* PoolManager::sharedPoolManager()
|
|
|
|
{
|
|
|
|
if (s_pPoolManager == NULL)
|
|
|
|
{
|
|
|
|
s_pPoolManager = new PoolManager();
|
|
|
|
}
|
|
|
|
return s_pPoolManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PoolManager::purgePoolManager()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(s_pPoolManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolManager::PoolManager()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
_releasePoolStack.reserve(150);
|
2013-10-12 15:25:45 +08:00
|
|
|
_curReleasePool = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolManager::~PoolManager()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing PoolManager: %p", this);
|
|
|
|
finalize();
|
|
|
|
|
|
|
|
// we only release the last autorelease pool here
|
|
|
|
_curReleasePool = 0;
|
2013-12-11 17:53:45 +08:00
|
|
|
_releasePoolStack.erase(0);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PoolManager::finalize()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
if (!_releasePoolStack.empty())
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &pool : _releasePoolStack) {
|
2013-12-07 14:23:29 +08:00
|
|
|
pool->clear();
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PoolManager::push()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
AutoreleasePool* pool = new AutoreleasePool(); //ref = 1
|
|
|
|
_curReleasePool = pool;
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-07 14:23:29 +08:00
|
|
|
_releasePoolStack.pushBack(pool); //ref = 2
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2013-12-07 14:23:29 +08:00
|
|
|
pool->release(); //ref = 1
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PoolManager::pop()
|
|
|
|
{
|
|
|
|
if (! _curReleasePool)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t count = _releasePoolStack.size();
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
_curReleasePool->clear();
|
|
|
|
|
2013-12-07 14:23:29 +08:00
|
|
|
if (count > 1)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:53:45 +08:00
|
|
|
_releasePoolStack.erase(count-1);
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
// if(nCount > 1)
|
|
|
|
// {
|
2013-12-07 14:23:29 +08:00
|
|
|
// _curReleasePool = _releasePoolStack.at(count - 2);
|
2013-10-12 15:25:45 +08:00
|
|
|
// return;
|
|
|
|
// }
|
2013-12-07 14:23:29 +08:00
|
|
|
_curReleasePool = _releasePoolStack.at(count - 2);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*_curReleasePool = NULL;*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void PoolManager::removeObject(Object* object)
|
|
|
|
{
|
|
|
|
CCASSERT(_curReleasePool, "current auto release pool should not be null");
|
|
|
|
|
|
|
|
_curReleasePool->removeObject(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PoolManager::addObject(Object* object)
|
|
|
|
{
|
|
|
|
getCurReleasePool()->addObject(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AutoreleasePool* PoolManager::getCurReleasePool()
|
|
|
|
{
|
|
|
|
if(!_curReleasePool)
|
|
|
|
{
|
|
|
|
push();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(_curReleasePool, "current auto release pool should not be null");
|
|
|
|
|
|
|
|
return _curReleasePool;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|