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.
28 * Authors: Steve Raasch
36 #include "base/intmath.hh"
37 #include "base/misc.hh"
39 // Any types you use this class for must be covered here...
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(); };
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
64 SchedList(unsigned size);
67 void init(unsigned size);
69 T &operator[](unsigned offset);
82 SchedList<T>::SchedList(unsigned _size)
86 // size must be a power of two
87 if (!isPowerOf2(size)) {
88 panic("SchedList: size must be a power of two");
92 panic("SchedList: you don't want a list that small");
95 // calculate the bit mask for the modulo operation
98 data_array = new T[size];
101 panic("SchedList: could not allocate memory");
108 SchedList<T>::SchedList(void)
115 template<class T> void
116 SchedList<T>::init(unsigned _size)
121 // size must be a power of two
122 if (size & (size-1)) {
123 panic("SchedList: size must be a power of two");
127 panic("SchedList: you don't want a list that small");
130 // calculate the bit mask for the modulo operation
133 data_array = new T[size];
136 panic("SchedList: could not allocate memory");
144 template<class T> void
145 SchedList<T>::advance(void)
147 ClearEntry(data_array[position]);
149 // position = (++position % size);
150 position = ++position & mask;
154 template<class T> void
155 SchedList<T>::clear(void)
157 for (unsigned i=0; i<size; ++i) {
158 ClearEntry(data_array[i]);
166 SchedList<T>::operator[](unsigned offset)
168 if (offset >= size) {
169 panic("SchedList: can't access element beyond current pointer");
172 // unsigned p = (position + offset) % size;
173 unsigned p = (position + offset) & mask;
175 return data_array[p];