Pool.inl 6.2 KB

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