Debian Codegraph @

core/templates/paged_allocator.h

raw symbols SCIP
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
31 #pragma once
32
33 #include "core/core_globals.h"
34 #include "core/os/memory.h"
35 #include "core/os/spin_lock.h"
36 #include "core/string/ustring.h"
37 #include "core/typedefs.h"
38
39 #include <type_traits>
40 #include <typeinfo> // IWYU pragma: keep // Used in macro.
41
42 template <typename T, bool thread_safe = false, uint32_t DEFAULT_PAGE_SIZE = 4096>
43 class PagedAllocator {
44 T **page_pool = nullptr;
45 T ***available_pool = nullptr;
46 uint32_t pages_allocated = 0;
47 uint32_t allocs_available = 0;
48
49 uint32_t page_shift = 0;
50 uint32_t page_mask = 0;
51 uint32_t page_size = 0;
52 SpinLock spin_lock;
53
54 public:
55 template <typename... Args>
56 T *alloc(Args &&...p_args) {
57 if constexpr (thread_safe) {
58 spin_lock.lock();
59 }
60 if (unlikely(allocs_available == 0)) {
61 uint32_t pages_used = pages_allocated;
62
63 pages_allocated++;
64 page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated);
65 available_pool = (T ***)memrealloc(available_pool, sizeof(T **) * pages_allocated);
66
67 page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size);
68 available_pool[pages_used] = (T **)memalloc(sizeof(T *) * page_size);
69
70 for (uint32_t i = 0; i < page_size; i++) {
71 available_pool[0][i] = &page_pool[pages_used][i];
72 }
73 allocs_available += page_size;
74 }
75
76 allocs_available--;
77 T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask];
78 if constexpr (thread_safe) {
79 spin_lock.unlock();
80 }
81 memnew_placement(alloc, T(p_args...));
82 return alloc;
83 }
84
85 void free(T *p_mem) {
86 if constexpr (thread_safe) {
87 spin_lock.lock();
88 }
89 p_mem->~T();
90 available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem;
91 allocs_available++;
92 if constexpr (thread_safe) {
93 spin_lock.unlock();
94 }
95 }
96
97 template <typename... Args>
98 T *new_allocation(Args &&...p_args) { return alloc(p_args...); }
99 void delete_allocation(T *p_mem) { free(p_mem); }
100
101 private:
102 void _reset(bool p_allow_unfreed) {
103 if (!p_allow_unfreed || !std::is_trivially_destructible_v<T>) {
104 ERR_FAIL_COND(allocs_available < pages_allocated * page_size);
105 }
106 if (pages_allocated) {
107 for (uint32_t i = 0; i < pages_allocated; i++) {
108 memfree(page_pool[i]);
109 memfree(available_pool[i]);
110 }
111 memfree(page_pool);
112 memfree(available_pool);
113 page_pool = nullptr;
114 available_pool = nullptr;
115 pages_allocated = 0;
116 allocs_available = 0;
117 }
118 }
119
120 public:
121 void reset(bool p_allow_unfreed = false) {
122 if constexpr (thread_safe) {
123 spin_lock.lock();
124 }
125 _reset(p_allow_unfreed);
126 if constexpr (thread_safe) {
127 spin_lock.unlock();
128 }
129 }
130
131 bool is_configured() const {
132 if constexpr (thread_safe) {
133 spin_lock.lock();
134 }
135 bool result = page_size > 0;
136 if constexpr (thread_safe) {
137 spin_lock.unlock();
138 }
139 return result;
140 }
141
142 void configure(uint32_t p_page_size) {
143 if constexpr (thread_safe) {
144 spin_lock.lock();
145 }
146 ERR_FAIL_COND(page_pool != nullptr); // Safety check.
147 ERR_FAIL_COND(p_page_size == 0);
148 page_size = nearest_power_of_2_templated(p_page_size);
149 page_mask = page_size - 1;
150 page_shift = get_shift_from_power_of_2(page_size);
151 if constexpr (thread_safe) {
152 spin_lock.unlock();
153 }
154 }
155
156 // Power of 2 recommended because of alignment with OS page sizes.
157 // Even if element is bigger, it's still a multiple and gets rounded to amount of pages.
158 PagedAllocator(uint32_t p_page_size = DEFAULT_PAGE_SIZE) {
159 configure(p_page_size);
160 }
161
162 ~PagedAllocator() {
163 if constexpr (thread_safe) {
164 spin_lock.lock();
165 }
166 bool leaked = allocs_available < pages_allocated * page_size;
167 if (leaked) {
168 if (CoreGlobals::leak_reporting_enabled) {
169 ERR_PRINT(String("Pages in use exist at exit in PagedAllocator: ") + String(typeid(T).name()));
170 }
171 } else {
172 _reset(false);
173 }
174 if constexpr (thread_safe) {
175 spin_lock.unlock();
176 }
177 }
178 };