gem5  v21.1.0.2
timebuf.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004-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 __BASE_TIMEBUF_HH__
30 #define __BASE_TIMEBUF_HH__
31 
32 #include <cassert>
33 #include <cstring>
34 #include <vector>
35 
36 namespace gem5
37 {
38 
39 template <class T>
41 {
42  protected:
43  int past;
44  int future;
45  unsigned size;
46  int _id;
47 
48  char *data;
50  unsigned base;
51 
52  void valid(int idx) const
53  {
54  assert (idx >= -past && idx <= future);
55  }
56 
57  public:
58  friend class wire;
59  class wire
60  {
61  friend class TimeBuffer;
62  protected:
64  int index;
65 
66  void set(int idx)
67  {
68  buffer->valid(idx);
69  index = idx;
70  }
71 
72  wire(TimeBuffer<T> *buf, int i)
73  : buffer(buf), index(i)
74  { }
75 
76  public:
77  wire()
78  { }
79 
80  wire(const wire &i)
81  : buffer(i.buffer), index(i.index)
82  { }
83 
84  const wire &operator=(const wire &i)
85  {
86  buffer = i.buffer;
87  set(i.index);
88  return *this;
89  }
90 
91  const wire &operator=(int idx)
92  {
93  set(idx);
94  return *this;
95  }
96 
97  const wire &operator+=(int offset)
98  {
99  set(index + offset);
100  return *this;
101  }
102 
103  const wire &operator-=(int offset)
104  {
105  set(index - offset);
106  return *this;
107  }
108 
110  {
111  set(index + 1);
112  return *this;
113  }
114 
116  {
117  int i = index;
118  set(index + 1);
119  return wire(this, i);
120  }
121 
123  {
124  set(index - 1);
125  return *this;
126  }
127 
129  {
130  int i = index;
131  set(index - 1);
132  return wire(this, i);
133  }
134  T &operator*() const { return *buffer->access(index); }
135  T *operator->() const { return buffer->access(index); }
136  };
137 
138 
139  public:
140  TimeBuffer(int p, int f)
141  : past(p), future(f), size(past + future + 1),
142  data(new char[size * sizeof(T)]), index(size), base(0)
143  {
144  assert(past >= 0 && future >= 0);
145  char *ptr = data;
146  for (unsigned i = 0; i < size; i++) {
147  index[i] = ptr;
148  std::memset(ptr, 0, sizeof(T));
149  new (ptr) T;
150  ptr += sizeof(T);
151  }
152 
153  _id = -1;
154  }
155 
157  : data(NULL)
158  {
159  }
160 
162  {
163  for (unsigned i = 0; i < size; ++i)
164  (reinterpret_cast<T *>(index[i]))->~T();
165  delete [] data;
166  }
167 
168  void id(int id)
169  {
170  _id = id;
171  }
172 
173  int id()
174  {
175  return _id;
176  }
177 
178  void
180  {
181  if (++base >= size)
182  base = 0;
183 
184  int ptr = base + future;
185  if (ptr >= (int)size)
186  ptr -= size;
187  (reinterpret_cast<T *>(index[ptr]))->~T();
188  std::memset(index[ptr], 0, sizeof(T));
189  new (index[ptr]) T;
190  }
191 
192  protected:
193  //Calculate the index into this->index for element at position idx
194  //relative to now
195  inline int calculateVectorIndex(int idx) const
196  {
197  //Need more complex math here to calculate index.
198  valid(idx);
199 
200  int vector_index = idx + base;
201  if (vector_index >= (int)size) {
202  vector_index -= size;
203  } else if (vector_index < 0) {
204  vector_index += size;
205  }
206 
207  return vector_index;
208  }
209 
210  public:
211  T *access(int idx)
212  {
213  int vector_index = calculateVectorIndex(idx);
214 
215  return reinterpret_cast<T *>(index[vector_index]);
216  }
217 
218  T &operator[](int idx)
219  {
220  int vector_index = calculateVectorIndex(idx);
221 
222  return reinterpret_cast<T &>(*index[vector_index]);
223  }
224 
225  const T &operator[] (int idx) const
226  {
227  int vector_index = calculateVectorIndex(idx);
228 
229  return reinterpret_cast<const T &>(*index[vector_index]);
230  }
231 
232  wire getWire(int idx)
233  {
234  valid(idx);
235 
236  return wire(this, idx);
237  }
238 
240  {
241  return wire(this, 0);
242  }
243 
244  unsigned getSize()
245  {
246  return size;
247  }
248 };
249 
250 } // namespace gem5
251 
252 #endif // __BASE_TIMEBUF_HH__
gem5::TimeBuffer::getSize
unsigned getSize()
Definition: timebuf.hh:244
gem5::TimeBuffer::~TimeBuffer
~TimeBuffer()
Definition: timebuf.hh:161
gem5::TimeBuffer::getWire
wire getWire(int idx)
Definition: timebuf.hh:232
gem5::TimeBuffer::data
char * data
Definition: timebuf.hh:48
gem5::TimeBuffer::zero
wire zero()
Definition: timebuf.hh:239
gem5::TimeBuffer::id
int id()
Definition: timebuf.hh:173
gem5::ArmISA::f
Bitfield< 6 > f
Definition: misc_types.hh:67
gem5::TimeBuffer::calculateVectorIndex
int calculateVectorIndex(int idx) const
Definition: timebuf.hh:195
gem5::TimeBuffer::wire::index
int index
Definition: timebuf.hh:64
gem5::TimeBuffer::size
unsigned size
Definition: timebuf.hh:45
std::vector< char * >
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::TimeBuffer::valid
void valid(int idx) const
Definition: timebuf.hh:52
gem5::TimeBuffer::wire::operator++
wire & operator++()
Definition: timebuf.hh:109
gem5::TimeBuffer::wire::operator=
const wire & operator=(int idx)
Definition: timebuf.hh:91
gem5::TimeBuffer
Definition: timebuf.hh:40
gem5::TimeBuffer::wire::operator-=
const wire & operator-=(int offset)
Definition: timebuf.hh:103
gem5::TimeBuffer::wire::operator=
const wire & operator=(const wire &i)
Definition: timebuf.hh:84
gem5::TimeBuffer::past
int past
Definition: timebuf.hh:43
gem5::TimeBuffer::TimeBuffer
TimeBuffer()
Definition: timebuf.hh:156
gem5::TimeBuffer::wire::wire
wire(TimeBuffer< T > *buf, int i)
Definition: timebuf.hh:72
gem5::TimeBuffer::wire::wire
wire()
Definition: timebuf.hh:77
gem5::TimeBuffer::wire::operator->
T * operator->() const
Definition: timebuf.hh:135
gem5::TimeBuffer::wire::wire
wire(const wire &i)
Definition: timebuf.hh:80
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::TimeBuffer::wire::operator*
T & operator*() const
Definition: timebuf.hh:134
gem5::TimeBuffer::wire::buffer
TimeBuffer< T > * buffer
Definition: timebuf.hh:63
gem5::TimeBuffer::wire::operator++
wire & operator++(int)
Definition: timebuf.hh:115
gem5::ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:144
gem5::TimeBuffer::wire::operator--
wire & operator--(int)
Definition: timebuf.hh:128
gem5::TimeBuffer::wire::operator+=
const wire & operator+=(int offset)
Definition: timebuf.hh:97
gem5::TimeBuffer::access
T * access(int idx)
Definition: timebuf.hh:211
gem5::TimeBuffer::wire::set
void set(int idx)
Definition: timebuf.hh:66
gem5::TimeBuffer::index
std::vector< char * > index
Definition: timebuf.hh:49
gem5::TimeBuffer::advance
void advance()
Definition: timebuf.hh:179
gem5::TimeBuffer::_id
int _id
Definition: timebuf.hh:46
gem5::TimeBuffer::id
void id(int id)
Definition: timebuf.hh:168
gem5::TimeBuffer::TimeBuffer
TimeBuffer(int p, int f)
Definition: timebuf.hh:140
gem5::TimeBuffer::future
int future
Definition: timebuf.hh:44
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::TimeBuffer::wire::operator--
wire & operator--()
Definition: timebuf.hh:122
gem5::TimeBuffer::wire
friend class wire
Definition: timebuf.hh:58
gem5::TimeBuffer::operator[]
T & operator[](int idx)
Definition: timebuf.hh:218
gem5::TimeBuffer::wire
Definition: timebuf.hh:59
gem5::TimeBuffer::base
unsigned base
Definition: timebuf.hh:50

Generated on Tue Sep 21 2021 12:25:09 for gem5 by doxygen 1.8.17