gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
refcnt.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Authors: Nathan Binkert
29  */
30 
31 #ifndef __BASE_REFCNT_HH__
32 #define __BASE_REFCNT_HH__
33 
46 {
47  private:
48  // The reference count is mutable because one may want to
49  // reference count a const pointer. This really is OK because
50  // const is about logical constness of the object not really about
51  // strictly disallowing an object to change.
52  mutable int count;
53 
54  private:
55  // Don't allow a default copy constructor or copy operator on
56  // these objects because the default operation will copy the
57  // reference count as well and we certainly don't want that.
58  RefCounted(const RefCounted &);
60 
61  public:
69  RefCounted() : count(0) {}
70 
80  virtual ~RefCounted() {}
81 
83  void incref() { ++count; }
84 
87  void decref() { if (--count <= 0) delete this; }
88 };
89 
105 template <class T>
107 {
108  protected:
111  T *data;
112 
119  void
120  copy(T *d)
121  {
122  data = d;
123  if (data)
124  data->incref();
125  }
126 
132  void
133  del()
134  {
135  if (data)
136  data->decref();
137  }
138 
142  void
143  set(T *d)
144  {
145  // Need to check if we're actually changing because otherwise
146  // we could delete the last reference before adding the new
147  // reference.
148  if (data != d) {
149  del();
150  copy(d);
151  }
152  }
153 
154  public:
157 
160  RefCountingPtr(T *data) { copy(data); }
161 
165 
168 
169  // The following pointer access functions are const because they
170  // don't actually change the pointer, though the user could change
171  // what is pointed to. This is analagous to a "Foo * const".
172 
174  T *operator->() const { return data; }
175 
177  T &operator*() const { return *data; }
178 
180  T *get() const { return data; }
181 
183  const RefCountingPtr &operator=(T *p) { set(p); return *this; }
184 
187  { return operator=(r.data); }
188 
190  bool operator!() const { return data == 0; }
191 
193  operator bool() const { return data != 0; }
194 };
195 
197 template<class T>
198 inline bool operator==(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
199 { return l.get() == r.get(); }
200 
203 template<class T>
204 inline bool operator==(const RefCountingPtr<T> &l, const T *r)
205 { return l.get() == r; }
206 
209 template<class T>
210 inline bool operator==(const T *l, const RefCountingPtr<T> &r)
211 { return l == r.get(); }
212 
214 template<class T>
215 inline bool operator!=(const RefCountingPtr<T> &l, const RefCountingPtr<T> &r)
216 { return l.get() != r.get(); }
217 
220 template<class T>
221 inline bool operator!=(const RefCountingPtr<T> &l, const T *r)
222 { return l.get() != r; }
223 
226 template<class T>
227 inline bool operator!=(const T *l, const RefCountingPtr<T> &r)
228 { return l != r.get(); }
229 
230 #endif // __BASE_REFCNT_HH__
bool operator!() const
Check if the pointer is empty.
Definition: refcnt.hh:190
virtual ~RefCounted()
We make the destructor virtual because we're likely to have virtual functions on reference counted ob...
Definition: refcnt.hh:80
T * get() const
Directly access the pointer itself without taking a reference.
Definition: refcnt.hh:180
If you want a reference counting pointer to a mutable object, create it like this: ...
Definition: refcnt.hh:106
RefCountingPtr()
Create an empty reference counting pointer.
Definition: refcnt.hh:156
const RefCountingPtr & operator=(T *p)
Assign a new value to the pointer.
Definition: refcnt.hh:183
void set(T *d)
Drop the old reference and change it to something new.
Definition: refcnt.hh:143
~RefCountingPtr()
Destroy the pointer and any reference it may hold.
Definition: refcnt.hh:167
RefCounted()
We initialize the reference count to zero and the first object to take ownership of it must increment...
Definition: refcnt.hh:69
T * operator->() const
Access a member variable.
Definition: refcnt.hh:174
Bitfield< 9 > d
Definition: miscregs.hh:1375
void del()
Delete the reference to any existing object if it is non NULL.
Definition: refcnt.hh:133
Derive from RefCounted if you want to enable reference counting of this class.
Definition: refcnt.hh:45
RefCountingPtr(T *data)
Create a new reference counting pointer to some object (probably something newly created).
Definition: refcnt.hh:160
RefCountingPtr(const RefCountingPtr &r)
Create a new reference counting pointer by copying another one.
Definition: refcnt.hh:164
bool operator==(const RefCountingPtr< T > &l, const RefCountingPtr< T > &r)
Check for equality of two reference counting pointers.
Definition: refcnt.hh:198
RefCounted & operator=(const RefCounted &)
void copy(T *d)
Copy a new pointer value and increment the reference count if it is a valid pointer.
Definition: refcnt.hh:120
bool operator!=(const RefCountingPtr< T > &l, const RefCountingPtr< T > &r)
Check for inequality of two reference counting pointers.
Definition: refcnt.hh:215
const RefCountingPtr & operator=(const RefCountingPtr &r)
Copy the pointer from another RefCountingPtr.
Definition: refcnt.hh:186
T * data
The stored pointer.
Definition: refcnt.hh:111
void incref()
Increment the reference count.
Definition: refcnt.hh:83
Bitfield< 0 > p
void decref()
Decrement the reference count and destroy the object if all references are gone.
Definition: refcnt.hh:87
int count
Definition: refcnt.hh:52
Bitfield< 5 > l
T & operator*() const
Dereference the pointer.
Definition: refcnt.hh:177

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6