forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaged_ref.hh
126 lines (104 loc) · 2.81 KB
/
managed_ref.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include "utils/allocation_strategy.hh"
template<typename T>
requires std::is_nothrow_move_constructible_v<T>
class managed;
//
// Similar to std::unique_ptr<>, but for LSA-allocated objects. Remains
// valid across deferring points. See make_managed().
//
// std::unique_ptr<> can't be used with LSA-allocated objects because
// it assumes that the object doesn't move after being allocated. This
// is not true for LSA, which moves objects during compaction.
//
// Also works for objects allocated using standard allocators, though
// there the extra space overhead of a pointer is not justified.
// It still make sense to use it in places which are meant to work
// with either kind of allocator.
//
template<typename T>
struct managed_ref {
managed<T>* _ptr;
managed_ref() : _ptr(nullptr) {}
managed_ref(const managed_ref&) = delete;
managed_ref(managed_ref&& other) noexcept
: _ptr(other._ptr)
{
other._ptr = nullptr;
if (_ptr) {
_ptr->_backref = &_ptr;
}
}
~managed_ref() {
if (_ptr) {
current_allocator().destroy(_ptr);
}
}
managed_ref& operator=(managed_ref&& o) {
this->~managed_ref();
new (this) managed_ref(std::move(o));
return *this;
}
T* get() {
return _ptr ? &_ptr->_value : nullptr;
}
const T* get() const {
return _ptr ? &_ptr->_value : nullptr;
}
T& operator*() {
return _ptr->_value;
}
const T& operator*() const {
return _ptr->_value;
}
T* operator->() {
return &_ptr->_value;
}
const T* operator->() const {
return &_ptr->_value;
}
explicit operator bool() const {
return _ptr != nullptr;
}
size_t external_memory_usage() const {
return _ptr ? current_allocator().object_memory_size_in_allocator(_ptr) : 0;
}
};
template<typename T>
requires std::is_nothrow_move_constructible_v<T>
class managed {
managed<T>** _backref;
T _value;
template<typename T_>
friend struct managed_ref;
public:
managed(managed<T>** backref, T&& v) noexcept
: _backref(backref)
, _value(std::move(v))
{
*_backref = this;
}
managed(managed&& other) noexcept
: _backref(other._backref)
, _value(std::move(other._value))
{
*_backref = this;
}
};
//
// Allocates T using given AllocationStrategy and returns a managed_ref owning the
// allocated object.
//
template<typename T, typename... Args>
managed_ref<T>
make_managed(Args&&... args) {
managed_ref<T> ref;
current_allocator().construct<managed<T>>(&ref._ptr, T(std::forward<Args>(args)...));
return ref;
}