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()
|
2014-01-21 17:47:59 +08:00
|
|
|
:_name("")
|
|
|
|
{
|
2014-01-22 11:40:07 +08:00
|
|
|
_managedObjectArray.reserve(150);
|
2014-01-21 17:47:59 +08:00
|
|
|
PoolManager::getInstance()->push(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoreleasePool::AutoreleasePool(const std::string &name)
|
|
|
|
:_name(name)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-22 11:40:07 +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-22 11:40:07 +08:00
|
|
|
clear();
|
2014-01-20 17:08:22 +08:00
|
|
|
|
|
|
|
PoolManager::getInstance()->pop();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoreleasePool::addObject(Object* object)
|
|
|
|
{
|
2014-01-22 11:40:07 +08:00
|
|
|
_managedObjectArray.push_back(object);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoreleasePool::clear()
|
|
|
|
{
|
2014-01-22 11:03:06 +08:00
|
|
|
for (const auto &obj : _managedObjectArray)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-22 10:17:42 +08:00
|
|
|
obj->release();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
2014-01-22 10:17:42 +08:00
|
|
|
_managedObjectArray.clear();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-01-21 17:47:59 +08:00
|
|
|
void AutoreleasePool::dump()
|
|
|
|
{
|
|
|
|
CCLOG("autorelease pool: %s, number of managed object %d\n", _name.c_str(), static_cast<int>(_managedObjectArray.size()));
|
|
|
|
CCLOG("%20s%20s%20s", "Object pointer", "Object id", "reference count");
|
|
|
|
for (const auto &obj : _managedObjectArray)
|
|
|
|
{
|
2014-01-22 13:47:29 +08:00
|
|
|
CCLOG("%20p%20u%20u\n", obj, obj->_ID, obj->getReferenceCount());
|
2014-01-21 17:47:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2014-01-21 17:47:59 +08:00
|
|
|
s_singleInstance->_curReleasePool = new AutoreleasePool("cocos2d autorelease pool");
|
2014-01-20 17:08:22 +08:00
|
|
|
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
|