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