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
30 /// \addtogroup memory-module
33 /// Provides a mechanism for managing the lifetime of a buffer.
35 /// A \c deleter is an object that is used to inform the consumer
36 /// of some buffer (not referenced by the deleter itself) how to
37 /// delete the buffer. This can be by calling an arbitrary function
38 /// or destroying an object carried by the deleter. Examples of
39 /// a deleter's encapsulated actions are:
41 /// - calling \c std::free(p) on some captured pointer, p
42 /// - calling \c delete \c p on some captured pointer, p
43 /// - decrementing a reference count somewhere
45 /// A deleter performs its action from its destructor.
50 struct raw_object_tag
{};
53 // if bit 0 set, point to object to be freed directly.
54 impl
* _impl
= nullptr;
56 /// Constructs an empty deleter that does nothing in its destructor.
58 deleter(const deleter
&) = delete;
60 deleter(deleter
&& x
) noexcept
: _impl(x
._impl
) { x
._impl
= nullptr; }
62 explicit deleter(impl
* i
) : _impl(i
) {}
63 deleter(raw_object_tag tag
, void* object
)
64 : _impl(from_raw_object(object
)) {}
66 /// Destroys the deleter and carries out the encapsulated action.
68 deleter
& operator=(deleter
&& x
);
69 deleter
& operator=(deleter
&) = delete;
70 /// Performs a sharing operation. The encapsulated action will only
71 /// be carried out after both the original deleter and the returned
72 /// deleter are both destroyed.
74 /// \return a deleter with the same encapsulated action as this one.
76 /// Checks whether the deleter has an associated action.
77 explicit operator bool() const { return bool(_impl
); }
81 new (this) deleter(i
);
84 /// Appends another deleter to this deleter. When this deleter is
85 /// destroyed, both encapsulated actions will be carried out.
86 void append(deleter d
);
88 static bool is_raw_object(impl
* i
) {
89 auto x
= reinterpret_cast<uintptr_t>(i
);
92 bool is_raw_object() const {
93 return is_raw_object(_impl
);
95 static void* to_raw_object(impl
* i
) {
96 auto x
= reinterpret_cast<uintptr_t>(i
);
97 return reinterpret_cast<void*>(x
& ~uintptr_t(1));
99 void* to_raw_object() const {
100 return to_raw_object(_impl
);
102 impl
* from_raw_object(void* object
) {
103 auto x
= reinterpret_cast<uintptr_t>(object
);
104 return reinterpret_cast<impl
*>(x
| 1);
109 struct deleter::impl
{
110 std::atomic_uint refs
;
112 impl(deleter next
) : refs(1), next(std::move(next
)) {}
117 inline deleter::~deleter() {
118 if (is_raw_object()) {
119 std::free(to_raw_object());
122 if (_impl
&& --_impl
->refs
== 0) {
127 inline deleter
& deleter::operator=(deleter
&& x
) {
130 new (this) deleter(std::move(x
));
136 template <typename Deleter
>
137 struct lambda_deleter_impl final
: deleter::impl
{
139 lambda_deleter_impl(deleter next
, Deleter
&& del
)
140 : impl(std::move(next
)), del(std::move(del
)) {}
141 ~lambda_deleter_impl() override
{ del(); }
144 template <typename Object
>
145 struct object_deleter_impl final
: deleter::impl
{
147 object_deleter_impl(deleter next
, Object
&& obj
)
148 : impl(std::move(next
)), obj(std::move(obj
)) {}
151 template <typename Object
>
153 object_deleter_impl
<Object
>* make_object_deleter_impl(deleter next
, Object obj
) {
154 return new object_deleter_impl
<Object
>(std::move(next
), std::move(obj
));
158 /// Makes a \ref deleter that encapsulates the action of
159 /// destroying an object, as well as running another deleter. The input
160 /// object is moved to the deleter, and destroyed when the deleter is destroyed.
162 /// \param d deleter that will become part of the new deleter's encapsulated action
163 /// \param o object whose destructor becomes part of the new deleter's encapsulated action
165 template <typename Object
>
166 deleter
make_deleter(deleter next
, Object o
) {
167 return deleter(new lambda_deleter_impl
<Object
>(std::move(next
), std::move(o
)));
170 /// Makes a \ref deleter that encapsulates the action of destroying an object. The input
171 /// object is moved to the deleter, and destroyed when the deleter is destroyed.
173 /// \param o object whose destructor becomes the new deleter's encapsulated action
175 template <typename Object
>
176 deleter
make_deleter(Object o
) {
177 return make_deleter(deleter(), std::move(o
));
181 struct free_deleter_impl final
: deleter::impl
{
183 free_deleter_impl(void* obj
) : impl(deleter()), obj(obj
) {}
184 ~free_deleter_impl() override
{ std::free(obj
); }
188 inline deleter
deleter::share() {
192 if (is_raw_object()) {
193 _impl
= new free_deleter_impl(to_raw_object());
196 return deleter(_impl
);
199 // Appends 'd' to the chain of deleters. Avoids allocation if possible. For
200 // performance reasons the current chain should be shorter and 'd' should be
202 inline void deleter::append(deleter d
) {
206 impl
* next_impl
= _impl
;
207 deleter
* next_d
= this;
209 if (next_impl
== d
._impl
)
211 if (is_raw_object(next_impl
)) {
212 next_d
->_impl
= next_impl
= new free_deleter_impl(to_raw_object(next_impl
));
214 if (next_impl
->refs
!= 1) {
215 next_d
->_impl
= next_impl
= make_object_deleter_impl(std::move(next_impl
->next
), deleter(next_impl
));
217 next_d
= &next_impl
->next
;
218 next_impl
= next_d
->_impl
;
220 next_d
->_impl
= d
._impl
;
224 /// Makes a deleter that calls \c std::free() when it is destroyed.
226 /// \param obj object to free.
228 inline deleter
make_free_deleter(void* obj
) {
232 return deleter(deleter::raw_object_tag(), obj
);
235 /// Makes a deleter that calls \c std::free() when it is destroyed, as well
236 /// as invoking the encapsulated action of another deleter.
238 /// \param d deleter to invoke.
239 /// \param obj object to free.
241 inline deleter
make_free_deleter(deleter next
, void* obj
) {
242 return make_deleter(std::move(next
), [obj
] () mutable { std::free(obj
); });
245 /// \see make_deleter(Object)
247 template <typename T
>
248 inline deleter
make_object_deleter(T
&& obj
) {
249 return deleter
{make_object_deleter_impl(deleter(), std::move(obj
))};
252 /// \see make_deleter(deleter, Object)
254 template <typename T
>
255 inline deleter
make_object_deleter(deleter d
, T
&& obj
) {
256 return deleter
{make_object_deleter_impl(std::move(d
), std::move(obj
))};
261 #endif /* CEPH_COMMON_DELETER_H */