Blender V5.3
implicit_sharing.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
15
17
19 public:
20 void *data;
21
23 {
24 BLI_assert(data != nullptr);
25 }
26
27 private:
28 void delete_self_with_data() override
29 {
30 if (data != nullptr) {
32 }
33 MEM_delete(this);
34 }
35
36 void delete_data_only() override
37 {
39 data = nullptr;
40 }
41};
42
43namespace detail {
44
46{
47 return MEM_new<MEMFreeImplicitSharing>(__func__, data);
48}
49
50void *make_trivial_data_mutable_impl(void *old_data,
51 const int64_t size,
52 const int64_t alignment,
53 const ImplicitSharingInfo **sharing_info)
54{
55 if (!old_data) {
56 BLI_assert(size == 0);
57 return nullptr;
58 }
59
60 BLI_assert(*sharing_info != nullptr);
61 if ((*sharing_info)->is_mutable()) {
62 (*sharing_info)->tag_ensured_mutable();
63 }
64 else {
65 void *new_data = MEM_new_uninitialized_aligned(size, alignment, __func__);
66 memcpy(new_data, old_data, size);
67 (*sharing_info)->remove_user_and_delete_if_last();
68 *sharing_info = info_for_mem_free_impl(new_data);
69 return new_data;
70 }
71
72 return old_data;
73}
74
75void *resize_trivial_array_impl(void *old_data,
76 const int64_t old_size,
77 const int64_t new_size,
78 const int64_t alignment,
79 const ImplicitSharingInfo **sharing_info)
80{
81 if (new_size == 0) {
82 if (*sharing_info) {
83 (*sharing_info)->remove_user_and_delete_if_last();
84 *sharing_info = nullptr;
85 }
86 return nullptr;
87 }
88
89 if (!old_data) {
90 BLI_assert(old_size == 0);
91 BLI_assert(*sharing_info == nullptr);
92 void *new_data = MEM_new_uninitialized_aligned(new_size, alignment, __func__);
93 *sharing_info = info_for_mem_free_impl(new_data);
94 return new_data;
95 }
96
97 BLI_assert(old_size != 0);
98 if ((*sharing_info)->is_mutable()) {
99 if (auto *info = const_cast<MEMFreeImplicitSharing *>(
100 dynamic_cast<const MEMFreeImplicitSharing *>(*sharing_info)))
101 {
102 /* If the array was allocated with the MEM allocator, we can use realloc directly, which
103 * could theoretically give better performance if the data can be reused in place. */
104 void *new_data = static_cast<int *>(MEM_realloc_uninitialized(old_data, new_size));
105 info->data = new_data;
106 (*sharing_info)->tag_ensured_mutable();
107 return new_data;
108 }
109 }
110
111 void *new_data = MEM_new_uninitialized_aligned(new_size, alignment, __func__);
112 memcpy(new_data, old_data, std::min(old_size, new_size));
113 (*sharing_info)->remove_user_and_delete_if_last();
114 *sharing_info = info_for_mem_free_impl(new_data);
115 return new_data;
116}
117
118} // namespace detail
119} // namespace blender::implicit_sharing
Read Blender memory allocation and freeing.
#define MEM_realloc_uninitialized(vmemh, len)
void MEM_delete_void(void *vmemh)
Definition mallocn.cc:109
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)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
void MEM_delete_void(void *vmemh)
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
BMesh const char void * data