2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
AutoreleasePool::AutoreleasePool()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
_managedObjectArray.reserve(150);
|
2014-01-20 17:08:22 +08:00
|
|
|
|
|
|
|
PoolManager::getInstance()->push(this);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoreleasePool::~AutoreleasePool()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing AutoreleasePool: %p", this);
|
2014-01-20 17:08:22 +08:00
|
|
|
_managedObjectArray.clear();
|
|
|
|
|
|
|
|
PoolManager::getInstance()->pop();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoreleasePool::clear()
|
|
|
|
{
|
2013-12-07 14:23:29 +08:00
|
|
|
if (!_managedObjectArray.empty())
|
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
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
PoolManager* PoolManager::s_singleInstance = nullptr;
|
|
|
|
|
|
|
|
PoolManager* PoolManager::getInstance()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
if (s_singleInstance == nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
s_singleInstance = new PoolManager();
|
|
|
|
// Add the first auto release pool
|
|
|
|
s_singleInstance->_curReleasePool = new AutoreleasePool();
|
|
|
|
s_singleInstance->_releasePoolStack.push(s_singleInstance->_curReleasePool);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
2014-01-20 17:08:22 +08:00
|
|
|
return s_singleInstance;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
void PoolManager::destroyInstance()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
delete s_singleInstance;
|
|
|
|
s_singleInstance = nullptr;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PoolManager::PoolManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolManager::~PoolManager()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing PoolManager: %p", this);
|
2014-01-20 17:08:22 +08:00
|
|
|
|
|
|
|
while (!_releasePoolStack.empty())
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
AutoreleasePool* pool = _releasePoolStack.top();
|
|
|
|
_releasePoolStack.pop();
|
|
|
|
|
|
|
|
delete pool;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
AutoreleasePool* PoolManager::getCurrentPool() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
return _curReleasePool;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
void PoolManager::push(AutoreleasePool *pool)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
_releasePoolStack.push(pool);
|
|
|
|
_curReleasePool = pool;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
void PoolManager::pop()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
// Can not pop the pool that created by engine
|
|
|
|
CC_ASSERT(_releasePoolStack.size() >= 1);
|
|
|
|
|
|
|
|
_releasePoolStack.pop();
|
|
|
|
|
|
|
|
// Should update _curReleasePool if a temple pool is released
|
|
|
|
if (_releasePoolStack.size() > 1)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-20 17:08:22 +08:00
|
|
|
_curReleasePool = _releasePoolStack.top();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|