Blender V5.3
geometry_nodes_list.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_memory_counter.hh"
11
14
15namespace blender::nodes {
16
19{
20 auto *sharing_info = new ImplicitSharedValue<GArray<>>(std::move(array));
22 data.data = sharing_info->data.data();
23 data.sharing_info = ImplicitSharingPtr<>(sharing_info);
24 return data;
25}
26
28{
29 const CPPType &type = *value.type();
30 const void *value_ptr = value.get();
31
32 /* Prefer `calloc` to zeroing after allocation since it is faster. */
33 if (memory_is_zero(value_ptr, type.size)) {
34 void *buffer = MEM_new_array_zeroed_aligned(size, type.size, type.alignment, __func__);
35 return array_data_from_garray(GArray<>(type, buffer, size));
36 }
38 type.fill_construct_n(value_ptr, array.data(), size);
39 return array_data_from_garray(std::move(array));
40}
41
46
51
56
58{
59 const CPPType &type = *value.type();
60 /* Use an array with a single element to store the value. */
63 auto *sharing_info = new ImplicitSharedValue<GArray<>>(std::move(array));
65 data.value = sharing_info->data.data();
67 return data;
68}
69
74
76{
77 MEM_delete(this);
78}
79
81{
82 return GList::create(cpp_type_, data_, size_);
83}
84
86{
87 if (const auto *array_data = std::get_if<ArrayData>(&data_)) {
88 return GVArray::from_span(GSpan(cpp_type_, array_data->data, size_));
89 }
90 if (const auto *single_data = std::get_if<SingleData>(&data_)) {
91 return GVArray::from_single_ref(cpp_type_, size_, single_data->value);
92 }
94 return {};
95}
96
98{
99 if (const auto *array_data = std::get_if<ArrayData>(&data_)) {
100 array_data->count_memory(memory, cpp_type_, size_);
101 return;
102 }
103 if (const auto *single_data = std::get_if<SingleData>(&data_)) {
104 single_data->count_memory(memory, cpp_type_);
105 return;
106 }
107}
108
110{
111 if (cpp_type_.is<BundlePtr>()) {
112 this->typed<BundlePtr>().foreach_for_write([](BundlePtr &bundle_ptr) {
113 if (!bundle_ptr) {
114 return;
115 }
116 bundle_ptr.ensure_mutable_inplace();
117 const_cast<Bundle &>(*bundle_ptr).ensure_owns_direct_data();
118 });
119 }
120 else if (cpp_type_.is<bke::SocketValueVariant>()) {
121 this->typed<bke::SocketValueVariant>().foreach_for_write(
122 [](bke::SocketValueVariant &value) { value.ensure_owns_direct_data(); });
123 }
124 else if (cpp_type_.is<bke::GeometrySet>()) {
125 this->typed<bke::GeometrySet>().foreach_for_write(
126 [](bke::GeometrySet &geometry) { geometry.ensure_owns_direct_data(); });
127 }
128}
129
131{
132 const std::variant<GSpan, GPointer> &values = this->values();
133 if (cpp_type_.is<BundlePtr>()) {
134 /* Null bundles don't reference any data, so they are trivially owned. */
135 return std::visit(
136 []<typename T>(const T &value) {
137 if constexpr (std::is_same_v<T, GSpan>) {
138 const Span span = value.template typed<BundlePtr>();
139 return std::all_of(span.begin(), span.end(), [](const BundlePtr &bundle_ptr) {
140 if (!bundle_ptr) {
141 return true;
142 }
143 return bundle_ptr->owns_direct_data();
144 });
145 }
146 else if constexpr (std::is_same_v<T, GPointer>) {
147 const BundlePtr *value_ptr = value.template get<BundlePtr>();
148 if (!value_ptr || !*value_ptr) {
149 return true;
150 }
151 return (*value_ptr)->owns_direct_data();
152 }
153 else {
155 }
156 },
157 values);
158 }
159 if (cpp_type_.is<bke::SocketValueVariant>()) {
160 return std::visit(
161 []<typename T>(const T &value) {
162 if constexpr (std::is_same_v<T, GSpan>) {
163 const Span span = value.template typed<bke::SocketValueVariant>();
164 return std::all_of(span.begin(), span.end(), [](const bke::SocketValueVariant &value) {
165 return value.owns_direct_data();
166 });
167 }
168 else if constexpr (std::is_same_v<T, GPointer>) {
169 return value.template get<bke::SocketValueVariant>()->owns_direct_data();
170 }
171 else {
173 }
174 },
175 values);
176 }
177 if (cpp_type_.is<bke::GeometrySet>()) {
178 return std::visit(
179 []<typename T>(const T &value) {
180 if constexpr (std::is_same_v<T, GSpan>) {
181 const Span span = value.template typed<bke::GeometrySet>();
182 return std::all_of(span.begin(), span.end(), [](const bke::GeometrySet &value) {
183 return value.owns_direct_data();
184 });
185 }
186 else if constexpr (std::is_same_v<T, GPointer>) {
187 return value.template get<bke::GeometrySet>()->owns_direct_data();
188 }
189 else {
191 }
192 },
193 values);
194 }
195 return true;
196}
197
199 const CPPType &type,
200 const int64_t size) const
201{
202 memory.add_shared(this->sharing_info.get(), type.size * size);
203}
204
206{
207 memory.add(type.size);
208}
209
211{
212 if (this->sharing_info && !this->sharing_info->is_mutable()) {
213 GArray<> new_array(type, size, NoInitialization());
214 type.copy_construct_n(this->data, new_array.data(), size);
215 *this = array_data_from_garray(std::move(new_array));
216 }
217 if (this->sharing_info) {
218 this->sharing_info->tag_ensured_mutable();
219 }
220 return {type, const_cast<void *>(this->data), size};
221}
222
224{
225 if (this->sharing_info && !this->sharing_info->is_mutable()) {
226 *this = SingleData::ForValue(GPointer(type, this->value));
227 }
228 if (this->sharing_info) {
229 this->sharing_info->tag_ensured_mutable();
230 }
231 return GMutablePointer{type, const_cast<void *>(this->value)};
232}
233
234std::variant<GSpan, GPointer> GList::values() const
235{
236 if (const auto *array_data = std::get_if<ArrayData>(&data_)) {
237 return GSpan(cpp_type_, array_data->data, size_);
238 }
239 if (const auto *single_data = std::get_if<SingleData>(&data_)) {
240 return GPointer(cpp_type_, single_data->value);
241 }
243 return {};
244}
245
246std::variant<GMutableSpan, GMutablePointer> GList::values_for_write()
247{
248 if (auto *array_data = std::get_if<ArrayData>(&data_)) {
249 return array_data->span_for_write(cpp_type_, size_);
250 }
251 if (auto *single_data = std::get_if<SingleData>(&data_)) {
252 return single_data->value_for_write(cpp_type_);
253 }
255 return {};
256}
257
259 : cpp_type_(type), data_(std::move(data)), size_(size)
260{
261}
262
264{
265 return GListPtr(MEM_new<GList>(__func__, type, std::move(data), size));
266}
267
269{
270 const CPPType &type = array.type();
271 const int64_t size = array.size();
272 return GList::create(type, array_data_from_garray(std::move(array)), size);
273}
274
276{
277 const CPPType &type = *value.type();
278 return GListPtr{MEM_new<GList>(__func__, type, SingleData::ForValue(value), size)};
279}
280
281} // namespace blender::nodes
#define BLI_assert_unreachable()
#define BLI_assert_unreachable_static_t(T)
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
size_t size() const
void copy_construct_n(const void *src, void *dst, int64_t n) const
void fill_construct_n(const void *value, void *dst, int64_t n) const
void copy_construct(const void *src, void *dst) const
const void * default_value() const
const void * data() const
const CPPType * type() const
GPointer()=default
const void * get() const
GSpan()=default
static GVArray from_span(GSpan span)
static GVArray from_single_ref(const CPPType &type, int64_t size, const void *value)
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
void add_shared(const ImplicitSharingInfo *sharing_info, const FunctionRef< void(MemoryCounter &shared_memory)> count_fn)
static ArrayData ForConstructed(const CPPType &type, int64_t size)
static ArrayData ForUninitialized(const CPPType &type, int64_t size)
static ArrayData ForDefaultValue(const CPPType &type, int64_t size)
GMutableSpan span_for_write(const CPPType &type, int64_t size)
void count_memory(MemoryCounter &memory, const CPPType &type, const int64_t size) const
static ArrayData ForValue(const GPointer &value, int64_t size)
static SingleData ForValue(const GPointer &value)
void count_memory(MemoryCounter &memory, const CPPType &type) const
static SingleData ForDefaultValue(const CPPType &type)
GMutablePointer value_for_write(const CPPType &type)
const List< T > & typed() const
static GListPtr from_garray(GArray<> array)
void count_memory(MemoryCounter &memory) const
std::variant< ArrayData, SingleData > DataVariant
static GListPtr from_single(GPointer value, int64_t size)
std::variant< GSpan, GPointer > values() const
static GListPtr create(const CPPType &type, DataVariant data, const int64_t size)
std::variant< GMutableSpan, GMutablePointer > values_for_write()
#define T
static GList::ArrayData array_data_from_garray(GArray<> array)
ImplicitSharingPtr< Bundle > BundlePtr
__int64 int64_t
Definition BKE_text.h:90
constexpr bool memory_is_zero(const void *data, const size_t size)
BMesh const char void * data
void *(*) MEM_new_array_zeroed_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1