Pool.inl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. namespace Rml {
  29. template < typename PoolType >
  30. Pool< PoolType >::Pool(int _chunk_size, bool _grow)
  31. {
  32. chunk_size = 0;
  33. grow = _grow;
  34. num_allocated_objects = 0;
  35. pool = nullptr;
  36. first_allocated_node = nullptr;
  37. first_free_node = nullptr;
  38. if (_chunk_size > 0)
  39. Initialise(_chunk_size, _grow);
  40. }
  41. template < typename PoolType >
  42. Pool< PoolType >::~Pool()
  43. {
  44. RMLUI_ASSERT(num_allocated_objects == 0);
  45. PoolChunk* chunk = pool;
  46. while (chunk)
  47. {
  48. PoolChunk* next_chunk = chunk->next;
  49. delete[] chunk->chunk;
  50. delete chunk;
  51. chunk = next_chunk;
  52. }
  53. }
  54. // Initialises the pool to a given size.
  55. template < typename PoolType >
  56. void Pool< PoolType >::Initialise(int _chunk_size, bool _grow)
  57. {
  58. // Should resize the pool here ... ?
  59. if (chunk_size > 0)
  60. return;
  61. if (_chunk_size <= 0)
  62. return;
  63. grow = _grow;
  64. chunk_size = _chunk_size;
  65. pool = nullptr;
  66. // Create the initial chunk.
  67. CreateChunk();
  68. }
  69. // Returns the head of the linked list of allocated objects.
  70. template < typename PoolType >
  71. typename Pool< PoolType >::Iterator Pool< PoolType >::Begin()
  72. {
  73. return typename Pool< PoolType >::Iterator(first_allocated_node);
  74. }
  75. // Attempts to allocate a deallocated object in the memory pool.
  76. template<typename PoolType>
  77. template<typename ...Args>
  78. inline PoolType* Pool<PoolType>::AllocateAndConstruct(Args&&... args)
  79. {
  80. // We can't allocate a new object if the deallocated list is empty.
  81. if (first_free_node == nullptr)
  82. {
  83. // Attempt to grow the pool first.
  84. if (grow)
  85. {
  86. CreateChunk();
  87. if (first_free_node == nullptr)
  88. return nullptr;
  89. }
  90. else
  91. return nullptr;
  92. }
  93. // We're about to allocate an object.
  94. ++num_allocated_objects;
  95. #ifdef RMLUI_DEBUG
  96. if (num_allocated_objects > max_num_allocated_objects)
  97. max_num_allocated_objects = num_allocated_objects;
  98. #endif
  99. // This one!
  100. PoolNode* allocated_object = first_free_node;
  101. // Remove the newly allocated object from the list of deallocated objects.
  102. first_free_node = first_free_node->next;
  103. if (first_free_node != nullptr)
  104. first_free_node->previous = nullptr;
  105. // Add the newly allocated object to the head of the list of allocated objects.
  106. if (first_allocated_node != nullptr)
  107. {
  108. allocated_object->previous = nullptr;
  109. allocated_object->next = first_allocated_node;
  110. first_allocated_node->previous = allocated_object;
  111. }
  112. else
  113. {
  114. // This object is the only allocated object.
  115. allocated_object->previous = nullptr;
  116. allocated_object->next = nullptr;
  117. }
  118. first_allocated_node = allocated_object;
  119. return new (allocated_object->object) PoolType(std::forward<Args>(args)...);
  120. }
  121. // Deallocates the object pointed to by the given iterator.
  122. template < typename PoolType >
  123. void Pool< PoolType >::DestroyAndDeallocate(Iterator& iterator)
  124. {
  125. // We're about to deallocate an object.
  126. --num_allocated_objects;
  127. PoolNode* object = iterator.node;
  128. reinterpret_cast<PoolType*>(object->object)->~PoolType();
  129. // Get the previous and next pointers now, because they will be overwritten
  130. // before we're finished.
  131. PoolNode* previous_object = object->previous;
  132. PoolNode* next_object = object->next;
  133. if (previous_object != nullptr)
  134. previous_object->next = next_object;
  135. else
  136. {
  137. RMLUI_ASSERT(first_allocated_node == object);
  138. first_allocated_node = next_object;
  139. }
  140. if (next_object != nullptr)
  141. next_object->previous = previous_object;
  142. // Insert the freed node at the beginning of the free object list.
  143. if (first_free_node == nullptr)
  144. {
  145. object->previous = nullptr;
  146. object->next = nullptr;
  147. }
  148. else
  149. {
  150. object->previous = nullptr;
  151. object->next = first_free_node;
  152. }
  153. first_free_node = object;
  154. // Increment the iterator, so it points to the next active object.
  155. iterator.node = next_object;
  156. }
  157. // Deallocates the given object.
  158. template < typename PoolType >
  159. void Pool< PoolType >::DestroyAndDeallocate(PoolType* object)
  160. {
  161. // This assumes the object has the same address as the node, which will be
  162. // true as long as the struct definition does not change.
  163. Iterator iterator((PoolNode*) object);
  164. DestroyAndDeallocate(iterator);
  165. }
  166. // Returns the number of objects in the pool.
  167. template < typename PoolType >
  168. int Pool< PoolType >::GetSize() const
  169. {
  170. return chunk_size * GetNumChunks();
  171. }
  172. /// Returns the number of object chunks in the pool.
  173. template < typename PoolType >
  174. int Pool< PoolType >::GetNumChunks() const
  175. {
  176. int num_chunks = 0;
  177. PoolChunk* chunk = pool;
  178. while (chunk != nullptr)
  179. {
  180. ++num_chunks;
  181. chunk = chunk->next;
  182. }
  183. return num_chunks;
  184. }
  185. // Returns the number of allocated objects in the pool.
  186. template < typename PoolType >
  187. int Pool< PoolType >::GetNumAllocatedObjects() const
  188. {
  189. return num_allocated_objects;
  190. }
  191. // Creates a new pool chunk and appends its nodes to the beginning of the free list.
  192. template < typename PoolType >
  193. void Pool< PoolType >::CreateChunk()
  194. {
  195. if (chunk_size <= 0)
  196. return;
  197. // Create the new chunk and mark it as the first chunk.
  198. PoolChunk* new_chunk = new PoolChunk();
  199. new_chunk->next = pool;
  200. pool = new_chunk;
  201. // Create chunk's pool nodes.
  202. new_chunk->chunk = new PoolNode[chunk_size];
  203. // Initialise the linked list.
  204. for (int i = 0; i < chunk_size; i++)
  205. {
  206. if (i == 0)
  207. new_chunk->chunk[i].previous = nullptr ;
  208. else
  209. new_chunk->chunk[i].previous = &new_chunk->chunk[i - 1];
  210. if (i == chunk_size - 1)
  211. new_chunk->chunk[i].next = first_free_node;
  212. else
  213. new_chunk->chunk[i].next = &new_chunk->chunk[i + 1];
  214. }
  215. first_free_node = new_chunk->chunk;
  216. }
  217. } // namespace Rml