gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
refcnt.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017-2018 ARM Limited
3 * All rights reserved.
4 *
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.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 */
40
41#ifndef __BASE_REFCNT_HH__
42#define __BASE_REFCNT_HH__
43
44#include <type_traits>
45
46#include "base/compiler.hh"
47
53
54namespace gem5
55{
56
63{
64 private:
65 // The reference count is mutable because one may want to
66 // reference count a const pointer. This really is OK because
67 // const is about logical constness of the object not really about
68 // strictly disallowing an object to change.
69 mutable int count;
70
71 private:
72 // Don't allow a default copy constructor or copy operator on
73 // these objects because the default operation will copy the
74 // reference count as well and we certainly don't want that.
77
78 public:
86 RefCounted() : count(0) {}
87
97 virtual ~RefCounted() {}
98
100 void incref() const { ++count; }
101
104 bool
105 decref() const
106 {
107 return --count <= 0;
108 }
109};
110
126template <class T>
128{
129 public:
130 using PtrType = T*;
131
132 protected:
135 static constexpr auto TisConst = std::is_const_v<T>;
136 using ConstT = typename std::conditional_t<TisConst,
139 friend ConstT;
140 using NonConstT = typename std::conditional_t<TisConst,
143 friend NonConstT;
148
155 void
156 copy(T *d)
157 {
158 data = d;
159 if (data)
160 data->incref();
161 }
162
168 GEM5_NO_INLINE void
170 {
171 if (data && data->decref()) {
172 delete data;
173 }
174 }
175
179 void
180 set(T *d)
181 {
182 // Need to check if we're actually changing because otherwise
183 // we could delete the last reference before adding the new
184 // reference.
185 if (data != d) {
186 del();
187 copy(d);
188 }
189 }
190
191 public:
194
198
202
207 {
208 data = r.data;
209 r.data = nullptr;
210 }
211
212 template <bool B = TisConst>
213 RefCountingPtr(const NonConstT &r) { copy(r.data); }
214
217
218 // The following pointer access functions are const because they
219 // don't actually change the pointer, though the user could change
220 // what is pointed to. This is analagous to a "Foo * const".
221
223 T *operator->() const { return data; }
224
226 T &operator*() const { return *data; }
227
229 T *get() const { return data; }
230
231 template <bool B = TisConst>
236
238 const RefCountingPtr &operator=(T *p) { set(p); return *this; }
239
241 const RefCountingPtr &
243 {
244 return operator=(r.data);
245 }
246
248 const RefCountingPtr &
250 {
251 /* This happens regardless of whether the pointer is the same or not,
252 * because of the move semantics, the rvalue needs to be 'destroyed'.
253 */
254 del();
255 data = r.data;
256 r.data = nullptr;
257 return *this;
258 }
259
261 bool operator!() const { return data == 0; }
262
264 operator bool() const { return data != 0; }
265};
266
268template<class T>
269inline bool
271{
272 return l.get() == r.get();
273}
274
277template<class T>
278inline bool
280{
281 return l.get() == r;
282}
283
286template<class T>
287inline bool
289{
290 return l == r.get();
291}
292
294template<class T>
295inline bool
297{
298 return l.get() != r.get();
299}
300
303template<class T>
304inline bool
306{
307 return l.get() != r;
308}
309
312template<class T>
313inline bool
315{
316 return l != r.get();
317}
318
319} // namespace gem5
320
321#endif // __BASE_REFCNT_HH__
RefCounted(const RefCounted &)
virtual ~RefCounted()
We make the destructor virtual because we're likely to have virtual functions on reference counted ob...
Definition refcnt.hh:97
RefCounted()
We initialize the reference count to zero and the first object to take ownership of it must increment...
Definition refcnt.hh:86
bool decref() const
Decrement the reference count and return true if all references are gone.
Definition refcnt.hh:105
RefCounted & operator=(const RefCounted &)
void incref() const
Increment the reference count.
Definition refcnt.hh:100
If you want a reference counting pointer to a mutable object, create it like this:
Definition refcnt.hh:128
RefCountingPtr()
Create an empty reference counting pointer.
Definition refcnt.hh:193
T & operator*() const
Dereference the pointer.
Definition refcnt.hh:226
GEM5_NO_INLINE void del()
Delete the reference to any existing object if it is non NULL.
Definition refcnt.hh:169
const RefCountingPtr & operator=(RefCountingPtr &&r)
Move-assign the pointer from another RefCountingPtr.
Definition refcnt.hh:249
typename std::conditional_t< TisConst, RefCountingPtr< T >, RefCountingPtr< typename std::add_const< T >::type > > ConstT
Definition refcnt.hh:136
bool operator!() const
Check if the pointer is empty.
Definition refcnt.hh:261
const RefCountingPtr & operator=(T *p)
Assign a new value to the pointer.
Definition refcnt.hh:238
void set(T *d)
Drop the old reference and change it to something new.
Definition refcnt.hh:180
static constexpr auto TisConst
Definition refcnt.hh:135
~RefCountingPtr()
Destroy the pointer and any reference it may hold.
Definition refcnt.hh:216
RefCountingPtr(T *data)
Create a new reference counting pointer to some object (probably something newly created).
Definition refcnt.hh:197
RefCountingPtr(const RefCountingPtr &r)
Create a new reference counting pointer by copying another one.
Definition refcnt.hh:201
RefCountingPtr(const NonConstT &r)
Definition refcnt.hh:213
void copy(T *d)
Copy a new pointer value and increment the reference count if it is a valid pointer.
Definition refcnt.hh:156
T * operator->() const
Access a member variable.
Definition refcnt.hh:223
const RefCountingPtr & operator=(const RefCountingPtr &r)
Copy the pointer from another RefCountingPtr.
Definition refcnt.hh:242
RefCountingPtr(RefCountingPtr &&r)
Move-constructor.
Definition refcnt.hh:206
typename std::conditional_t< TisConst, RefCountingPtr< typename std::remove_const< T >::type >, RefCountingPtr< T > > NonConstT
Definition refcnt.hh:140
T * get() const
Directly access the pointer itself without taking a reference.
Definition refcnt.hh:229
Bitfield< 12, 11 > set
Bitfield< 9 > d
Definition misc_types.hh:64
Bitfield< 5 > l
Bitfield< 0 > p
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
static bool operator==(const PCStateBase &a, const PCStateBase &b)
Definition pcstate.hh:163
static bool operator!=(const PCStateBase &a, const PCStateBase &b)
Definition pcstate.hh:169

Generated on Mon Oct 27 2025 04:12:59 for gem5 by doxygen 1.14.0