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