Blender
V5.0
source
blender
blenlib
tests
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 "
MEM_guardedalloc.h
"
6
7
#include "
BLI_implicit_sharing_ptr.hh
"
8
9
#include "testing/testing.h"
10
11
namespace
blender::tests
{
12
13
class
ImplicitlySharedData
:
public
ImplicitSharingMixin
{
14
public
:
15
ImplicitSharingPtr<ImplicitlySharedData>
copy
()
const
16
{
17
return
ImplicitSharingPtr<ImplicitlySharedData>
(MEM_new<ImplicitlySharedData>(__func__));
18
}
19
20
void
delete_self
()
override
21
{
22
MEM_delete(
this
);
23
}
24
};
25
26
class
SharedDataContainer
{
27
private
:
28
ImplicitSharingPtr<ImplicitlySharedData>
data_;
29
30
public
:
31
SharedDataContainer
() : data_(MEM_new<
ImplicitlySharedData
>(__func__)) {}
32
33
const
ImplicitSharingInfo
*
sharing_info
()
const
34
{
35
return
data_.get();
36
}
37
38
const
ImplicitlySharedData
*
get_for_read
()
const
39
{
40
return
data_.get();
41
}
42
43
ImplicitlySharedData
*
get_for_write
()
44
{
45
if
(!data_) {
46
return
nullptr
;
47
}
48
if
(data_->is_mutable()) {
49
data_->tag_ensured_mutable();
50
}
51
else
{
52
data_ = data_->copy();
53
}
54
return
const_cast<
ImplicitlySharedData
*
>
(data_.get());
55
}
56
};
57
58
TEST
(
implicit_sharing
, CopyOnWriteAccess)
59
{
60
/* Create the initial data. */
61
SharedDataContainer
a;
62
EXPECT_NE(a.
get_for_read
(),
nullptr
);
63
64
/* a and b share the same underlying data now. */
65
SharedDataContainer
b
= a;
66
EXPECT_EQ
(a.
get_for_read
(),
b
.get_for_read());
67
68
/* c now shares the data with a and b. */
69
SharedDataContainer
c = a;
70
EXPECT_EQ
(
b
.get_for_read(), c.
get_for_read
());
71
72
/* Retrieving write access on b should make a copy because the data is shared. */
73
ImplicitlySharedData
*data_b1 =
b
.get_for_write();
74
EXPECT_NE(data_b1,
nullptr
);
75
EXPECT_EQ
(data_b1,
b
.get_for_read());
76
EXPECT_NE(data_b1, a.
get_for_read
());
77
EXPECT_NE(data_b1, c.
get_for_read
());
78
79
/* Retrieving the same write access again should *not* make another copy. */
80
ImplicitlySharedData
*data_b2 =
b
.get_for_write();
81
EXPECT_EQ
(data_b1, data_b2);
82
83
/* Moving b should also move the data. b then does not have ownership anymore. Since the data in
84
* b only had one owner, the data is still mutable now that d is the owner. */
85
SharedDataContainer
d = std::move(
b
);
86
EXPECT_EQ
(
b
.get_for_read(),
nullptr
);
87
EXPECT_EQ
(
b
.get_for_write(),
nullptr
);
88
EXPECT_EQ
(d.
get_for_read
(), data_b1);
89
EXPECT_EQ
(d.
get_for_write
(), data_b1);
90
}
91
92
TEST
(
implicit_sharing
, WeakUser)
93
{
94
SharedDataContainer
a;
95
const
ImplicitSharingInfo
*sharing_info = a.
sharing_info
();
96
EXPECT_FALSE(sharing_info->
is_expired
());
97
EXPECT_TRUE(sharing_info->
is_mutable
());
98
sharing_info->
add_weak_user
();
99
EXPECT_FALSE(sharing_info->
is_expired
());
100
EXPECT_TRUE(sharing_info->
is_mutable
());
101
a = {};
102
EXPECT_TRUE(sharing_info->
is_expired
());
103
sharing_info->
remove_weak_user_and_delete_if_last
();
104
}
105
106
TEST
(
implicit_sharing
, Version)
107
{
108
SharedDataContainer
a;
109
const
ImplicitSharingInfo
*sharing_info = a.
sharing_info
();
110
const
int
old_version = sharing_info->
version
();
111
a.
get_for_read
();
112
EXPECT_EQ
(old_version, sharing_info->
version
());
113
a.
get_for_write
();
114
EXPECT_LT(old_version, sharing_info->
version
());
115
}
116
117
}
// namespace blender::tests
EXPECT_EQ
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
BLI_implicit_sharing_ptr.hh
MEM_guardedalloc.h
Read Guarded memory(de)allocation.
blender::ImplicitSharingInfo
Definition
BLI_implicit_sharing.hh:41
blender::ImplicitSharingInfo::remove_weak_user_and_delete_if_last
void remove_weak_user_and_delete_if_last() const
Definition
BLI_implicit_sharing.hh:168
blender::ImplicitSharingInfo::is_expired
bool is_expired() const
Definition
BLI_implicit_sharing.hh:82
blender::ImplicitSharingInfo::is_mutable
bool is_mutable() const
Definition
BLI_implicit_sharing.hh:73
blender::ImplicitSharingInfo::add_weak_user
void add_weak_user() const
Definition
BLI_implicit_sharing.hh:102
blender::ImplicitSharingInfo::version
int64_t version() const
Definition
BLI_implicit_sharing.hh:125
blender::ImplicitSharingMixin
Definition
BLI_implicit_sharing.hh:192
blender::ImplicitSharingPtr
Definition
BLI_implicit_sharing_ptr.hh:24
blender::tests::ImplicitlySharedData
Definition
BLI_implicit_sharing_test.cc:13
blender::tests::ImplicitlySharedData::copy
ImplicitSharingPtr< ImplicitlySharedData > copy() const
Definition
BLI_implicit_sharing_test.cc:15
blender::tests::ImplicitlySharedData::delete_self
void delete_self() override
Definition
BLI_implicit_sharing_test.cc:20
blender::tests::SharedDataContainer
Definition
BLI_implicit_sharing_test.cc:26
blender::tests::SharedDataContainer::get_for_read
const ImplicitlySharedData * get_for_read() const
Definition
BLI_implicit_sharing_test.cc:38
blender::tests::SharedDataContainer::get_for_write
ImplicitlySharedData * get_for_write()
Definition
BLI_implicit_sharing_test.cc:43
blender::tests::SharedDataContainer::sharing_info
const ImplicitSharingInfo * sharing_info() const
Definition
BLI_implicit_sharing_test.cc:33
blender::tests::SharedDataContainer::SharedDataContainer
SharedDataContainer()
Definition
BLI_implicit_sharing_test.cc:31
b
b
Definition
compositor_morphological_distance_infos.hh:24
blender::implicit_sharing
Definition
BLI_implicit_sharing.hh:234
blender::tests
Definition
BLF_tests.cc:9
blender::tests::TEST
TEST(blf_load, load)
Definition
BLF_tests.cc:34
Generated on
for Blender by
doxygen
1.16.1