paged_array.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**************************************************************************/
  2. /* paged_array.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. #ifndef PAGED_ARRAY_H
  31. #define PAGED_ARRAY_H
  32. #include "core/os/memory.h"
  33. #include "core/os/spin_lock.h"
  34. #include "core/typedefs.h"
  35. #include <type_traits>
  36. // PagedArray is used mainly for filling a very large array from multiple threads efficiently and without causing major fragmentation
  37. // PageArrayPool manages central page allocation in a thread safe matter
  38. template <class T>
  39. class PagedArrayPool {
  40. T **page_pool = nullptr;
  41. uint32_t pages_allocated = 0;
  42. uint32_t *available_page_pool = nullptr;
  43. uint32_t pages_available = 0;
  44. uint32_t page_size = 0;
  45. SpinLock spin_lock;
  46. public:
  47. uint32_t alloc_page() {
  48. spin_lock.lock();
  49. if (unlikely(pages_available == 0)) {
  50. uint32_t pages_used = pages_allocated;
  51. pages_allocated++;
  52. page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated);
  53. available_page_pool = (uint32_t *)memrealloc(available_page_pool, sizeof(uint32_t) * pages_allocated);
  54. page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size);
  55. available_page_pool[0] = pages_used;
  56. pages_available++;
  57. }
  58. pages_available--;
  59. uint32_t page = available_page_pool[pages_available];
  60. spin_lock.unlock();
  61. return page;
  62. }
  63. T *get_page(uint32_t p_page_id) {
  64. return page_pool[p_page_id];
  65. }
  66. void free_page(uint32_t p_page_id) {
  67. spin_lock.lock();
  68. available_page_pool[pages_available] = p_page_id;
  69. pages_available++;
  70. spin_lock.unlock();
  71. }
  72. uint32_t get_page_size_shift() const {
  73. return get_shift_from_power_of_2(page_size);
  74. }
  75. uint32_t get_page_size_mask() const {
  76. return page_size - 1;
  77. }
  78. void reset() {
  79. ERR_FAIL_COND(pages_available < pages_allocated);
  80. if (pages_allocated) {
  81. for (uint32_t i = 0; i < pages_allocated; i++) {
  82. memfree(page_pool[i]);
  83. }
  84. memfree(page_pool);
  85. memfree(available_page_pool);
  86. page_pool = nullptr;
  87. available_page_pool = nullptr;
  88. pages_allocated = 0;
  89. pages_available = 0;
  90. }
  91. }
  92. bool is_configured() const {
  93. return page_size > 0;
  94. }
  95. void configure(uint32_t p_page_size) {
  96. ERR_FAIL_COND(page_pool != nullptr); //sanity check
  97. ERR_FAIL_COND(p_page_size == 0);
  98. page_size = nearest_power_of_2_templated(p_page_size);
  99. }
  100. PagedArrayPool(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
  101. configure(p_page_size);
  102. }
  103. ~PagedArrayPool() {
  104. ERR_FAIL_COND_MSG(pages_available < pages_allocated, "Pages in use exist at exit in PagedArrayPool");
  105. reset();
  106. }
  107. };
  108. // PageArray is a local array that is optimized to grow in place, then be cleared often.
  109. // It does so by allocating pages from a PagedArrayPool.
  110. // It is safe to use multiple PagedArrays from different threads, sharing a single PagedArrayPool
  111. template <class T>
  112. class PagedArray {
  113. PagedArrayPool<T> *page_pool = nullptr;
  114. T **page_data = nullptr;
  115. uint32_t *page_ids = nullptr;
  116. uint32_t max_pages_used = 0;
  117. uint32_t page_size_shift = 0;
  118. uint32_t page_size_mask = 0;
  119. uint64_t count = 0;
  120. _FORCE_INLINE_ uint32_t _get_pages_in_use() const {
  121. if (count == 0) {
  122. return 0;
  123. } else {
  124. return ((count - 1) >> page_size_shift) + 1;
  125. }
  126. }
  127. void _grow_page_array() {
  128. //no more room in the page array to put the new page, make room
  129. if (max_pages_used == 0) {
  130. max_pages_used = 1;
  131. } else {
  132. max_pages_used *= 2; // increase in powers of 2 to keep allocations to minimum
  133. }
  134. page_data = (T **)memrealloc(page_data, sizeof(T *) * max_pages_used);
  135. page_ids = (uint32_t *)memrealloc(page_ids, sizeof(uint32_t) * max_pages_used);
  136. }
  137. public:
  138. _FORCE_INLINE_ const T &operator[](uint64_t p_index) const {
  139. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  140. uint32_t page = p_index >> page_size_shift;
  141. uint32_t offset = p_index & page_size_mask;
  142. return page_data[page][offset];
  143. }
  144. _FORCE_INLINE_ T &operator[](uint64_t p_index) {
  145. CRASH_BAD_UNSIGNED_INDEX(p_index, count);
  146. uint32_t page = p_index >> page_size_shift;
  147. uint32_t offset = p_index & page_size_mask;
  148. return page_data[page][offset];
  149. }
  150. _FORCE_INLINE_ void push_back(const T &p_value) {
  151. uint32_t remainder = count & page_size_mask;
  152. if (unlikely(remainder == 0)) {
  153. // at 0, so time to request a new page
  154. uint32_t page_count = _get_pages_in_use();
  155. uint32_t new_page_count = page_count + 1;
  156. if (unlikely(new_page_count > max_pages_used)) {
  157. ERR_FAIL_COND(page_pool == nullptr); //sanity check
  158. _grow_page_array(); //keep out of inline
  159. }
  160. uint32_t page_id = page_pool->alloc_page();
  161. page_data[page_count] = page_pool->get_page(page_id);
  162. page_ids[page_count] = page_id;
  163. }
  164. // place the new value
  165. uint32_t page = count >> page_size_shift;
  166. uint32_t offset = count & page_size_mask;
  167. if (!std::is_trivially_constructible<T>::value) {
  168. memnew_placement(&page_data[page][offset], T(p_value));
  169. } else {
  170. page_data[page][offset] = p_value;
  171. }
  172. count++;
  173. }
  174. _FORCE_INLINE_ void pop_back() {
  175. ERR_FAIL_COND(count == 0);
  176. if (!std::is_trivially_destructible<T>::value) {
  177. uint32_t page = (count - 1) >> page_size_shift;
  178. uint32_t offset = (count - 1) & page_size_mask;
  179. page_data[page][offset].~T();
  180. }
  181. uint32_t remainder = count & page_size_mask;
  182. if (unlikely(remainder == 1)) {
  183. // one element remained, so page must be freed.
  184. uint32_t last_page = _get_pages_in_use() - 1;
  185. page_pool->free_page(page_ids[last_page]);
  186. }
  187. count--;
  188. }
  189. void clear() {
  190. //destruct if needed
  191. if (!std::is_trivially_destructible<T>::value) {
  192. for (uint64_t i = 0; i < count; i++) {
  193. uint32_t page = i >> page_size_shift;
  194. uint32_t offset = i & page_size_mask;
  195. page_data[page][offset].~T();
  196. }
  197. }
  198. //return the pages to the pagepool, so they can be used by another array eventually
  199. uint32_t pages_used = _get_pages_in_use();
  200. for (uint32_t i = 0; i < pages_used; i++) {
  201. page_pool->free_page(page_ids[i]);
  202. }
  203. count = 0;
  204. //note we leave page_data and page_indices intact for next use. If you really want to clear them call reset()
  205. }
  206. void reset() {
  207. clear();
  208. if (page_data) {
  209. memfree(page_data);
  210. memfree(page_ids);
  211. page_data = nullptr;
  212. page_ids = nullptr;
  213. max_pages_used = 0;
  214. }
  215. }
  216. // This takes the pages from a source array and merges them to this one
  217. // resulting order is undefined, but content is merged very efficiently,
  218. // making it ideal to fill content on several threads to later join it.
  219. void merge_unordered(PagedArray<T> &p_array) {
  220. ERR_FAIL_COND(page_pool != p_array.page_pool);
  221. uint32_t remainder = count & page_size_mask;
  222. T *remainder_page = nullptr;
  223. uint32_t remainder_page_id = 0;
  224. if (remainder > 0) {
  225. uint32_t last_page = _get_pages_in_use() - 1;
  226. remainder_page = page_data[last_page];
  227. remainder_page_id = page_ids[last_page];
  228. }
  229. count -= remainder;
  230. uint32_t src_page_index = 0;
  231. uint32_t page_size = page_size_mask + 1;
  232. while (p_array.count > 0) {
  233. uint32_t page_count = _get_pages_in_use();
  234. uint32_t new_page_count = page_count + 1;
  235. if (unlikely(new_page_count > max_pages_used)) {
  236. _grow_page_array(); //keep out of inline
  237. }
  238. page_data[page_count] = p_array.page_data[src_page_index];
  239. page_ids[page_count] = p_array.page_ids[src_page_index];
  240. uint32_t take = MIN(p_array.count, page_size); //pages to take away
  241. p_array.count -= take;
  242. count += take;
  243. src_page_index++;
  244. }
  245. //handle the remainder page if exists
  246. if (remainder_page) {
  247. uint32_t new_remainder = count & page_size_mask;
  248. if (new_remainder > 0) {
  249. //must merge old remainder with new remainder
  250. T *dst_page = page_data[_get_pages_in_use() - 1];
  251. uint32_t to_copy = MIN(page_size - new_remainder, remainder);
  252. for (uint32_t i = 0; i < to_copy; i++) {
  253. if (!std::is_trivially_constructible<T>::value) {
  254. memnew_placement(&dst_page[i + new_remainder], T(remainder_page[i + remainder - to_copy]));
  255. } else {
  256. dst_page[i + new_remainder] = remainder_page[i + remainder - to_copy];
  257. }
  258. if (!std::is_trivially_destructible<T>::value) {
  259. remainder_page[i + remainder - to_copy].~T();
  260. }
  261. }
  262. remainder -= to_copy; //subtract what was copied from remainder
  263. count += to_copy; //add what was copied to the count
  264. if (remainder == 0) {
  265. //entire remainder copied, let go of remainder page
  266. page_pool->free_page(remainder_page_id);
  267. remainder_page = nullptr;
  268. }
  269. }
  270. if (remainder > 0) {
  271. //there is still remainder, append it
  272. uint32_t page_count = _get_pages_in_use();
  273. uint32_t new_page_count = page_count + 1;
  274. if (unlikely(new_page_count > max_pages_used)) {
  275. _grow_page_array(); //keep out of inline
  276. }
  277. page_data[page_count] = remainder_page;
  278. page_ids[page_count] = remainder_page_id;
  279. count += remainder;
  280. }
  281. }
  282. }
  283. _FORCE_INLINE_ uint64_t size() const {
  284. return count;
  285. }
  286. void set_page_pool(PagedArrayPool<T> *p_page_pool) {
  287. ERR_FAIL_COND(max_pages_used > 0); //sanity check
  288. page_pool = p_page_pool;
  289. page_size_mask = page_pool->get_page_size_mask();
  290. page_size_shift = page_pool->get_page_size_shift();
  291. }
  292. ~PagedArray() {
  293. reset();
  294. }
  295. };
  296. #endif // PAGED_ARRAY_H