CCAllocatorStrategyPool.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_POOL_H
  22. #define CC_ALLOCATOR_STRATEGY_POOL_H
  23. /// @cond DO_NOT_SHOW
  24. #include <vector>
  25. #include <typeinfo>
  26. #include <sstream>
  27. #include "base/allocator/CCAllocatorMacros.h"
  28. #include "base/allocator/CCAllocatorGlobal.h"
  29. #include "base/allocator/CCAllocatorStrategyFixedBlock.h"
  30. #include "base/allocator/CCAllocatorDiagnostics.h"
  31. #include "base/CCConfiguration.h"
  32. NS_CC_BEGIN
  33. NS_CC_ALLOCATOR_BEGIN
  34. /**
  35. * ObjectTraits describes an allocatable object.
  36. *
  37. * Template class that represents a default allocatable object.
  38. * Provide custom implementations to change the constructor/destructor behavior,
  39. * or to change the default alignment of the object in memory.
  40. *
  41. * @param T Type of object.
  42. * @param _alignment Alignment of object T.
  43. */
  44. template <typename T, size_t _alignment = sizeof(uint32_t)>
  45. class ObjectTraits
  46. {
  47. public:
  48. typedef T value_type;
  49. static const size_t alignment = _alignment;
  50. virtual ~ObjectTraits()
  51. {}
  52. /** Constructor implementation for type T.*/
  53. void construct(T* address)
  54. {
  55. ::new(address) T();
  56. }
  57. /** Destructor implementation for type T.*/
  58. void destroy(T* address)
  59. {
  60. address->~T();
  61. }
  62. /** Returns the name of this object type T.*/
  63. const char* name() const
  64. {
  65. return typeid(T).name();
  66. }
  67. };
  68. /**
  69. * Fixed sized pool allocator strategy for objects of type T.
  70. *
  71. * Optionally takes a page size which determines how many objects are added when the allocator needs more storage.
  72. * ObjectTraits allows you to control the alignment, construction and destruction of an object in the pool.
  73. *
  74. * @param T Type of object.
  75. * @param _page_size Number of objects of T in each page.
  76. * @param O ObjectTraits for type T
  77. * @see CC_USE_ALLOCATOR_POOL
  78. */
  79. template <typename T, typename O = ObjectTraits<T>, typename locking_traits = lockless_semantics>
  80. class AllocatorStrategyPool
  81. : public AllocatorStrategyFixedBlock<sizeof(T), O::alignment, locking_traits>
  82. , public O
  83. {
  84. public:
  85. typedef T value_type;
  86. typedef value_type* pointer;
  87. /** Ugh wish I knew a way that I could declare this just once.*/
  88. typedef AllocatorStrategyFixedBlock<sizeof(T), O::alignment, locking_traits> tParentStrategy;
  89. AllocatorStrategyPool(const char* tag = nullptr, size_t poolSize = 100)
  90. : tParentStrategy(tag)
  91. {
  92. poolSize = Configuration::getInstance()->getValue(tag, Value((int)poolSize)).asInt();
  93. tParentStrategy::_pageSize = poolSize;
  94. }
  95. /**
  96. * Allocate block of size T.
  97. *
  98. * If size does not match sizeof(T) then the global allocator is called instead.
  99. * @see CC_USE_ALLOCATOR_POOL
  100. */
  101. CC_ALLOCATOR_INLINE void* allocate(size_t size)
  102. {
  103. T* object;
  104. if (sizeof(T) == size)
  105. {
  106. object = (pointer)tParentStrategy::allocate(sizeof(T));
  107. }
  108. else
  109. {
  110. object = (T*)ccAllocatorGlobal.allocate(size);
  111. }
  112. O::construct(object);
  113. return object;
  114. }
  115. /**
  116. * Deallocate block of size T.
  117. *
  118. * If size does not match sizeof(T) then the global allocator is called instead.
  119. * @see CC_USE_ALLOCATOR_POOL
  120. */
  121. CC_ALLOCATOR_INLINE void deallocate(void* address, size_t size = 0)
  122. {
  123. if (address)
  124. {
  125. O::destroy((T*)address);
  126. if (sizeof(T) == size)
  127. {
  128. tParentStrategy::deallocate(address, sizeof(T));
  129. }
  130. else
  131. {
  132. ccAllocatorGlobal.deallocate(address, size);
  133. }
  134. }
  135. }
  136. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  137. std::string diagnostics() const
  138. {
  139. std::stringstream s;
  140. s << AllocatorBase::tag() << " initial:" << tParentStrategy::_pageSize << " count:" << tParentStrategy::_allocated << " highest:" << tParentStrategy::_highestCount << "\n";
  141. return s.str();
  142. }
  143. #endif
  144. };
  145. NS_CC_ALLOCATOR_END
  146. NS_CC_END
  147. /// @endcond
  148. #endif//CC_ALLOCATOR_STRATEGY_POOL_H