Blender V5.3
BLI_implicit_sharing_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <memory>
6
7#include "MEM_guardedalloc.h"
8
10
11#include "testing/testing.h"
12
13namespace blender::tests {
14
16 public:
18 {
19 return ImplicitSharingPtr<ImplicitlySharedData>(MEM_new<ImplicitlySharedData>(__func__));
20 }
21
22 void delete_self() override
23 {
24 MEM_delete(this);
25 }
26};
27
29 private:
31
32 public:
33 SharedDataContainer() : data_(MEM_new<ImplicitlySharedData>(__func__)) {}
34
36 {
37 return data_.get();
38 }
39
41 {
42 return data_.get();
43 }
44
46 {
47 if (!data_) {
48 return nullptr;
49 }
50 if (data_->is_mutable()) {
51 data_->tag_ensured_mutable();
52 }
53 else {
54 data_ = data_->copy();
55 }
56 return const_cast<ImplicitlySharedData *>(data_.get());
57 }
58};
59
60TEST(implicit_sharing, CopyOnWriteAccess)
61{
62 /* Create the initial data. */
64 EXPECT_NE(a.get_for_read(), nullptr);
65
66 /* a and b share the same underlying data now. */
68 EXPECT_EQ(a.get_for_read(), b.get_for_read());
69
70 /* c now shares the data with a and b. */
72 EXPECT_EQ(b.get_for_read(), c.get_for_read());
73
74 /* Retrieving write access on b should make a copy because the data is shared. */
75 ImplicitlySharedData *data_b1 = b.get_for_write();
76 EXPECT_NE(data_b1, nullptr);
77 EXPECT_EQ(data_b1, b.get_for_read());
78 EXPECT_NE(data_b1, a.get_for_read());
79 EXPECT_NE(data_b1, c.get_for_read());
80
81 /* Retrieving the same write access again should *not* make another copy. */
82 ImplicitlySharedData *data_b2 = b.get_for_write();
83 EXPECT_EQ(data_b1, data_b2);
84
85 /* Moving b should also move the data. b then does not have ownership anymore. Since the data in
86 * b only had one owner, the data is still mutable now that d is the owner. */
87 SharedDataContainer d = std::move(b);
88 EXPECT_EQ(b.get_for_read(), nullptr);
89 EXPECT_EQ(b.get_for_write(), nullptr);
90 EXPECT_EQ(d.get_for_read(), data_b1);
91 EXPECT_EQ(d.get_for_write(), data_b1);
92}
93
95{
97 const ImplicitSharingInfo *sharing_info = a.sharing_info();
98 EXPECT_FALSE(sharing_info->is_expired());
99 EXPECT_TRUE(sharing_info->is_mutable());
100 sharing_info->add_weak_user();
101 EXPECT_FALSE(sharing_info->is_expired());
102 EXPECT_TRUE(sharing_info->is_mutable());
103 a = {};
104 EXPECT_TRUE(sharing_info->is_expired());
106}
107
109{
111 const ImplicitSharingInfo *sharing_info = a.sharing_info();
112 const int old_version = sharing_info->version();
113 a.get_for_read();
114 EXPECT_EQ(old_version, sharing_info->version());
115 a.get_for_write();
116 EXPECT_LT(old_version, sharing_info->version());
117}
118
119} // namespace blender::tests
Read Blender memory allocation and freeing.
ImplicitSharingPtr< ImplicitlySharedData > copy() const
const ImplicitlySharedData * get_for_read() const
const ImplicitSharingInfo * sharing_info() const
#define EXPECT_FALSE(result)
#define EXPECT_LT(result, expect)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
TEST(any_virtual, DefaultConstructor)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)