2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 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.
|
|
|
|
****************************************************************************/
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "base/CCAutoreleasePool.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
AutoreleasePool::AutoreleasePool()
|
2014-01-24 14:46:06 +08:00
|
|
|
: _name("")
|
2014-01-24 17:51:36 +08:00
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
|
|
|
, _isClearing(false)
|
|
|
|
#endif
|
2014-01-21 17:47:59 +08:00
|
|
|
{
|
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)
|
2014-01-24 14:46:06 +08:00
|
|
|
: _name(name)
|
2014-01-24 17:51:36 +08:00
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
|
|
|
, _isClearing(false)
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void AutoreleasePool::addObject(Ref* object)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
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-24 17:51:36 +08:00
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
|
|
|
_isClearing = true;
|
|
|
|
#endif
|
2015-03-11 15:31:06 +08:00
|
|
|
std::vector<Ref*> releasings;
|
|
|
|
releasings.swap(_managedObjectArray);
|
|
|
|
for (const auto &obj : releasings)
|
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-24 17:51:36 +08:00
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
|
|
|
_isClearing = false;
|
|
|
|
#endif
|
2014-01-24 14:46:06 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool AutoreleasePool::contains(Ref* object) const
|
2014-01-24 14:46:06 +08:00
|
|
|
{
|
|
|
|
for (const auto& obj : _managedObjectArray)
|
|
|
|
{
|
|
|
|
if (obj == object)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
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-03-20 16:34:42 +08:00
|
|
|
CC_UNUSED_PARAM(obj);
|
2014-02-20 10:53:49 +08:00
|
|
|
CCLOG("%20p%20u\n", obj, 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-08-28 07:31:57 +08:00
|
|
|
s_singleInstance = new (std::nothrow) PoolManager();
|
2014-01-20 17:08:22 +08:00
|
|
|
// Add the first auto release pool
|
2014-06-03 18:57:52 +08:00
|
|
|
new AutoreleasePool("cocos2d autorelease pool");
|
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()
|
|
|
|
{
|
2014-06-03 19:09:48 +08:00
|
|
|
_releasePoolStack.reserve(10);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-24 14:46:06 +08:00
|
|
|
AutoreleasePool* pool = _releasePoolStack.back();
|
2014-01-20 17:08:22 +08:00
|
|
|
|
|
|
|
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-06-03 19:08:28 +08:00
|
|
|
return _releasePoolStack.back();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool PoolManager::isObjectInPools(Ref* obj) const
|
2014-01-24 14:46:06 +08:00
|
|
|
{
|
|
|
|
for (const auto& pool : _releasePoolStack)
|
|
|
|
{
|
|
|
|
if (pool->contains(obj))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:08:22 +08:00
|
|
|
void PoolManager::push(AutoreleasePool *pool)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-24 14:46:06 +08:00
|
|
|
_releasePoolStack.push_back(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-06-03 18:57:52 +08:00
|
|
|
CC_ASSERT(!_releasePoolStack.empty());
|
2014-01-24 14:46:06 +08:00
|
|
|
_releasePoolStack.pop_back();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|