Pool.inl 6.2 KB

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