Pool.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef EMPCOREPOOL_H
  28. #define EMPCOREPOOL_H
  29. #include <EMP/Core/Header.h>
  30. #include <EMP/Core/Debug.h>
  31. namespace EMP {
  32. namespace Core {
  33. template < typename PoolType >
  34. class Pool
  35. {
  36. private:
  37. class PoolNode
  38. {
  39. public:
  40. PoolType object;
  41. PoolNode* previous;
  42. PoolNode* next;
  43. };
  44. class PoolChunk
  45. {
  46. public:
  47. PoolNode* chunk;
  48. PoolChunk* next;
  49. };
  50. public:
  51. /**
  52. Iterator objects are used for safe traversal of the allocated
  53. members of a pool.
  54. */
  55. class Iterator
  56. {
  57. friend class Pool< PoolType >;
  58. public :
  59. /// Increments the iterator to reference the next node in the
  60. /// linked list. It is an error to call this function if the
  61. /// node this iterator references is invalid.
  62. inline void operator++()
  63. {
  64. ROCKET_ASSERT(node != NULL);
  65. node = node->next;
  66. }
  67. /// Returns true if it is OK to deference or increment this
  68. /// iterator.
  69. inline operator bool()
  70. {
  71. return (node != NULL);
  72. }
  73. /// Returns the object referenced by the iterator's current
  74. /// node.
  75. inline PoolType& operator*()
  76. {
  77. return node->object;
  78. }
  79. /// Returns a pointer to the object referenced by the
  80. /// iterator's current node.
  81. inline PoolType* operator->()
  82. {
  83. return &node->object;
  84. }
  85. private:
  86. // Constructs an iterator referencing the given node.
  87. inline Iterator(PoolNode* node)
  88. {
  89. this->node = node;
  90. }
  91. PoolNode* node;
  92. };
  93. Pool(int chunk_size = 0, bool grow = false);
  94. ~Pool();
  95. /// Initialises the pool to a given size.
  96. void Initialise(int chunk_size, bool grow = false);
  97. /// Returns the head of the linked list of allocated objects.
  98. inline Iterator Begin();
  99. /// Attempts to allocate a deallocated object in the memory pool. If
  100. /// the process is successful, the newly allocated object is returned.
  101. /// If the process fails (due to no free objects being available), NULL
  102. /// is returned.
  103. inline PoolType* AllocateObject();
  104. /// Deallocates the object pointed to by the given iterator.
  105. inline void DeallocateObject(Iterator& iterator);
  106. /// Deallocates the given object.
  107. inline void DeallocateObject(PoolType* object);
  108. /// Returns the number of objects in the pool.
  109. inline int GetSize() const;
  110. /// Returns the number of object chunks in the pool.
  111. inline int GetNumChunks() const;
  112. /// Returns the number of allocated objects in the pool.
  113. inline int GetNumAllocatedObjects() const;
  114. private:
  115. // Creates a new pool chunk and appends its nodes to the beginning of the free list.
  116. void CreateChunk();
  117. int chunk_size;
  118. bool grow;
  119. PoolChunk* pool;
  120. // The heads of the two linked lists.
  121. PoolNode* first_allocated_node;
  122. PoolNode* first_free_node;
  123. int num_allocated_objects;
  124. };
  125. #include <EMP/Core/Pool.inl>
  126. }
  127. }
  128. #endif