Blender V5.3
BLI_resource_scope.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
13#include "BLI_utility_mixins.hh"
14
15namespace blender {
16
38 private:
39 struct ResourceData {
40 void *data;
41 void (*free)(void *data);
42 };
44
46 ResourceDataList resources_;
52 LinearAllocator<> &allocator_;
53
54 public:
58 explicit ResourceScope(int64_t initial_size = 32);
59
64 template<size_t Size, size_t Alignment>
66
71 ResourceScope(void *buffer, int64_t size);
72
77
79
84 template<typename T> T *add(std::unique_ptr<T> resource);
85
90 template<typename T> T *add(std::shared_ptr<T> resource);
91
96 template<typename T> T *add(destruct_ptr<T> resource);
97
102 void add(void *userdata, void (*free)(void *));
103
108 template<typename T> T &add_value(T &&value);
109
113 template<typename Func> void add_destruct_call(Func func);
114
118 template<typename T, typename... Args> T &construct(Args &&...args);
119
124 void *allocate_owned(const CPPType &type);
125
130
131 private:
132 static LinearAllocator<> &create_own_allocator(ResourceDataList &r_resources,
133 int64_t initial_size);
134 static LinearAllocator<> &create_own_allocator_in_buffer(ResourceDataList &r_resources,
135 void *data,
136 int64_t size,
137 bool free_on_destruct);
138};
139
140/* This tests the expected size of #ResourceScope. It's currently easy to break by e.g. inheriting
141 * from #NonCopyable. That happens because then there would be two #NonCopyable types at the same
142 * address which is not allowed. The proper solution is probably to change how NonCopyable and
143 * NonMovable are used overall. */
144static_assert(sizeof(ResourceScope) == 16);
145
146/* -------------------------------------------------------------------- */
149
151
152template<size_t Size, size_t Alignment>
154 : ResourceScope(buffer.ptr(), Size)
155{
156}
157
158template<typename T> inline T *ResourceScope::add(std::unique_ptr<T> resource)
159{
160 T *ptr = resource.release();
161 if (ptr == nullptr) {
162 return nullptr;
163 }
164 this->add(ptr, [](void *data) {
165 T *typed_data = reinterpret_cast<T *>(data);
166 delete typed_data;
167 });
168 return ptr;
169}
170
171template<typename T> inline T *ResourceScope::add(std::shared_ptr<T> resource)
172{
173 if (!resource) {
174 return nullptr;
175 }
176 T *ptr = resource.get();
177 this->add_destruct_call([resource = std::move(resource)]() mutable { resource.reset(); });
178 return ptr;
179}
180
181template<typename T> inline T *ResourceScope::add(destruct_ptr<T> resource)
182{
183 T *ptr = resource.release();
184 if (ptr == nullptr) {
185 return nullptr;
186 }
187 /* There is no need to keep track of such types. */
188 if constexpr (std::is_trivially_destructible_v<T>) {
189 return ptr;
190 }
191
192 this->add(ptr, [](void *data) {
193 T *typed_data = reinterpret_cast<T *>(data);
194 typed_data->~T();
195 });
196 return ptr;
197}
198
199inline void ResourceScope::add(void *userdata, void (*free)(void *))
200{
201 ResourceData data;
202 data.data = userdata;
203 data.free = free;
204 resources_.append(allocator_, data);
205}
206
207template<typename T> inline T &ResourceScope::add_value(T &&value)
208{
209 return this->construct<T>(std::forward<T>(value));
210}
211
212template<typename Func> inline void ResourceScope::add_destruct_call(Func func)
213{
214 void *buffer = allocator_.allocate(sizeof(Func), alignof(Func));
215 new (buffer) Func(std::move(func));
216 this->add(buffer, [](void *data) { (*static_cast<Func *>(data))(); });
217}
218
219template<typename T, typename... Args> inline T &ResourceScope::construct(Args &&...args)
220{
221 destruct_ptr<T> value_ptr = allocator_.construct<T>(std::forward<Args>(args)...);
222 T &value_ref = *value_ptr;
223 this->add(std::move(value_ptr));
224 return value_ref;
225}
226
227inline void *ResourceScope::allocate_owned(const CPPType &type)
228{
229 void *buffer = allocator_.allocate(type);
230 if (!type.is_trivially_destructible) {
231 this->add_destruct_call([&type, buffer]() { type.destruct(buffer); });
232 }
233 return buffer;
234}
235
237{
238 return allocator_;
239}
240
242
243} // namespace blender
void destruct(void *ptr) const
T & construct(Args &&...args)
void add_destruct_call(Func func)
void * allocate_owned(const CPPType &type)
LinearAllocator & allocator()
T * add(std::unique_ptr< T > resource)
ResourceScope(int64_t initial_size=32)
NonMovable(NonMovable &&other)=delete
#define T
void free(Subdiv *subdiv)
Definition subdiv.cc:192
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
BMesh const char void * data