Blender V5.3
BLI_implicit_sharing.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 <atomic>
12
13#include "BLI_assert.hh"
14#include "BLI_utility_mixins.hh"
15
16#include "MEM_guardedalloc.h"
17
18namespace blender {
19
22
45 private:
53 mutable std::atomic<int> strong_users_ = 1;
60 mutable std::atomic<int> weak_users_ = 1;
66 mutable std::atomic<int64_t> version_ = 0;
67
68 public:
70 {
71 BLI_assert(strong_users_ == 0);
72 BLI_assert(weak_users_ == 0);
73 }
74
76 bool is_mutable() const
77 {
78 return strong_users_.load(std::memory_order_relaxed) == 1;
79 }
80
85 bool is_expired() const
86 {
87 return strong_users_.load(std::memory_order_acquire) == 0;
88 }
89
91 void add_user() const
92 {
93 BLI_assert(!this->is_expired());
94 strong_users_.fetch_add(1, std::memory_order_relaxed);
95 }
96
105 void add_weak_user() const
106 {
107 weak_users_.fetch_add(1, std::memory_order_relaxed);
108 }
109
116 {
117 BLI_assert(this->is_mutable());
118 /* This might not need an atomic increment when the #version method below is only called when
119 * the code calling it is a strong user of this sharing info. Better be safe and use an atomic
120 * for now. */
121 version_.fetch_add(1, std::memory_order_acq_rel);
122 }
123
129 {
130 return version_.load(std::memory_order_acquire);
131 }
132
133 int strong_users() const
134 {
135 return strong_users_.load(std::memory_order_acquire);
136 }
137
143 {
144 const int old_user_count = strong_users_.fetch_sub(1, std::memory_order_acq_rel);
145 BLI_assert(old_user_count >= 1);
146 const bool was_last_user = old_user_count == 1;
147 if (was_last_user) {
148 const int old_weak_user_count = weak_users_.load(std::memory_order_acquire);
149 BLI_assert(old_weak_user_count >= 1);
150 if (old_weak_user_count == 1) {
151 /* If the weak user count is 1 it means that there is no actual weak user. The 1 just
152 * indicates that there was still at least one strong user. */
153 weak_users_ = 0;
154 const_cast<ImplicitSharingInfo *>(this)->delete_self_with_data();
155 }
156 else {
157 /* There is still at least one actual weak user, so don't free the sharing info yet. The
158 * data can be freed though. */
159 const_cast<ImplicitSharingInfo *>(this)->delete_data_only();
160 /* Also remove the "fake" weak user that indicated that there was at least one strong
161 * user. */
163 }
164 }
165 }
166
172 {
173 const int old_weak_user_count = weak_users_.fetch_sub(1, std::memory_order_acq_rel);
174 BLI_assert(old_weak_user_count >= 1);
175 const bool was_last_weak_user = old_weak_user_count == 1;
176 if (was_last_weak_user) {
177 /* It's possible that the data has been freed before already, but now it is definitely freed
178 * together with the sharing info. */
179 const_cast<ImplicitSharingInfo *>(this)->delete_self_with_data();
180 }
181 }
182
183 private:
188 virtual void delete_self_with_data() = 0;
190 virtual void delete_data_only() {}
191};
192
198
199 private:
200 void delete_self_with_data() override
201 {
202 /* Can't use `delete this` here, because we don't know what allocator was used. */
203 this->delete_self();
204 }
205
206 virtual void delete_self() = 0;
207};
208
213template<typename T> class ImplicitSharedValue : public ImplicitSharingInfo {
214 public:
216
217 template<typename... Args>
218 ImplicitSharedValue(Args &&...args) : data(std::forward<Args>(args)...)
219 {
220 }
221
222 MEM_CXX_CLASS_ALLOC_FUNCS("ImplicitSharedValue");
223
224 private:
225 void delete_self_with_data() override
226 {
227 delete this;
228 }
229
230 void delete_data_only() override
231 {
232 /* Free the data when there are only weak users left, since they may keep the sharing info
233 * alive much longer. Types that can't be reset are freed together with the sharing info. */
234 if constexpr (requires { this->data = T(); }) {
235 this->data = T();
236 }
237 }
238};
239
245 const void *data = nullptr;
246};
247
249
250namespace detail {
251
252void *resize_trivial_array_impl(void *old_data,
253 int64_t old_size,
254 int64_t new_size,
255 int64_t alignment,
256 const ImplicitSharingInfo **sharing_info);
257void *make_trivial_data_mutable_impl(void *old_data,
258 int64_t size,
259 int64_t alignment,
260 const ImplicitSharingInfo **sharing_info);
261
263
264} // namespace detail
265
270template<typename T>
271void copy_shared_pointer(T *src_ptr,
272 const ImplicitSharingInfo *src_sharing_info,
273 T **r_dst_ptr,
274 const ImplicitSharingInfo **r_dst_sharing_info)
275{
276 *r_dst_ptr = src_ptr;
277 *r_dst_sharing_info = src_sharing_info;
278 if (*r_dst_ptr) {
279 BLI_assert(*r_dst_sharing_info != nullptr);
280 (*r_dst_sharing_info)->add_user();
281 }
282}
283
287template<typename T> void free_shared_data(T **data, const ImplicitSharingInfo **sharing_info)
288{
289 if (*sharing_info) {
290 BLI_assert(*data != nullptr);
291 (*sharing_info)->remove_user_and_delete_if_last();
292 }
293 *data = nullptr;
294 *sharing_info = nullptr;
295}
296
301template<typename T> const ImplicitSharingInfo *info_for_mem_free(T *data)
302{
303 static_assert(!std::is_void_v<T>);
304 static_assert(std::is_trivially_destructible_v<T>,
305 "Non-trivial types should use e.g. ImplicitSharedValue<Array<T>> instead");
306 return detail::info_for_mem_free_impl(const_cast<void *>(static_cast<const void *>(data)));
307}
308
312template<typename T>
314 const ImplicitSharingInfo **sharing_info,
315 const int64_t size)
316{
317 *data = static_cast<T *>(
318 detail::make_trivial_data_mutable_impl(*data, sizeof(T) * size, alignof(T), sharing_info));
319}
320
325template<typename T>
327 const ImplicitSharingInfo **sharing_info,
328 int64_t old_size,
329 int64_t new_size)
330{
331 *data = static_cast<T *>(detail::resize_trivial_array_impl(
332 *data, sizeof(T) * old_size, sizeof(T) * new_size, alignof(T), sharing_info));
333}
334
335} // namespace implicit_sharing
336
337} // namespace blender
Read Blender memory allocation and freeing.
MEM_CXX_CLASS_ALLOC_FUNCS("ImplicitSharedValue")
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
#define T
void * resize_trivial_array_impl(void *old_data, int64_t old_size, int64_t new_size, int64_t alignment, const ImplicitSharingInfo **sharing_info)
const ImplicitSharingInfo * info_for_mem_free_impl(void *data)
void * make_trivial_data_mutable_impl(void *old_data, int64_t size, int64_t alignment, const ImplicitSharingInfo **sharing_info)
void resize_trivial_array(T **data, const ImplicitSharingInfo **sharing_info, int64_t old_size, int64_t new_size)
void copy_shared_pointer(T *src_ptr, const ImplicitSharingInfo *src_sharing_info, T **r_dst_ptr, const ImplicitSharingInfo **r_dst_sharing_info)
void free_shared_data(T **data, const ImplicitSharingInfo **sharing_info)
const ImplicitSharingInfo * info_for_mem_free(T *data)
void make_trivial_data_mutable(T **data, const ImplicitSharingInfo **sharing_info, const int64_t size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
ImplicitSharingInfo ImplicitSharingInfoHandle
BMesh const char void * data
const ImplicitSharingInfo * sharing_info