1
0

CCAllocatorStrategyGlobalSmallBlock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. Author: Justin Graham (https://github.com/mannewalis)
  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. #ifndef CC_ALLOCATOR_STRATEGY_GLOBAL_SMALL_BLOCK_H
  22. #define CC_ALLOCATOR_STRATEGY_GLOBAL_SMALL_BLOCK_H
  23. /// @cond DO_NOT_SHOW
  24. /****************************************************************************
  25. WARNING!
  26. Do not use Console::log or any other methods that use NEW inside of this
  27. allocator. Failure to do so will result in recursive memory allocation.
  28. ****************************************************************************/
  29. #include "base/allocator/CCAllocatorMacros.h"
  30. #include "base/allocator/CCAllocatorBase.h"
  31. #include "base/allocator/CCAllocatorGlobal.h"
  32. #include "base/allocator/CCAllocatorStrategyFixedBlock.h"
  33. NS_CC_BEGIN
  34. NS_CC_ALLOCATOR_BEGIN
  35. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  36. #define TRACK(slot, size, op) _smallBlockAllocations[slot] op size
  37. #else
  38. #define TRACK(...)
  39. #endif
  40. // @brief
  41. class AllocatorStrategyGlobalSmallBlock
  42. : public AllocatorBase
  43. {
  44. public:
  45. // default number of block to allocate per page.
  46. static const size_t kDefaultSmallBlockCount = 100;
  47. // default max small block size pool.
  48. static const size_t kMaxSmallBlockPower = 13; // 2^13 8kb
  49. // @brief define for allocator strategy, cannot be typedef because we want to eval at use
  50. #define SType(size) AllocatorStrategyFixedBlock<size>
  51. void _lazy_init()
  52. {
  53. // this gets called before static constructors
  54. // so make sure we only get called once.
  55. static bool once = true;
  56. if (once)
  57. {
  58. once = false;
  59. // call our own constructor. Global new can be called before the constructors are called.
  60. // Make sure it gets called by having it done lazily in the call to allocate.
  61. new (this) AllocatorStrategyGlobalSmallBlock();
  62. }
  63. }
  64. AllocatorStrategyGlobalSmallBlock()
  65. {
  66. // this gets called before static constructors
  67. // so make sure we only get called once.
  68. static bool once = true;
  69. if (once)
  70. {
  71. once = false;
  72. _maxBlockSize = 1 << kMaxSmallBlockPower;
  73. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  74. AllocatorDiagnostics::instance()->trackAllocator(this);
  75. AllocatorBase::setTag("GlobalSmallBlock");
  76. #endif
  77. memset(_smallBlockAllocators, 0, sizeof(_smallBlockAllocators));
  78. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  79. memset(_smallBlockAllocations, 0, sizeof(_smallBlockAllocations));
  80. #endif
  81. // cannot call new on the allocator here because it will recurse
  82. // so instead we allocate from the global allocator and construct in place.
  83. #define SBA(n, size) \
  84. if (size <= _maxBlockSize) \
  85. { \
  86. auto v = ccAllocatorGlobal.allocate(sizeof(SType(size))); \
  87. _smallBlockAllocators[n] = (AllocatorBase*)(new (v) SType(size)("GlobalSmallBlock::"#size)); \
  88. }
  89. SBA(2, 4)
  90. SBA(3, 8);
  91. SBA(4, 16);
  92. SBA(5, 32);
  93. SBA(6, 64);
  94. SBA(7, 128);
  95. SBA(8, 256);
  96. SBA(9, 512);
  97. SBA(10, 1024);
  98. SBA(11, 2048);
  99. SBA(12, 4096);
  100. SBA(13, 8192);
  101. #undef SBA
  102. }
  103. }
  104. virtual ~AllocatorStrategyGlobalSmallBlock()
  105. {
  106. for (int i = 0; i <= kMaxSmallBlockPower; ++i)
  107. if (_smallBlockAllocators[i])
  108. ccAllocatorGlobal.deallocate(_smallBlockAllocators[i]);
  109. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  110. AllocatorDiagnostics::instance()->untrackAllocator(this);
  111. #endif
  112. }
  113. // @brief Allocate a block of some size. If the block is <= 8192 it is allocated out of an array
  114. // of fixed size block allocators. If larger, then we default back to the global allocator.
  115. // @param size Size of block to allocate. This will be rounded to the next power of two.
  116. CC_ALLOCATOR_INLINE void* allocate(size_t size)
  117. {
  118. _lazy_init();
  119. if (size < sizeof(intptr_t)) // always allocate at least enough space to store a pointer. this is
  120. size = sizeof(intptr_t); // so we can link the empty blocks together in the block allocator.
  121. // if the size is greater than what we determine to be a small block
  122. // size then fall through to calling the global allocator instead.
  123. if (size > _maxBlockSize)
  124. return ccAllocatorGlobal.allocate(size);
  125. // make sure the size fits into one of the
  126. // fixed sized block allocators we have above.
  127. size_t adjusted_size = AllocatorBase::nextPow2BlockSize(size);
  128. #define ALLOCATE(slot, size) \
  129. case size: \
  130. { \
  131. void* v = _smallBlockAllocators[slot]; \
  132. CC_ASSERT(nullptr != v); \
  133. auto a = (SType(size)*)v; \
  134. address = a->allocate(adjusted_size); \
  135. TRACK(slot, size, +=); \
  136. } \
  137. break;
  138. void* address = nullptr;
  139. switch (adjusted_size)
  140. {
  141. ALLOCATE(2, 4);
  142. ALLOCATE(3, 8);
  143. ALLOCATE(4, 16);
  144. ALLOCATE(5, 32);
  145. ALLOCATE(6, 64);
  146. ALLOCATE(7, 128);
  147. ALLOCATE(8, 256);
  148. ALLOCATE(9, 512);
  149. ALLOCATE(10, 1024);
  150. ALLOCATE(11, 2048);
  151. ALLOCATE(12, 4096);
  152. ALLOCATE(13, 8192);
  153. default:
  154. CC_ASSERT(false);
  155. break;
  156. }
  157. #undef ALLOCATE
  158. CC_ASSERT(adjusted_size < AllocatorBase::kDefaultAlignment || 0 == ((intptr_t)address & (AllocatorBase::kDefaultAlignment - 1)));
  159. CC_ASSERT(nullptr != address);
  160. return address;
  161. }
  162. // @brief Deallocate a block by choosing one of the fixed size block allocators
  163. // or defaulting to the global allocator if we do not own this block.
  164. CC_ALLOCATOR_INLINE void deallocate(void* address, size_t size = 0)
  165. {
  166. // if we didn't get a size, then we need to find the allocator
  167. // by asking each if they own the block. For allocators that
  168. // have few large pages, this is extremely fast.
  169. if (0 == size)
  170. {
  171. #define OWNS(slot, S, address) \
  172. case S: \
  173. { \
  174. void* v = _smallBlockAllocators[slot]; \
  175. if (v) \
  176. { \
  177. auto a = (SType(S)*)v; \
  178. if (a->owns(address)) \
  179. { \
  180. size = SType(S)::block_size; \
  181. break; \
  182. } \
  183. } \
  184. }
  185. // falls through until found
  186. switch (sizeof(uint32_t))
  187. {
  188. OWNS(2, 4, address);
  189. OWNS(3, 8, address);
  190. OWNS(4, 16, address);
  191. OWNS(5, 32, address);
  192. OWNS(6, 64, address);
  193. OWNS(7, 128, address);
  194. OWNS(8, 256, address);
  195. OWNS(9, 512, address);
  196. OWNS(10, 1024, address);
  197. OWNS(11, 2048, address);
  198. OWNS(12, 4096, address);
  199. OWNS(13, 8192, address);
  200. }
  201. }
  202. // if the size is greater than what we determine to be a small block
  203. // size then default to calling the global allocator instead.
  204. if (0 == size || size > _maxBlockSize)
  205. return ccAllocatorGlobal.deallocate(address, size);
  206. if (size < sizeof(intptr_t)) // always allocate at least enough space to store a pointer. this is
  207. size = sizeof(intptr_t); // so we can link the empty blocks together in the block allocator.
  208. // make sure the size fits into one of the
  209. // fixed sized block allocators we have above.
  210. size_t adjusted_size = AllocatorBase::nextPow2BlockSize(size);
  211. #define DEALLOCATE(slot, size, address) \
  212. case size: \
  213. { \
  214. void* v = _smallBlockAllocators[slot]; \
  215. CC_ASSERT(nullptr != v); \
  216. auto a = (SType(size)*)v; \
  217. a->deallocate(address, size); \
  218. TRACK(slot, size, -=); \
  219. } \
  220. break;
  221. switch (adjusted_size)
  222. {
  223. DEALLOCATE(2, 4, address);
  224. DEALLOCATE(3, 8, address);
  225. DEALLOCATE(4, 16, address);
  226. DEALLOCATE(5, 32, address);
  227. DEALLOCATE(6, 64, address);
  228. DEALLOCATE(7, 128, address);
  229. DEALLOCATE(8, 256, address);
  230. DEALLOCATE(9, 512, address);
  231. DEALLOCATE(10, 1024, address);
  232. DEALLOCATE(11, 2048, address);
  233. DEALLOCATE(12, 4096, address);
  234. DEALLOCATE(13, 8192, address);
  235. default:
  236. CC_ASSERT(false);
  237. }
  238. #undef DEALLOCATE
  239. }
  240. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  241. std::string diagnostics() const
  242. {
  243. std::stringstream s;
  244. size_t total = 0;
  245. for (auto i = 2; i <= kMaxSmallBlockPower; ++i)
  246. {
  247. auto a = _smallBlockAllocators[i];
  248. if (a)
  249. {
  250. total += _smallBlockAllocations[i];
  251. s << a->tag() << " allocated:" << _smallBlockAllocations[i] << "\n";
  252. }
  253. }
  254. s << "Total:" << total << "\n";
  255. return s.str();
  256. }
  257. size_t _highestCount;
  258. #endif
  259. protected:
  260. // @brief the max size of a block this allocator will pool before using global allocator
  261. size_t _maxBlockSize;
  262. // @brief array of small block allocators from 2^0 -> 2^kMaxSmallBlockPower
  263. AllocatorBase* _smallBlockAllocators[kMaxSmallBlockPower + 1];
  264. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  265. size_t _smallBlockAllocations[kMaxSmallBlockPower + 1];
  266. #endif
  267. };
  268. NS_CC_ALLOCATOR_END
  269. NS_CC_END
  270. /// @endcond
  271. #endif//CC_ALLOCATOR_STRATEGY_GLOBAL_SMALL_BLOCK_H