Pool.inl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 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. namespace Core {
  30. template < typename PoolType >
  31. Pool< PoolType >::Pool(int _chunk_size, bool _grow)
  32. {
  33. chunk_size = 0;
  34. grow = _grow;
  35. num_allocated_objects = 0;
  36. pool = nullptr;
  37. first_allocated_node = nullptr;
  38. first_free_node = nullptr;
  39. if (_chunk_size > 0)
  40. Initialise(_chunk_size, _grow);
  41. }
  42. template < typename PoolType >
  43. Pool< PoolType >::~Pool()
  44. {
  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. // This one!
  96. PoolNode* allocated_object = first_free_node;
  97. // Remove the newly allocated object from the list of deallocated objects.
  98. first_free_node = first_free_node->next;
  99. if (first_free_node != nullptr)
  100. first_free_node->previous = nullptr;
  101. // Add the newly allocated object to the head of the list of allocated objects.
  102. if (first_allocated_node != nullptr)
  103. {
  104. allocated_object->previous = nullptr;
  105. allocated_object->next = first_allocated_node;
  106. first_allocated_node->previous = allocated_object;
  107. }
  108. else
  109. {
  110. // This object is the only allocated object.
  111. allocated_object->previous = nullptr;
  112. allocated_object->next = nullptr;
  113. }
  114. first_allocated_node = allocated_object;
  115. return new (allocated_object->object) PoolType(std::forward<Args>(args)...);
  116. }
  117. // Deallocates the object pointed to by the given iterator.
  118. template < typename PoolType >
  119. void Pool< PoolType >::DestroyAndDeallocate(Iterator& iterator)
  120. {
  121. // We're about to deallocate an object.
  122. --num_allocated_objects;
  123. PoolNode* object = iterator.node;
  124. reinterpret_cast<PoolType*>(object->object)->~PoolType();
  125. // Get the previous and next pointers now, because they will be overwritten
  126. // before we're finished.
  127. PoolNode* previous_object = object->previous;
  128. PoolNode* next_object = object->next;
  129. if (previous_object != nullptr)
  130. previous_object->next = next_object;
  131. else
  132. {
  133. RMLUI_ASSERT(first_allocated_node == object);
  134. first_allocated_node = next_object;
  135. }
  136. if (next_object != nullptr)
  137. next_object->previous = previous_object;
  138. // Insert the freed node at the beginning of the free object list.
  139. if (first_free_node == nullptr)
  140. {
  141. object->previous = nullptr;
  142. object->next = nullptr;
  143. }
  144. else
  145. {
  146. object->previous = nullptr;
  147. object->next = first_free_node;
  148. }
  149. first_free_node = object;
  150. // Increment the iterator, so it points to the next active object.
  151. iterator.node = next_object;
  152. }
  153. // Deallocates the given object.
  154. template < typename PoolType >
  155. void Pool< PoolType >::DestroyAndDeallocate(PoolType* object)
  156. {
  157. // This assumes the object has the same address as the node, which will be
  158. // true as long as the struct definition does not change.
  159. Iterator iterator((PoolNode*) object);
  160. DestroyAndDeallocate(iterator);
  161. }
  162. // Returns the number of objects in the pool.
  163. template < typename PoolType >
  164. int Pool< PoolType >::GetSize() const
  165. {
  166. return chunk_size * GetNumChunks();
  167. }
  168. /// Returns the number of object chunks in the pool.
  169. template < typename PoolType >
  170. int Pool< PoolType >::GetNumChunks() const
  171. {
  172. int num_chunks = 0;
  173. PoolChunk* chunk = pool;
  174. while (chunk != nullptr)
  175. {
  176. ++num_chunks;
  177. chunk = chunk->next;
  178. }
  179. return num_chunks;
  180. }
  181. // Returns the number of allocated objects in the pool.
  182. template < typename PoolType >
  183. int Pool< PoolType >::GetNumAllocatedObjects() const
  184. {
  185. return num_allocated_objects;
  186. }
  187. // Creates a new pool chunk and appends its nodes to the beginning of the free list.
  188. template < typename PoolType >
  189. void Pool< PoolType >::CreateChunk()
  190. {
  191. if (chunk_size <= 0)
  192. return;
  193. // Create the new chunk and mark it as the first chunk.
  194. PoolChunk* new_chunk = new PoolChunk();
  195. new_chunk->next = pool;
  196. pool = new_chunk;
  197. // Create chunk's pool nodes.
  198. new_chunk->chunk = new PoolNode[chunk_size];
  199. // Initialise the linked list.
  200. for (int i = 0; i < chunk_size; i++)
  201. {
  202. if (i == 0)
  203. new_chunk->chunk[i].previous = nullptr ;
  204. else
  205. new_chunk->chunk[i].previous = &new_chunk->chunk[i - 1];
  206. if (i == chunk_size - 1)
  207. new_chunk->chunk[i].next = first_free_node;
  208. else
  209. new_chunk->chunk[i].next = &new_chunk->chunk[i + 1];
  210. }
  211. first_free_node = new_chunk->chunk;
  212. }
  213. }
  214. }