2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
AutoreleasePool::AutoreleasePool()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _name("")
|
2022-07-16 10:43:05 +08:00
|
|
|
#if defined(AXIS_DEBUG) && (AXIS_DEBUG > 0)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _isClearing(false)
|
2014-01-24 17:51:36 +08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
AutoreleasePool::AutoreleasePool(std::string_view name)
|
2021-12-25 10:04:45 +08:00
|
|
|
: _name(name)
|
2022-07-16 10:43:05 +08:00
|
|
|
#if defined(AXIS_DEBUG) && (AXIS_DEBUG > 0)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _isClearing(false)
|
2014-01-24 17:51:36 +08:00
|
|
|
#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()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("deallocing AutoreleasePool: %p", this);
|
2014-01-22 11:40:07 +08:00
|
|
|
clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if defined(AXIS_DEBUG) && (AXIS_DEBUG > 0)
|
2014-01-24 17:51:36 +08:00
|
|
|
_isClearing = true;
|
|
|
|
#endif
|
2015-03-11 15:31:06 +08:00
|
|
|
std::vector<Ref*> releasings;
|
|
|
|
releasings.swap(_managedObjectArray);
|
2021-12-25 10:04:45 +08:00
|
|
|
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
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#if defined(AXIS_DEBUG) && (AXIS_DEBUG > 0)
|
2014-01-24 17:51:36 +08:00
|
|
|
_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()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("autorelease pool: %s, number of managed object %d\n", _name.c_str(),
|
2021-12-25 10:04:45 +08:00
|
|
|
static_cast<int>(_managedObjectArray.size()));
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("%20s%20s%20s", "Object pointer", "Object id", "reference count");
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& obj : _managedObjectArray)
|
2014-01-21 17:47:59 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("%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
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
s_singleInstance = new 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()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("deallocing PoolManager: %p", this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +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
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_ASSERT(!_releasePoolStack.empty());
|
2014-01-24 14:46:06 +08:00
|
|
|
_releasePoolStack.pop_back();
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|