Pool.h 4.2 KB

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