2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
33 #include "base/intmath.hh"
34 #include "base/misc.hh"
37 // Any types you use this class for must be covered here...
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(); };
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
62 SchedList(unsigned size);
65 void init(unsigned size);
67 T &operator[](unsigned offset);
80 SchedList<T>::SchedList(unsigned _size)
84 // size must be a power of two
85 if (!isPowerOf2(size)) {
86 panic("SchedList: size must be a power of two");
90 panic("SchedList: you don't want a list that small");
93 // calculate the bit mask for the modulo operation
96 data_array = new T[size];
99 panic("SchedList: could not allocate memory");
106 SchedList<T>::SchedList(void)
113 template<class T> void
114 SchedList<T>::init(unsigned _size)
119 // size must be a power of two
120 if (size & (size-1)) {
121 panic("SchedList: size must be a power of two");
125 panic("SchedList: you don't want a list that small");
128 // calculate the bit mask for the modulo operation
131 data_array = new T[size];
134 panic("SchedList: could not allocate memory");
142 template<class T> void
143 SchedList<T>::advance(void)
145 ClearEntry(data_array[position]);
147 // position = (++position % size);
148 position = ++position & mask;
152 template<class T> void
153 SchedList<T>::clear(void)
155 for (unsigned i=0; i<size; ++i) {
156 ClearEntry(data_array[i]);
164 SchedList<T>::operator[](unsigned offset)
166 if (offset >= size) {
167 panic("SchedList: can't access element beyond current pointer");
170 // unsigned p = (position + offset) % size;
171 unsigned p = (position + offset) & mask;
173 return data_array[p];