paged_allocator.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************/
  2. /* paged_allocator.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/core_globals.h"
  32. #include "core/os/memory.h"
  33. #include "core/os/spin_lock.h"
  34. #include "core/string/ustring.h"
  35. #include "core/typedefs.h"
  36. #include <type_traits>
  37. #include <typeinfo> // IWYU pragma: keep // Used in macro.
  38. template <typename T, bool thread_safe = false, uint32_t DEFAULT_PAGE_SIZE = 4096>
  39. class PagedAllocator {
  40. T **page_pool = nullptr;
  41. T ***available_pool = nullptr;
  42. uint32_t pages_allocated = 0;
  43. uint32_t allocs_available = 0;
  44. uint32_t page_shift = 0;
  45. uint32_t page_mask = 0;
  46. uint32_t page_size = 0;
  47. SpinLock spin_lock;
  48. public:
  49. template <typename... Args>
  50. T *alloc(Args &&...p_args) {
  51. if constexpr (thread_safe) {
  52. spin_lock.lock();
  53. }
  54. if (unlikely(allocs_available == 0)) {
  55. uint32_t pages_used = pages_allocated;
  56. pages_allocated++;
  57. page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated);
  58. available_pool = (T ***)memrealloc(available_pool, sizeof(T **) * pages_allocated);
  59. page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size);
  60. available_pool[pages_used] = (T **)memalloc(sizeof(T *) * page_size);
  61. for (uint32_t i = 0; i < page_size; i++) {
  62. available_pool[0][i] = &page_pool[pages_used][i];
  63. }
  64. allocs_available += page_size;
  65. }
  66. allocs_available--;
  67. T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask];
  68. if constexpr (thread_safe) {
  69. spin_lock.unlock();
  70. }
  71. memnew_placement(alloc, T(p_args...));
  72. return alloc;
  73. }
  74. void free(T *p_mem) {
  75. if constexpr (thread_safe) {
  76. spin_lock.lock();
  77. }
  78. p_mem->~T();
  79. available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem;
  80. allocs_available++;
  81. if constexpr (thread_safe) {
  82. spin_lock.unlock();
  83. }
  84. }
  85. template <typename... Args>
  86. T *new_allocation(Args &&...p_args) { return alloc(p_args...); }
  87. void delete_allocation(T *p_mem) { free(p_mem); }
  88. private:
  89. void _reset(bool p_allow_unfreed) {
  90. if (!p_allow_unfreed || !std::is_trivially_destructible_v<T>) {
  91. ERR_FAIL_COND(allocs_available < pages_allocated * page_size);
  92. }
  93. if (pages_allocated) {
  94. for (uint32_t i = 0; i < pages_allocated; i++) {
  95. memfree(page_pool[i]);
  96. memfree(available_pool[i]);
  97. }
  98. memfree(page_pool);
  99. memfree(available_pool);
  100. page_pool = nullptr;
  101. available_pool = nullptr;
  102. pages_allocated = 0;
  103. allocs_available = 0;
  104. }
  105. }
  106. public:
  107. void reset(bool p_allow_unfreed = false) {
  108. if constexpr (thread_safe) {
  109. spin_lock.lock();
  110. }
  111. _reset(p_allow_unfreed);
  112. if constexpr (thread_safe) {
  113. spin_lock.unlock();
  114. }
  115. }
  116. bool is_configured() const {
  117. if constexpr (thread_safe) {
  118. spin_lock.lock();
  119. }
  120. bool result = page_size > 0;
  121. if constexpr (thread_safe) {
  122. spin_lock.unlock();
  123. }
  124. return result;
  125. }
  126. void configure(uint32_t p_page_size) {
  127. if constexpr (thread_safe) {
  128. spin_lock.lock();
  129. }
  130. ERR_FAIL_COND(page_pool != nullptr); // Safety check.
  131. ERR_FAIL_COND(p_page_size == 0);
  132. page_size = nearest_power_of_2_templated(p_page_size);
  133. page_mask = page_size - 1;
  134. page_shift = get_shift_from_power_of_2(page_size);
  135. if constexpr (thread_safe) {
  136. spin_lock.unlock();
  137. }
  138. }
  139. // Power of 2 recommended because of alignment with OS page sizes.
  140. // Even if element is bigger, it's still a multiple and gets rounded to amount of pages.
  141. PagedAllocator(uint32_t p_page_size = DEFAULT_PAGE_SIZE) {
  142. configure(p_page_size);
  143. }
  144. ~PagedAllocator() {
  145. if constexpr (thread_safe) {
  146. spin_lock.lock();
  147. }
  148. bool leaked = allocs_available < pages_allocated * page_size;
  149. if (leaked) {
  150. if (CoreGlobals::leak_reporting_enabled) {
  151. ERR_PRINT(String("Pages in use exist at exit in PagedAllocator: ") + String(typeid(T).name()));
  152. }
  153. } else {
  154. _reset(false);
  155. }
  156. if constexpr (thread_safe) {
  157. spin_lock.unlock();
  158. }
  159. }
  160. };