CheckerCPU: Re-factor CheckerCPU to be compatible with current gem5
[gem5.git] / src / cpu / sched_list.hh
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: Steve Raasch
29 */
30
31 #ifndef SCHED_LIST_HH
32 #define SCHED_LIST_HH
33
34 #include <list>
35
36 #include "base/intmath.hh"
37 #include "base/misc.hh"
38
39 // Any types you use this class for must be covered here...
40 namespace {
41 void ClearEntry(int &i) { i = 0; };
42 void ClearEntry(unsigned &i) { i = 0; };
43 void ClearEntry(double &i) { i = 0; };
44 template <class T> void ClearEntry(std::list<T> &l) { l.clear(); };
45 };
46
47
48 //
49 // this is a special list type that allows the user to insert elements at a
50 // specified positive offset from the "current" element, but only allow them
51 // be extracted from the "current" element
52 //
53
54
55 template <class T>
56 class SchedList
57 {
58 T *data_array;
59 unsigned position;
60 unsigned size;
61 unsigned mask;
62
63 public:
64 SchedList(unsigned size);
65 SchedList(void);
66
67 void init(unsigned size);
68
69 T &operator[](unsigned offset);
70
71 void advance(void);
72
73 void clear(void);
74 };
75
76
77
78 //
79 // Constructor
80 //
81 template<class T>
82 SchedList<T>::SchedList(unsigned _size)
83 {
84 size = _size;
85
86 // size must be a power of two
87 if (!isPowerOf2(size)) {
88 panic("SchedList: size must be a power of two");
89 }
90
91 if (size < 2) {
92 panic("SchedList: you don't want a list that small");
93 }
94
95 // calculate the bit mask for the modulo operation
96 mask = size - 1;
97
98 data_array = new T[size];
99
100 if (!data_array) {
101 panic("SchedList: could not allocate memory");
102 }
103
104 clear();
105 }
106
107 template<class T>
108 SchedList<T>::SchedList(void)
109 {
110 data_array = 0;
111 size = 0;
112 }
113
114
115 template<class T> void
116 SchedList<T>::init(unsigned _size)
117 {
118 size = _size;
119
120 if (!data_array) {
121 // size must be a power of two
122 if (size & (size-1)) {
123 panic("SchedList: size must be a power of two");
124 }
125
126 if (size < 2) {
127 panic("SchedList: you don't want a list that small");
128 }
129
130 // calculate the bit mask for the modulo operation
131 mask = size - 1;
132
133 data_array = new T[size];
134
135 if (!data_array) {
136 panic("SchedList: could not allocate memory");
137 }
138
139 clear();
140 }
141 }
142
143
144 template<class T> void
145 SchedList<T>::advance(void)
146 {
147 ClearEntry(data_array[position]);
148
149 // position = (++position % size);
150 position = ++position & mask;
151 }
152
153
154 template<class T> void
155 SchedList<T>::clear(void)
156 {
157 for (unsigned i=0; i<size; ++i) {
158 ClearEntry(data_array[i]);
159 }
160
161 position = 0;
162 }
163
164
165 template<class T> T&
166 SchedList<T>::operator[](unsigned offset)
167 {
168 if (offset >= size) {
169 panic("SchedList: can't access element beyond current pointer");
170 }
171
172 // unsigned p = (position + offset) % size;
173 unsigned p = (position + offset) & mask;
174
175 return data_array[p];
176 }
177
178
179
180 #endif