Blender V5.3
BLI_memory_utils.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
11#include <algorithm>
12#include <memory>
13#include <type_traits>
14
15#include "BLI_utildefines.hh"
16
17namespace blender {
18
23template<typename T> inline constexpr bool is_trivial_extended_v = std::is_trivial_v<T>;
24template<typename T>
26 std::is_trivially_destructible_v<T>;
27template<typename T>
29 is_trivial_extended_v<T> || std::is_trivially_copy_constructible_v<T>;
30template<typename T>
32 is_trivial_extended_v<T> || std::is_trivially_move_constructible_v<T>;
33
34template<typename T> void destruct_n(T *ptr, int64_t n)
35{
37 return;
38 }
39
40 std::destroy_n(ptr, n);
41}
42
43template<typename T> void default_construct_n(T *ptr, int64_t n)
44{
45 std::uninitialized_default_construct_n(ptr, n);
46}
47
48template<typename T> void initialized_copy_n(const T *src, int64_t n, T *dst)
49{
50 std::copy_n(src, n, dst);
51}
52
53template<typename T> void uninitialized_copy_n(const T *src, int64_t n, T *dst)
54{
55 std::uninitialized_copy_n(src, n, dst);
56}
57
58template<typename From, typename To>
59void uninitialized_convert_n(const From *src, int64_t n, To *dst)
60{
61 std::uninitialized_copy_n(src, n, dst);
62}
63
64template<typename T> void initialized_move_n(T *src, int64_t n, T *dst)
65{
66 std::copy_n(std::make_move_iterator(src), n, dst);
67}
68
69template<typename T> void uninitialized_move_n(T *src, int64_t n, T *dst)
70{
71 std::uninitialized_copy_n(std::make_move_iterator(src), n, dst);
72}
73
74template<typename T> void initialized_relocate_n(T *src, int64_t n, T *dst)
75{
76 initialized_move_n(src, n, dst);
77 destruct_n(src, n);
78}
79
80template<typename T> void uninitialized_relocate_n(T *src, int64_t n, T *dst)
81{
82 uninitialized_move_n(src, n, dst);
83 destruct_n(src, n);
84}
85
86template<typename T> void initialized_fill_n(T *dst, int64_t n, const T &value)
87{
88 std::fill_n(dst, n, value);
89}
90
91template<typename T> void uninitialized_fill_n(T *dst, int64_t n, const T &value)
92{
93 std::uninitialized_fill_n(dst, n, value);
94}
95
96template<typename T> struct DestructValueAtAddress {
98
99 template<typename U> DestructValueAtAddress(const U &) {}
100
101 void operator()(T *ptr)
102 {
103 ptr->~T();
104 }
105};
106
111template<typename T> using destruct_ptr = std::unique_ptr<T, DestructValueAtAddress<T>>;
112
117template<size_t Size, size_t Alignment> class AlignedBuffer {
118 struct Empty {};
119 struct alignas(Alignment) Sized {
120 /* Don't create an empty array. This causes problems with some compilers. */
121 std::byte buffer_[Size > 0 ? Size : 1];
122 };
123
124 using BufferType = std::conditional_t<Size == 0, Empty, Sized>;
125 BLI_NO_UNIQUE_ADDRESS BufferType buffer_;
126
127 public:
128 operator void *()
129 {
130 return this;
131 }
132
133 operator const void *() const
134 {
135 return this;
136 }
137
138 void *ptr()
139 {
140 return this;
141 }
142
143 const void *ptr() const
144 {
145 return this;
146 }
147};
148
154template<typename T, int64_t Size = 1> class TypedBuffer {
155 private:
157 static constexpr size_t get_size()
158 {
159 if constexpr (Size == 0) {
160 return 0;
161 }
162 else {
163 return sizeof(T) * size_t(Size);
164 }
165 }
166
168 static constexpr size_t get_alignment()
169 {
170 if constexpr (Size == 0) {
171 return 1;
172 }
173 else {
174 return alignof(T);
175 }
176 }
177
178 BLI_NO_UNIQUE_ADDRESS AlignedBuffer<get_size(), get_alignment()> buffer_;
179
180 public:
181 operator T *()
182 {
183 return static_cast<T *>(buffer_.ptr());
184 }
185
186 operator const T *() const
187 {
188 return static_cast<const T *>(buffer_.ptr());
189 }
190
192 {
193 return *static_cast<T *>(buffer_.ptr());
194 }
195
196 const T &operator*() const
197 {
198 return *static_cast<const T *>(buffer_.ptr());
199 }
200
202 {
203 return static_cast<T *>(buffer_.ptr());
204 }
205
206 const T *ptr() const
207 {
208 return static_cast<const T *>(buffer_.ptr());
209 }
210
212 {
213 return *static_cast<T *>(buffer_.ptr());
214 }
215
216 const T &ref() const
217 {
218 return *static_cast<const T *>(buffer_.ptr());
219 }
220};
221
227
235
241template<typename From, typename To>
242inline constexpr bool is_convertible_pointer_v = std::is_convertible_v<From, To> &&
243 std::is_pointer_v<From> && std::is_pointer_v<To>;
244
251template<typename From, typename To>
252inline constexpr bool is_span_convertible_pointer_v =
253 /* Make sure we are working with pointers. */
254 std::is_pointer_v<From> && std::is_pointer_v<To> &&
255 (/* No casting is necessary when both types are the same. */
256 std::is_same_v<From, To> ||
257 /* Allow adding const to the underlying type. */
258 std::is_same_v<std::remove_pointer_t<From>, std::remove_const_t<std::remove_pointer_t<To>>> ||
259 /* Allow casting non-const pointers to void pointers. */
260 (!std::is_const_v<std::remove_pointer_t<From>> && std::is_same_v<To, void *>) ||
261 /* Allow casting any pointer to const void pointers. */
262 std::is_same_v<To, const void *>);
263
267template<typename T, typename... Args>
268inline constexpr bool is_same_any_v = (std::is_same_v<T, Args> || ...);
269
274constexpr int64_t default_inline_buffer_capacity(size_t element_size)
275{
276 return (int64_t(element_size) < 100) ? 4 : 0;
277}
278
284template<typename Container> Container &copy_assign_container(Container &dst, const Container &src)
285{
286 if (&src == &dst) {
287 return dst;
288 }
289
290 Container container_copy{src};
291 dst = std::move(container_copy);
292 return dst;
293}
294
300template<typename Container>
301Container &move_assign_container(Container &dst, Container &&src) noexcept(
302 std::is_nothrow_move_constructible_v<Container>)
303{
304 if (&dst == &src) {
305 return dst;
306 }
307
308 dst.~Container();
309 if constexpr (std::is_nothrow_move_constructible_v<Container>) {
310 new (&dst) Container(std::move(src));
311 }
312 else {
313 try {
314 new (&dst) Container(std::move(src));
315 }
316 catch (...) {
317 new (&dst) Container(NoExceptConstructor());
318 throw;
319 }
320 }
321 return dst;
322}
323
327template<typename T> inline bool assign_if_different(T &old_value, T &&new_value)
328{
329 if (old_value != new_value) {
330 old_value = std::move(new_value);
331 return true;
332 }
333 return false;
334}
335template<typename T> inline bool assign_if_different(T &old_value, const T &new_value)
336{
337 if (old_value != new_value) {
338 old_value = new_value;
339 return true;
340 }
341 return false;
342}
343
344namespace blenlib_detail {
345
346template<typename Func> struct ScopedDeferHelper {
347 Func func;
348
350 {
351 func();
352 }
353};
354
355} // namespace blenlib_detail
356
357#define BLI_SCOPED_DEFER_NAME1(a, b) a##b
358#define BLI_SCOPED_DEFER_NAME2(a, b) BLI_SCOPED_DEFER_NAME1(a, b)
359#define BLI_SCOPED_DEFER_NAME(a) BLI_SCOPED_DEFER_NAME2(_scoped_defer_##a##_, __LINE__)
360
367#define BLI_SCOPED_DEFER(function_to_defer) \
368 auto BLI_SCOPED_DEFER_NAME(func) = (function_to_defer); \
369 blenlib_detail::ScopedDeferHelper<decltype(BLI_SCOPED_DEFER_NAME(func))> BLI_SCOPED_DEFER_NAME( \
370 helper){std::move(BLI_SCOPED_DEFER_NAME(func))};
371
372} // namespace blender
#define BLI_NO_UNIQUE_ADDRESS
const void * ptr() const
const T & ref() const
const T & operator*() const
const T * ptr() const
#define T
BMesh Iterators.
UserDef U
Definition blender.cc:58
__int64 int64_t
Definition BKE_text.h:90
constexpr bool is_same_any_v
void default_construct_n(T *ptr, int64_t n)
bool assign_if_different(T &old_value, T &&new_value)
void initialized_relocate_n(T *src, int64_t n, T *dst)
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
void initialized_fill_n(T *dst, int64_t n, const T &value)
void initialized_move_n(T *src, int64_t n, T *dst)
Container & copy_assign_container(Container &dst, const Container &src)
constexpr bool is_convertible_pointer_v
constexpr bool is_trivially_move_constructible_extended_v
Container & move_assign_container(Container &dst, Container &&src) noexcept(std::is_nothrow_move_constructible_v< Container >)
constexpr int64_t default_inline_buffer_capacity(size_t element_size)
constexpr bool is_trivially_destructible_extended_v
void uninitialized_fill_n(T *dst, int64_t n, const T &value)
void uninitialized_relocate_n(T *src, int64_t n, T *dst)
void initialized_copy_n(const T *src, int64_t n, T *dst)
void uninitialized_convert_n(const From *src, int64_t n, To *dst)
constexpr bool is_trivially_copy_constructible_extended_v
void destruct_n(T *ptr, int64_t n)
constexpr bool is_span_convertible_pointer_v
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
constexpr bool is_trivial_extended_v
void uninitialized_move_n(T *src, int64_t n, T *dst)