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.
|
|
|
|
****************************************************************************/
|
2010-08-20 09:31:35 +08:00
|
|
|
#include "NSAutoreleasePool.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
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
NSPoolManager g_PoolManager;
|
2010-07-06 20:19:19 +08:00
|
|
|
|
|
|
|
NSAutoreleasePool::NSAutoreleasePool(void)
|
|
|
|
{
|
2010-07-20 11:25:37 +08:00
|
|
|
m_pManagedObjectArray = new NSMutableArray<NSObject*>();
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSAutoreleasePool::~NSAutoreleasePool(void)
|
|
|
|
{
|
|
|
|
delete m_pManagedObjectArray;
|
2010-07-06 20:19:19 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSAutoreleasePool::addObject(NSObject* 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
|
|
|
|
|
|
|
assert(pObject->m_uRefrence > 1);
|
|
|
|
|
|
|
|
pObject->release(); // no ref count, in this case autorelease pool added.
|
2010-07-06 20:19:19 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSAutoreleasePool::removeObject(NSObject* 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
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSAutoreleasePool::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
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
//NSAutoreleasePool* pReleasePool;
|
|
|
|
#ifdef _DEBUG
|
|
|
|
int nIndex = m_pManagedObjectArray->count() - 1;
|
|
|
|
#endif
|
|
|
|
NSMutableArray<NSObject*>::NSMutableArrayRevIterator it;
|
|
|
|
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-08-20 09:31:35 +08:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// NSPoolManager
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
NSPoolManager* NSPoolManager::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
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
NSPoolManager::NSPoolManager()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
m_pReleasePoolStack = new NSMutableArray<NSAutoreleasePool*>();
|
|
|
|
m_pCurReleasePool = NULL;
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
NSPoolManager::~NSPoolManager()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
|
|
|
|
delete m_pReleasePoolStack;
|
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSPoolManager::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
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
//NSAutoreleasePool* pReleasePool;
|
|
|
|
NSMutableArray<NSAutoreleasePool*>::NSMutableArrayIterator it;
|
|
|
|
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();
|
|
|
|
|
|
|
|
delete (*it);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSPoolManager::push()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
NSAutoreleasePool* pPool = new NSAutoreleasePool(); //ref = 1
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSPoolManager::pop()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
int nCount = m_pReleasePoolStack->count();
|
|
|
|
|
|
|
|
if(nCount > 0)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
assert(m_pCurReleasePool);
|
|
|
|
|
|
|
|
m_pCurReleasePool->clear();
|
|
|
|
|
|
|
|
m_pReleasePoolStack->removeObjectAtIndex(nCount-1);
|
|
|
|
|
|
|
|
if(nCount > 1)
|
|
|
|
{
|
|
|
|
m_pCurReleasePool = m_pReleasePoolStack->getObjectAtIndex(nCount - 2);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
2010-08-18 14:57:36 +08:00
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
m_pCurReleasePool = NULL;
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSPoolManager::removeObject(NSObject* pObject)
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
assert(m_pCurReleasePool);
|
|
|
|
|
|
|
|
m_pCurReleasePool->removeObject(pObject);
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
void NSPoolManager::addObject(NSObject* 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
|
|
|
|
|
|
|
NSAutoreleasePool* NSPoolManager::getCurReleasePool()
|
2010-07-12 17:01:26 +08:00
|
|
|
{
|
2010-08-20 09:31:35 +08:00
|
|
|
if(!m_pCurReleasePool)
|
2010-07-12 17:01:26 +08:00
|
|
|
push();
|
|
|
|
|
2010-08-20 09:31:35 +08:00
|
|
|
assert(m_pCurReleasePool);
|
|
|
|
|
|
|
|
return m_pCurReleasePool;
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:01:26 +08:00
|
|
|
}
|