2 * Copyright (c) 2002-2003 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/misc.hh"
35 // Any types you use this class for must be covered here...
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(); };
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
60 SchedList(unsigned size);
63 void init(unsigned size);
65 T &operator[](unsigned offset);
78 SchedList<T>::SchedList(unsigned _size)
82 // size must be a power of two
83 if (size & (size-1)) {
84 panic("SchedList: size must be a power of two");
88 panic("SchedList: you don't want a list that small");
91 // calculate the bit mask for the modulo operation
94 data_array = new T[size];
97 panic("SchedList: could not allocate memory");
104 SchedList<T>::SchedList(void)
111 template<class T> void
112 SchedList<T>::init(unsigned _size)
117 // size must be a power of two
118 if (size & (size-1)) {
119 panic("SchedList: size must be a power of two");
123 panic("SchedList: you don't want a list that small");
126 // calculate the bit mask for the modulo operation
129 data_array = new T[size];
132 panic("SchedList: could not allocate memory");
140 template<class T> void
141 SchedList<T>::advance(void)
143 ClearEntry(data_array[position]);
145 // position = (++position % size);
146 position = ++position & mask;
150 template<class T> void
151 SchedList<T>::clear(void)
153 for (unsigned i=0; i<size; ++i) {
154 ClearEntry(data_array[i]);
162 SchedList<T>::operator[](unsigned offset)
164 if (offset >= size) {
165 panic("SchedList: can't access element beyond current pointer");
168 // unsigned p = (position + offset) % size;
169 unsigned p = (position + offset) & mask;
171 return data_array[p];