CCAutoreleasePool.cpp 4.3 KB

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