paged_allocator.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* paged_allocator.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef PAGED_ALLOCATOR_H
  31. #define PAGED_ALLOCATOR_H
  32. #include "core/os/memory.h"
  33. #include "core/os/spin_lock.h"
  34. #include "core/typedefs.h"
  35. #include <type_traits>
  36. template <class T, bool thread_safe = false>
  37. class PagedAllocator {
  38. T **page_pool = nullptr;
  39. T ***available_pool = nullptr;
  40. uint32_t pages_allocated = 0;
  41. uint32_t allocs_available = 0;
  42. uint32_t page_shift = 0;
  43. uint32_t page_mask = 0;
  44. uint32_t page_size = 0;
  45. SpinLock spin_lock;
  46. public:
  47. template <class... Args>
  48. T *alloc(const Args &&...p_args) {
  49. if (thread_safe) {
  50. spin_lock.lock();
  51. }
  52. if (unlikely(allocs_available == 0)) {
  53. uint32_t pages_used = pages_allocated;
  54. pages_allocated++;
  55. page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated);
  56. available_pool = (T ***)memrealloc(available_pool, sizeof(T **) * pages_allocated);
  57. page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size);
  58. available_pool[pages_used] = (T **)memalloc(sizeof(T *) * page_size);
  59. for (uint32_t i = 0; i < page_size; i++) {
  60. available_pool[0][i] = &page_pool[pages_used][i];
  61. }
  62. allocs_available += page_size;
  63. }
  64. allocs_available--;
  65. T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask];
  66. if (thread_safe) {
  67. spin_lock.unlock();
  68. }
  69. memnew_placement(alloc, T(p_args...));
  70. return alloc;
  71. }
  72. void free(T *p_mem) {
  73. if (thread_safe) {
  74. spin_lock.lock();
  75. }
  76. p_mem->~T();
  77. available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem;
  78. if (thread_safe) {
  79. spin_lock.unlock();
  80. }
  81. allocs_available++;
  82. }
  83. void reset(bool p_allow_unfreed = false) {
  84. if (!p_allow_unfreed || !std::is_trivially_destructible<T>::value) {
  85. ERR_FAIL_COND(allocs_available < pages_allocated * page_size);
  86. }
  87. if (pages_allocated) {
  88. for (uint32_t i = 0; i < pages_allocated; i++) {
  89. memfree(page_pool[i]);
  90. memfree(available_pool[i]);
  91. }
  92. memfree(page_pool);
  93. memfree(available_pool);
  94. page_pool = nullptr;
  95. available_pool = nullptr;
  96. pages_allocated = 0;
  97. allocs_available = 0;
  98. }
  99. }
  100. bool is_configured() const {
  101. return page_size > 0;
  102. }
  103. void configure(uint32_t p_page_size) {
  104. ERR_FAIL_COND(page_pool != nullptr); //sanity check
  105. ERR_FAIL_COND(p_page_size == 0);
  106. page_size = nearest_power_of_2_templated(p_page_size);
  107. page_mask = page_size - 1;
  108. page_shift = get_shift_from_power_of_2(page_size);
  109. }
  110. PagedAllocator(uint32_t p_page_size = 4096) { // power of 2 recommended because of alignment with OS page sizes. Even if element is bigger, its still a multiple and get rounded amount of pages
  111. configure(p_page_size);
  112. }
  113. ~PagedAllocator() {
  114. ERR_FAIL_COND_MSG(allocs_available < pages_allocated * page_size, "Pages in use exist at exit in PagedAllocator");
  115. reset();
  116. }
  117. };
  118. #endif // PAGED_ALLOCATOR_H