slicc: added MOESI_CMP_directory, DMA SequencerMsg, parameterized controllers
[gem5.git] / src / mem / gems_common / RefCnt.hh
1 /*
2 * Copyright (c) 1999-2005 Mark D. Hill and David A. Wood
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
29 #ifndef REFCNT_H
30 #define REFCNT_H
31
32 template <class TYPE>
33 class RefCnt {
34 public:
35 // Constructors
36 RefCnt();
37 RefCnt(const TYPE& data);
38
39 // Destructor
40 ~RefCnt();
41
42 // Public Methods
43 const TYPE* ref() const { return m_data_ptr; }
44 TYPE* ref() { return m_data_ptr; }
45 TYPE* mod_ref() const { return m_data_ptr; }
46 void freeRef();
47 void print(ostream& out) const;
48
49 // Public copy constructor and assignment operator
50 RefCnt(const RefCnt& obj);
51 RefCnt& operator=(const RefCnt& obj);
52
53 private:
54 // Private Methods
55
56 // Data Members (m_ prefix)
57 TYPE* m_data_ptr;
58 // int* m_count_ptr; // Not used yet
59 };
60
61 // Output operator declaration
62 template <class TYPE>
63 inline
64 ostream& operator<<(ostream& out, const RefCnt<TYPE>& obj);
65
66 // ******************* Definitions *******************
67
68 // Constructors
69 template <class TYPE>
70 inline
71 RefCnt<TYPE>::RefCnt()
72 {
73 m_data_ptr = NULL;
74 }
75
76 template <class TYPE>
77 inline
78 RefCnt<TYPE>::RefCnt(const TYPE& data)
79 {
80 m_data_ptr = data.clone();
81 m_data_ptr->setRefCnt(1);
82 }
83
84 template <class TYPE>
85 inline
86 RefCnt<TYPE>::~RefCnt()
87 {
88 freeRef();
89 }
90
91 template <class TYPE>
92 inline
93 void RefCnt<TYPE>::freeRef()
94 {
95 if (m_data_ptr != NULL) {
96 m_data_ptr->decRefCnt();
97 if (m_data_ptr->getRefCnt() == 0) {
98 m_data_ptr->destroy();
99 }
100 m_data_ptr = NULL;
101 }
102 }
103
104 template <class TYPE>
105 inline
106 void RefCnt<TYPE>::print(ostream& out) const
107 {
108 if (m_data_ptr == NULL) {
109 out << "[RefCnt: Null]";
110 } else {
111 out << "[RefCnt: ";
112 m_data_ptr->print(out);
113 out << "]";
114 }
115 }
116
117 // Copy constructor
118 template <class TYPE>
119 inline
120 RefCnt<TYPE>::RefCnt(const RefCnt<TYPE>& obj)
121 {
122 // m_data_ptr = obj.m_data_ptr->clone();
123 m_data_ptr = obj.m_data_ptr;
124
125 // Increment the reference count
126 if (m_data_ptr != NULL) {
127 m_data_ptr->incRefCnt();
128 }
129 }
130
131 // Assignment operator
132 template <class TYPE>
133 inline
134 RefCnt<TYPE>& RefCnt<TYPE>::operator=(const RefCnt<TYPE>& obj)
135 {
136 if (this == &obj) {
137 // If this is the case, do nothing
138 // assert(false);
139 } else {
140 freeRef();
141 m_data_ptr = obj.m_data_ptr;
142 if (m_data_ptr != NULL) {
143 m_data_ptr->incRefCnt();
144 }
145 }
146 return *this;
147 }
148
149
150 // Output operator definition
151 template <class TYPE>
152 inline
153 ostream& operator<<(ostream& out, const RefCnt<TYPE>& obj)
154 {
155 obj.print(out);
156 out << flush;
157 return out;
158 }
159
160
161
162 #endif //REFCNT_H