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