Pool.h 4.5 KB

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