2
0

Pool.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-2023 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/Debug.h"
  31. #include "../../Include/RmlUi/Core/Header.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. private:
  38. static constexpr size_t N = sizeof(PoolType);
  39. static constexpr size_t A = alignof(PoolType);
  40. class PoolNode : public NonCopyMoveable {
  41. public:
  42. alignas(A) unsigned char object[N];
  43. PoolNode* previous;
  44. PoolNode* next;
  45. };
  46. class PoolChunk : public NonCopyMoveable {
  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. friend class Rml::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. RMLUI_ASSERT(node != nullptr);
  65. node = node->next;
  66. }
  67. /// Returns true if it is OK to deference or increment this
  68. /// iterator.
  69. explicit inline operator bool() { return (node != nullptr); }
  70. /// Returns the object referenced by the iterator's current
  71. /// node.
  72. inline PoolType& operator*() { return *reinterpret_cast<PoolType*>(node->object); }
  73. /// Returns a pointer to the object referenced by the
  74. /// iterator's current node.
  75. inline PoolType* operator->() { return reinterpret_cast<PoolType*>(node->object); }
  76. private:
  77. // Constructs an iterator referencing the given node.
  78. inline Iterator(PoolNode* node) { this->node = node; }
  79. PoolNode* node;
  80. };
  81. Pool(int chunk_size = 0, bool grow = false);
  82. ~Pool();
  83. /// Initialises the pool to a given size.
  84. void Initialise(int chunk_size, bool grow = false);
  85. /// Returns the head of the linked list of allocated objects.
  86. inline Iterator Begin();
  87. /// Attempts to allocate an object into a free slot in the memory pool and construct it using the given arguments.
  88. /// If the process is successful, the newly constructed object is returned. Otherwise, if the process fails due to
  89. /// no free objects being available, nullptr is returned.
  90. template <typename... Args>
  91. inline PoolType* AllocateAndConstruct(Args&&... args);
  92. /// Deallocates the object pointed to by the given iterator.
  93. inline void DestroyAndDeallocate(Iterator& iterator);
  94. /// Deallocates the given object.
  95. inline void DestroyAndDeallocate(PoolType* object);
  96. /// Returns the number of objects in the pool.
  97. inline int GetSize() const;
  98. /// Returns the number of object chunks in the pool.
  99. inline int GetNumChunks() const;
  100. /// Returns the number of allocated objects in the pool.
  101. inline int GetNumAllocatedObjects() const;
  102. private:
  103. // Creates a new pool chunk and appends its nodes to the beginning of the free list.
  104. void CreateChunk();
  105. int chunk_size;
  106. bool grow;
  107. PoolChunk* pool;
  108. // The heads of the two linked lists.
  109. PoolNode* first_allocated_node;
  110. PoolNode* first_free_node;
  111. int num_allocated_objects;
  112. #ifdef RMLUI_DEBUG
  113. int max_num_allocated_objects = 0;
  114. #endif
  115. };
  116. } // namespace Rml
  117. #include "Pool.inl"
  118. #endif