CCAutoreleasePool.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCAutoreleasePool.h"
  22. #include "base/ccMacros.h"
  23. NS_CC_BEGIN
  24. AutoreleasePool::AutoreleasePool()
  25. : _name("")
  26. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  27. , _isClearing(false)
  28. #endif
  29. {
  30. _managedObjectArray.reserve(150);
  31. PoolManager::getInstance()->push(this);
  32. }
  33. AutoreleasePool::AutoreleasePool(const std::string &name)
  34. : _name(name)
  35. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  36. , _isClearing(false)
  37. #endif
  38. {
  39. _managedObjectArray.reserve(150);
  40. PoolManager::getInstance()->push(this);
  41. }
  42. AutoreleasePool::~AutoreleasePool()
  43. {
  44. CCLOGINFO("deallocing AutoreleasePool: %p", this);
  45. clear();
  46. PoolManager::getInstance()->pop();
  47. }
  48. void AutoreleasePool::addObject(Ref* object)
  49. {
  50. _managedObjectArray.push_back(object);
  51. }
  52. void AutoreleasePool::clear()
  53. {
  54. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  55. _isClearing = true;
  56. #endif
  57. std::vector<Ref*> releasings;
  58. releasings.swap(_managedObjectArray);
  59. for (const auto &obj : releasings)
  60. {
  61. obj->release();
  62. }
  63. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  64. _isClearing = false;
  65. #endif
  66. }
  67. bool AutoreleasePool::contains(Ref* object) const
  68. {
  69. for (const auto& obj : _managedObjectArray)
  70. {
  71. if (obj == object)
  72. return true;
  73. }
  74. return false;
  75. }
  76. void AutoreleasePool::dump()
  77. {
  78. CCLOG("autorelease pool: %s, number of managed object %d\n", _name.c_str(), static_cast<int>(_managedObjectArray.size()));
  79. CCLOG("%20s%20s%20s", "Object pointer", "Object id", "reference count");
  80. for (const auto &obj : _managedObjectArray)
  81. {
  82. CCLOG("%20p%20u\n", obj, obj->getReferenceCount());
  83. }
  84. }
  85. //--------------------------------------------------------------------
  86. //
  87. // PoolManager
  88. //
  89. //--------------------------------------------------------------------
  90. PoolManager* PoolManager::s_singleInstance = nullptr;
  91. PoolManager* PoolManager::getInstance()
  92. {
  93. if (s_singleInstance == nullptr)
  94. {
  95. s_singleInstance = new (std::nothrow) PoolManager();
  96. // Add the first auto release pool
  97. new AutoreleasePool("cocos2d autorelease pool");
  98. }
  99. return s_singleInstance;
  100. }
  101. void PoolManager::destroyInstance()
  102. {
  103. delete s_singleInstance;
  104. s_singleInstance = nullptr;
  105. }
  106. PoolManager::PoolManager()
  107. {
  108. _releasePoolStack.reserve(10);
  109. }
  110. PoolManager::~PoolManager()
  111. {
  112. CCLOGINFO("deallocing PoolManager: %p", this);
  113. while (!_releasePoolStack.empty())
  114. {
  115. AutoreleasePool* pool = _releasePoolStack.back();
  116. delete pool;
  117. }
  118. }
  119. AutoreleasePool* PoolManager::getCurrentPool() const
  120. {
  121. return _releasePoolStack.back();
  122. }
  123. bool PoolManager::isObjectInPools(Ref* obj) const
  124. {
  125. for (const auto& pool : _releasePoolStack)
  126. {
  127. if (pool->contains(obj))
  128. return true;
  129. }
  130. return false;
  131. }
  132. void PoolManager::push(AutoreleasePool *pool)
  133. {
  134. _releasePoolStack.push_back(pool);
  135. }
  136. void PoolManager::pop()
  137. {
  138. CC_ASSERT(!_releasePoolStack.empty());
  139. _releasePoolStack.pop_back();
  140. }
  141. NS_CC_END