Pool.h 4.0 KB

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