Pool.h 4.4 KB

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