2010-07-06 15:46:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
2010-07-06 20:55:05 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
2010-07-06 15:46: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.
|
|
|
|
****************************************************************************/
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCAutoreleasePool.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "ccMacros.h"
|
2010-07-06 15:46:45 +08:00
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
namespace cocos2d
|
|
|
|
{
|
2010-07-12 17:01:26 +08:00
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
CCPoolManager g_PoolManager;
|
2010-07-06 20:19:19 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAutoreleasePool::CCAutoreleasePool(void)
|
2010-07-06 20:19:19 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
m_pManagedObjectArray = new CCMutableArray<CCObject*>();
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAutoreleasePool::~CCAutoreleasePool(void)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2011-04-01 16:06:53 +08:00
|
|
|
CC_SAFE_DELETE(m_pManagedObjectArray);
|
2010-07-06 20:19:19 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCAutoreleasePool::addObject(CCObject* pObject)
|
2010-07-06 20:19:19 +08:00
|
|
|
{
|
2010-07-12 17:01:26 +08:00
|
|
|
m_pManagedObjectArray->addObject(pObject);
|
2010-08-20 09:31:35 +08:00
|
|
|
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(pObject->m_uReference > 1, "reference count should greager than 1");
|
2010-08-20 09:31:35 +08:00
|
|
|
|
|
|
|
pObject->release(); // no ref count, in this case autorelease pool added.
|
2010-07-06 20:19:19 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCAutoreleasePool::removeObject(CCObject* pObject)
|
2010-07-06 20:19:19 +08:00
|
|
|
{
|
2010-09-01 12:01:48 +08:00
|
|
|
m_pManagedObjectArray->removeObject(pObject, false);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
void CCAutoreleasePool::clear()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
if(m_pManagedObjectArray->count() > 0)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
//CCAutoreleasePool* pReleasePool;
|
2010-08-20 09:31:35 +08:00
|
|
|
#ifdef _DEBUG
|
|
|
|
int nIndex = m_pManagedObjectArray->count() - 1;
|
|
|
|
#endif
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCObject*>::CCMutableArrayRevIterator it;
|
2011-06-21 13:30:03 +08:00
|
|
|
for(it = m_pManagedObjectArray->rbegin(); it != m_pManagedObjectArray->rend(); ++it)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
if(!*it)
|
|
|
|
break;
|
|
|
|
|
|
|
|
(*it)->m_bManaged = false;
|
|
|
|
//(*it)->release();
|
|
|
|
//delete (*it);
|
|
|
|
#ifdef _DEBUG
|
|
|
|
nIndex--;
|
|
|
|
#endif
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
m_pManagedObjectArray->removeAllObjects();
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
2010-07-06 20:19:19 +08:00
|
|
|
}
|
|
|
|
|
2010-10-10 21:03:11 +08:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
2011-03-18 14:31:29 +08:00
|
|
|
// CCPoolManager
|
2010-10-10 21:03:11 +08:00
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
CCPoolManager* CCPoolManager::getInstance()
|
2010-07-06 20:19:19 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
return &g_PoolManager;
|
2010-07-06 20:19:19 +08:00
|
|
|
}
|
2010-07-12 17:01:26 +08:00
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
CCPoolManager::CCPoolManager()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
m_pReleasePoolStack = new CCMutableArray<CCAutoreleasePool*>();
|
2011-11-01 14:50:08 +08:00
|
|
|
m_pCurReleasePool = 0;
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
CCPoolManager::~CCPoolManager()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-09-04 11:36:40 +08:00
|
|
|
|
2010-07-12 17:01:26 +08:00
|
|
|
finalize();
|
|
|
|
|
2010-09-04 11:36:40 +08:00
|
|
|
// we only release the last autorelease pool here
|
2011-11-01 14:50:08 +08:00
|
|
|
m_pCurReleasePool = 0;
|
2010-09-04 11:36:40 +08:00
|
|
|
m_pReleasePoolStack->removeObjectAtIndex(0);
|
|
|
|
|
2011-04-01 16:06:53 +08:00
|
|
|
CC_SAFE_DELETE(m_pReleasePoolStack);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
void CCPoolManager::finalize()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
if(m_pReleasePoolStack->count() > 0)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
//CCAutoreleasePool* pReleasePool;
|
|
|
|
CCMutableArray<CCAutoreleasePool*>::CCMutableArrayIterator it;
|
2011-06-21 13:30:03 +08:00
|
|
|
for(it = m_pReleasePoolStack->begin(); it != m_pReleasePoolStack->end(); ++it)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
if(!*it)
|
|
|
|
break;
|
|
|
|
|
|
|
|
(*it)->clear();
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
void CCPoolManager::push()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAutoreleasePool* pPool = new CCAutoreleasePool(); //ref = 1
|
2010-08-20 09:31:35 +08:00
|
|
|
m_pCurReleasePool = pPool;
|
2010-07-12 17:01:26 +08:00
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
m_pReleasePoolStack->addObject(pPool); //ref = 2
|
|
|
|
|
|
|
|
pPool->release(); //ref = 1
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
void CCPoolManager::pop()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-10-10 21:03:11 +08:00
|
|
|
if (! m_pCurReleasePool)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-08-20 09:31:35 +08:00
|
|
|
|
2010-10-10 21:03:11 +08:00
|
|
|
int nCount = m_pReleasePoolStack->count();
|
2010-08-20 09:31:35 +08:00
|
|
|
|
2010-09-04 11:36:40 +08:00
|
|
|
m_pCurReleasePool->clear();
|
|
|
|
|
|
|
|
if(nCount > 1)
|
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
m_pReleasePoolStack->removeObjectAtIndex(nCount-1);
|
|
|
|
|
2010-09-04 11:36:40 +08:00
|
|
|
// if(nCount > 1)
|
|
|
|
// {
|
|
|
|
// m_pCurReleasePool = m_pReleasePoolStack->getObjectAtIndex(nCount - 2);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
m_pCurReleasePool = m_pReleasePoolStack->getObjectAtIndex(nCount - 2);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
2010-08-18 14:57:36 +08:00
|
|
|
|
2010-09-04 11:36:40 +08:00
|
|
|
/*m_pCurReleasePool = NULL;*/
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
void CCPoolManager::removeObject(CCObject* pObject)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(m_pCurReleasePool, "current auto release pool should not be null");
|
2010-08-20 09:31:35 +08:00
|
|
|
|
|
|
|
m_pCurReleasePool->removeObject(pObject);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
void CCPoolManager::addObject(CCObject* pObject)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
getCurReleasePool()->addObject(pObject);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
CCAutoreleasePool* CCPoolManager::getCurReleasePool()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
if(!m_pCurReleasePool)
|
2011-11-28 17:28:43 +08:00
|
|
|
{
|
2010-07-12 17:01:26 +08:00
|
|
|
push();
|
2011-11-28 17:28:43 +08:00
|
|
|
}
|
2010-07-12 17:01:26 +08:00
|
|
|
|
2011-11-28 17:28:43 +08:00
|
|
|
CCAssert(m_pCurReleasePool, "current auto release pool should not be null");
|
2010-08-20 09:31:35 +08:00
|
|
|
|
|
|
|
return m_pCurReleasePool;
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|