2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
22 #ifndef CEPH_COMMON_DELETER_H
23 #define CEPH_COMMON_DELETER_H
27 /// \addtogroup memory-module
30 /// Provides a mechanism for managing the lifetime of a buffer.
32 /// A \c deleter is an object that is used to inform the consumer
33 /// of some buffer (not referenced by the deleter itself) how to
34 /// delete the buffer. This can be by calling an arbitrary function
35 /// or destroying an object carried by the deleter. Examples of
36 /// a deleter's encapsulated actions are:
38 /// - calling \c std::free(p) on some captured pointer, p
39 /// - calling \c delete \c p on some captured pointer, p
40 /// - decrementing a reference count somewhere
42 /// A deleter performs its action from its destructor.
47 struct raw_object_tag
{};
50 // if bit 0 set, point to object to be freed directly.
51 impl
* _impl
= nullptr;
53 /// Constructs an empty deleter that does nothing in its destructor.
55 deleter(const deleter
&) = delete;
57 deleter(deleter
&& x
) noexcept
: _impl(x
._impl
) { x
._impl
= nullptr; }
59 explicit deleter(impl
* i
) : _impl(i
) {}
60 deleter(raw_object_tag tag
, void* object
)
61 : _impl(from_raw_object(object
)) {}
63 /// Destroys the deleter and carries out the encapsulated action.
65 deleter
& operator=(deleter
&& x
);
66 deleter
& operator=(deleter
&) = delete;
67 /// Performs a sharing operation. The encapsulated action will only
68 /// be carried out after both the original deleter and the returned
69 /// deleter are both destroyed.
71 /// \return a deleter with the same encapsulated action as this one.
73 /// Checks whether the deleter has an associated action.
74 explicit operator bool() const { return bool(_impl
); }
78 new (this) deleter(i
);
81 /// Appends another deleter to this deleter. When this deleter is
82 /// destroyed, both encapsulated actions will be carried out.
83 void append(deleter d
);
85 static bool is_raw_object(impl
* i
) {
86 auto x
= reinterpret_cast<uintptr_t>(i
);
89 bool is_raw_object() const {
90 return is_raw_object(_impl
);
92 static void* to_raw_object(impl
* i
) {
93 auto x
= reinterpret_cast<uintptr_t>(i
);
94 return reinterpret_cast<void*>(x
& ~uintptr_t(1));
96 void* to_raw_object() const {
97 return to_raw_object(_impl
);
99 impl
* from_raw_object(void* object
) {
100 auto x
= reinterpret_cast<uintptr_t>(object
);
101 return reinterpret_cast<impl
*>(x
| 1);
106 struct deleter::impl
{
107 std::atomic_uint refs
;
109 impl(deleter next
) : refs(1), next(std::move(next
)) {}
114 inline deleter::~deleter() {
115 if (is_raw_object()) {
116 std::free(to_raw_object());
119 if (_impl
&& --_impl
->refs
== 0) {
124 inline deleter
& deleter::operator=(deleter
&& x
) {
127 new (this) deleter(std::move(x
));
133 template <typename Deleter
>
134 struct lambda_deleter_impl final
: deleter::impl
{
136 lambda_deleter_impl(deleter next
, Deleter
&& del
)
137 : impl(std::move(next
)), del(std::move(del
)) {}
138 ~lambda_deleter_impl() override
{ del(); }
141 template <typename Object
>
142 struct object_deleter_impl final
: deleter::impl
{
144 object_deleter_impl(deleter next
, Object
&& obj
)
145 : impl(std::move(next
)), obj(std::move(obj
)) {}
148 template <typename Object
>
150 object_deleter_impl
<Object
>* make_object_deleter_impl(deleter next
, Object obj
) {
151 return new object_deleter_impl
<Object
>(std::move(next
), std::move(obj
));
155 /// Makes a \ref deleter that encapsulates the action of
156 /// destroying an object, as well as running another deleter. The input
157 /// object is moved to the deleter, and destroyed when the deleter is destroyed.
159 /// \param d deleter that will become part of the new deleter's encapsulated action
160 /// \param o object whose destructor becomes part of the new deleter's encapsulated action
162 template <typename Object
>
163 deleter
make_deleter(deleter next
, Object o
) {
164 return deleter(new lambda_deleter_impl
<Object
>(std::move(next
), std::move(o
)));
167 /// Makes a \ref deleter that encapsulates the action of destroying an object. The input
168 /// object is moved to the deleter, and destroyed when the deleter is destroyed.
170 /// \param o object whose destructor becomes the new deleter's encapsulated action
172 template <typename Object
>
173 deleter
make_deleter(Object o
) {
174 return make_deleter(deleter(), std::move(o
));
178 struct free_deleter_impl final
: deleter::impl
{
180 free_deleter_impl(void* obj
) : impl(deleter()), obj(obj
) {}
181 ~free_deleter_impl() override
{ std::free(obj
); }
185 inline deleter
deleter::share() {
189 if (is_raw_object()) {
190 _impl
= new free_deleter_impl(to_raw_object());
193 return deleter(_impl
);
196 // Appends 'd' to the chain of deleters. Avoids allocation if possible. For
197 // performance reasons the current chain should be shorter and 'd' should be
199 inline void deleter::append(deleter d
) {
203 impl
* next_impl
= _impl
;
204 deleter
* next_d
= this;
206 if (next_impl
== d
._impl
)
208 if (is_raw_object(next_impl
)) {
209 next_d
->_impl
= next_impl
= new free_deleter_impl(to_raw_object(next_impl
));
211 if (next_impl
->refs
!= 1) {
212 next_d
->_impl
= next_impl
= make_object_deleter_impl(std::move(next_impl
->next
), deleter(next_impl
));
214 next_d
= &next_impl
->next
;
215 next_impl
= next_d
->_impl
;
217 next_d
->_impl
= d
._impl
;
221 /// Makes a deleter that calls \c std::free() when it is destroyed.
223 /// \param obj object to free.
225 inline deleter
make_free_deleter(void* obj
) {
229 return deleter(deleter::raw_object_tag(), obj
);
232 /// Makes a deleter that calls \c std::free() when it is destroyed, as well
233 /// as invoking the encapsulated action of another deleter.
235 /// \param d deleter to invoke.
236 /// \param obj object to free.
238 inline deleter
make_free_deleter(deleter next
, void* obj
) {
239 return make_deleter(std::move(next
), [obj
] () mutable { std::free(obj
); });
242 /// \see make_deleter(Object)
244 template <typename T
>
245 inline deleter
make_object_deleter(T
&& obj
) {
246 return deleter
{make_object_deleter_impl(deleter(), std::move(obj
))};
249 /// \see make_deleter(deleter, Object)
251 template <typename T
>
252 inline deleter
make_object_deleter(deleter d
, T
&& obj
) {
253 return deleter
{make_object_deleter_impl(std::move(d
), std::move(obj
))};
258 #endif /* CEPH_COMMON_DELETER_H */