2 * Copyright (c) 2017-2018 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __BASE_REFCNT_HH__
42 #define __BASE_REFCNT_HH__
44 #include <type_traits>
47 * @file base/refcnt.hh
49 * Classes for managing reference counted objects.
53 * Derive from RefCounted if you want to enable reference counting of
54 * this class. If you want to use automatic reference counting, you
55 * should use RefCountingPtr<T> instead of regular pointers.
60 // The reference count is mutable because one may want to
61 // reference count a const pointer. This really is OK because
62 // const is about logical constness of the object not really about
63 // strictly disallowing an object to change.
67 // Don't allow a default copy constructor or copy operator on
68 // these objects because the default operation will copy the
69 // reference count as well and we certainly don't want that.
70 RefCounted(const RefCounted &);
71 RefCounted &operator=(const RefCounted &);
75 * We initialize the reference count to zero and the first object
76 * to take ownership of it must increment it to one.
78 * @attention A memory leak will occur if you never assign a newly
79 * constructed object to a reference counting pointer.
81 RefCounted() : count(0) {}
84 * We make the destructor virtual because we're likely to have
85 * virtual functions on reference counted objects.
87 * @todo Even if this were true, does it matter? Shouldn't the
88 * derived class indicate this? This only matters if we would
89 * ever choose to delete a "RefCounted *" which I doubt we'd ever
90 * do. We don't ever delete a "void *".
92 virtual ~RefCounted() {}
94 /// Increment the reference count
95 void incref() const { ++count; }
97 /// Decrement the reference count and destroy the object if all
98 /// references are gone.
99 void decref() const { if (--count <= 0) delete this; }
103 * If you want a reference counting pointer to a mutable object,
104 * create it like this:
106 * typedef RefCountingPtr<Foo> FooPtr;
109 * @attention Do not use "const FooPtr"
110 * To create a reference counting pointer to a const object, use this:
112 * typedef RefCountingPtr<const Foo> ConstFooPtr;
115 * These two usages are analogous to iterator and const_iterator in the stl.
124 /** Convenience aliases for const/non-const versions of T w/ friendship. */
126 static constexpr auto TisConst = std::is_const<T>::value;
127 using ConstT = typename std::conditional<TisConst,
129 RefCountingPtr<typename std::add_const<T>::type>>::type;
131 using NonConstT = typename std::conditional<TisConst,
132 RefCountingPtr<typename std::remove_const<T>::type>,
133 RefCountingPtr<T>>::type;
136 /// The stored pointer.
137 /// Arguably this should be private.
141 * Copy a new pointer value and increment the reference count if
142 * it is a valid pointer. Note, this does not delete the
143 * reference any existing object.
144 * @param d Pointer to store.
155 * Delete the reference to any existing object if it is non NULL.
156 * @attention this doesn't clear the pointer value, so a double
157 * decref could happen if not careful.
167 * Drop the old reference and change it to something new.
172 // Need to check if we're actually changing because otherwise
173 // we could delete the last reference before adding the new
182 /// Create an empty reference counting pointer.
183 RefCountingPtr() : data(0) {}
185 /// Create a new reference counting pointer to some object
186 /// (probably something newly created). Adds a reference.
187 RefCountingPtr(T *data) { copy(data); }
189 /// Create a new reference counting pointer by copying another
190 /// one. Adds a reference.
191 RefCountingPtr(const RefCountingPtr &r) { copy(r.data); }
193 /** Move-constructor.
194 * Does not add a reference.
196 RefCountingPtr(RefCountingPtr&& r)
202 template <bool B = TisConst>
203 RefCountingPtr(const NonConstT &r) { copy(r.data); }
205 /// Destroy the pointer and any reference it may hold.
206 ~RefCountingPtr() { del(); }
208 // The following pointer access functions are const because they
209 // don't actually change the pointer, though the user could change
210 // what is pointed to. This is analagous to a "Foo * const".
212 /// Access a member variable.
213 T *operator->() const { return data; }
215 /// Dereference the pointer.
216 T &operator*() const { return *data; }
218 /// Directly access the pointer itself without taking a reference.
219 T *get() const { return data; }
221 template <bool B = TisConst>
222 operator RefCountingPtr<typename std::enable_if_t<!B, ConstT>>()
224 return RefCountingPtr<const T>(*this);
227 /// Assign a new value to the pointer
228 const RefCountingPtr &operator=(T *p) { set(p); return *this; }
230 /// Copy the pointer from another RefCountingPtr
231 const RefCountingPtr &operator=(const RefCountingPtr &r)
232 { return operator=(r.data); }
234 /// Move-assign the pointer from another RefCountingPtr
235 const RefCountingPtr &operator=(RefCountingPtr&& r)
237 /* This happens regardless of whether the pointer is the same or not,
238 * because of the move semantics, the rvalue needs to be 'destroyed'.
246 /// Check if the pointer is empty
247 bool operator!() const { return data == 0; }
249 /// Check if the pointer is non-empty
250 operator bool() const { return data != 0; }
253 /// Check for equality of two reference counting pointers.
255 inline bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
256 { return l.get() == r.get(); }
258 /// Check for equality of of a reference counting pointers and a
261 inline bool operator==(const RefCountingPtr<T> &l, const T *r)
262 { return l.get() == r; }
264 /// Check for equality of of a reference counting pointers and a
267 inline bool operator==(const T *l, const RefCountingPtr<T> &r)
268 { return l == r.get(); }
270 /// Check for inequality of two reference counting pointers.
272 inline bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
273 { return l.get() != r.get(); }
275 /// Check for inequality of of a reference counting pointers and a
278 inline bool operator!=(const RefCountingPtr<T> &l, const T *r)
279 { return l.get() != r; }
281 /// Check for inequality of of a reference counting pointers and a
284 inline bool operator!=(const T *l, const RefCountingPtr<T> &r)
285 { return l != r.get(); }
287 #endif // __BASE_REFCNT_HH__