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