Pool.inl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. 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. PoolChunk* chunk = pool;
  45. while (chunk)
  46. {
  47. PoolChunk* next_chunk = chunk->next;
  48. delete[] chunk->chunk;
  49. delete chunk;
  50. chunk = next_chunk;
  51. }
  52. }
  53. // Initialises the pool to a given size.
  54. template < typename PoolType >
  55. void Pool< PoolType >::Initialise(int _chunk_size, bool _grow)
  56. {
  57. // Should resize the pool here ... ?
  58. if (chunk_size > 0)
  59. return;
  60. if (_chunk_size <= 0)
  61. return;
  62. grow = _grow;
  63. chunk_size = _chunk_size;
  64. pool = nullptr;
  65. // Create the initial chunk.
  66. CreateChunk();
  67. }
  68. // Returns the head of the linked list of allocated objects.
  69. template < typename PoolType >
  70. typename Pool< PoolType >::Iterator Pool< PoolType >::Begin()
  71. {
  72. return typename Pool< PoolType >::Iterator(first_allocated_node);
  73. }
  74. // Attempts to allocate a deallocated object in the memory pool.
  75. template<typename PoolType>
  76. template<typename ...Args>
  77. inline PoolType* Pool<PoolType>::AllocateAndConstruct(Args&&... args)
  78. {
  79. // We can't allocate a new object if the deallocated list is empty.
  80. if (first_free_node == nullptr)
  81. {
  82. // Attempt to grow the pool first.
  83. if (grow)
  84. {
  85. CreateChunk();
  86. if (first_free_node == nullptr)
  87. return nullptr;
  88. }
  89. else
  90. return nullptr;
  91. }
  92. // We're about to allocate an object.
  93. ++num_allocated_objects;
  94. // This one!
  95. PoolNode* allocated_object = first_free_node;
  96. // Remove the newly allocated object from the list of deallocated objects.
  97. first_free_node = first_free_node->next;
  98. if (first_free_node != nullptr)
  99. first_free_node->previous = nullptr;
  100. // Add the newly allocated object to the head of the list of allocated objects.
  101. if (first_allocated_node != nullptr)
  102. {
  103. allocated_object->previous = nullptr;
  104. allocated_object->next = first_allocated_node;
  105. first_allocated_node->previous = allocated_object;
  106. }
  107. else
  108. {
  109. // This object is the only allocated object.
  110. allocated_object->previous = nullptr;
  111. allocated_object->next = nullptr;
  112. }
  113. first_allocated_node = allocated_object;
  114. return new (allocated_object->object) PoolType(std::forward<Args>(args)...);
  115. }
  116. // Deallocates the object pointed to by the given iterator.
  117. template < typename PoolType >
  118. void Pool< PoolType >::DestroyAndDeallocate(Iterator& iterator)
  119. {
  120. // We're about to deallocate an object.
  121. --num_allocated_objects;
  122. PoolNode* object = iterator.node;
  123. reinterpret_cast<PoolType*>(object->object)->~PoolType();
  124. // Get the previous and next pointers now, because they will be overwritten
  125. // before we're finished.
  126. PoolNode* previous_object = object->previous;
  127. PoolNode* next_object = object->next;
  128. if (previous_object != nullptr)
  129. previous_object->next = next_object;
  130. else
  131. {
  132. RMLUI_ASSERT(first_allocated_node == object);
  133. first_allocated_node = next_object;
  134. }
  135. if (next_object != nullptr)
  136. next_object->previous = previous_object;
  137. // Insert the freed node at the beginning of the free object list.
  138. if (first_free_node == nullptr)
  139. {
  140. object->previous = nullptr;
  141. object->next = nullptr;
  142. }
  143. else
  144. {
  145. object->previous = nullptr;
  146. object->next = first_free_node;
  147. }
  148. first_free_node = object;
  149. // Increment the iterator, so it points to the next active object.
  150. iterator.node = next_object;
  151. }
  152. // Deallocates the given object.
  153. template < typename PoolType >
  154. void Pool< PoolType >::DestroyAndDeallocate(PoolType* object)
  155. {
  156. // This assumes the object has the same address as the node, which will be
  157. // true as long as the struct definition does not change.
  158. Iterator iterator((PoolNode*) object);
  159. DestroyAndDeallocate(iterator);
  160. }
  161. // Returns the number of objects in the pool.
  162. template < typename PoolType >
  163. int Pool< PoolType >::GetSize() const
  164. {
  165. return chunk_size * GetNumChunks();
  166. }
  167. /// Returns the number of object chunks in the pool.
  168. template < typename PoolType >
  169. int Pool< PoolType >::GetNumChunks() const
  170. {
  171. int num_chunks = 0;
  172. PoolChunk* chunk = pool;
  173. while (chunk != nullptr)
  174. {
  175. ++num_chunks;
  176. chunk = chunk->next;
  177. }
  178. return num_chunks;
  179. }
  180. // Returns the number of allocated objects in the pool.
  181. template < typename PoolType >
  182. int Pool< PoolType >::GetNumAllocatedObjects() const
  183. {
  184. return num_allocated_objects;
  185. }
  186. // Creates a new pool chunk and appends its nodes to the beginning of the free list.
  187. template < typename PoolType >
  188. void Pool< PoolType >::CreateChunk()
  189. {
  190. if (chunk_size <= 0)
  191. return;
  192. // Create the new chunk and mark it as the first chunk.
  193. PoolChunk* new_chunk = new PoolChunk();
  194. new_chunk->next = pool;
  195. pool = new_chunk;
  196. // Create chunk's pool nodes.
  197. new_chunk->chunk = new PoolNode[chunk_size];
  198. // Initialise the linked list.
  199. for (int i = 0; i < chunk_size; i++)
  200. {
  201. if (i == 0)
  202. new_chunk->chunk[i].previous = nullptr ;
  203. else
  204. new_chunk->chunk[i].previous = &new_chunk->chunk[i - 1];
  205. if (i == chunk_size - 1)
  206. new_chunk->chunk[i].next = first_free_node;
  207. else
  208. new_chunk->chunk[i].next = &new_chunk->chunk[i + 1];
  209. }
  210. first_free_node = new_chunk->chunk;
  211. }
  212. } // namespace Rml