gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
circlebuf.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015,2017-2018 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * Authors: Andreas Sandberg
38  */
39 
40 #ifndef __BASE_CIRCLEBUF_HH__
41 #define __BASE_CIRCLEBUF_HH__
42 
43 #include <algorithm>
44 #include <cassert>
45 #include <vector>
46 
47 #include "base/circular_queue.hh"
48 #include "base/logging.hh"
49 #include "sim/serialize.hh"
50 
58 template<typename T>
59 class CircleBuf : public CircularQueue<T>
60 {
61  public:
62  explicit CircleBuf(size_t size)
63  : CircularQueue<T>(size) {}
71 
78  template <class OutputIterator>
79  void peek(OutputIterator out, size_t len) const {
80  peek(out, 0, len);
81  }
82 
90  template <class OutputIterator>
91  void peek(OutputIterator out, off_t offset, size_t len) const {
92  panic_if(offset + len > size(),
93  "Trying to read past end of circular buffer.\n");
94 
95  std::copy(begin() + offset, begin() + offset + len, out);
96  }
97 
104  template <class OutputIterator>
105  void read(OutputIterator out, size_t len) {
106  peek(out, len);
107  pop_front(len);
108  }
109 
116  template <class InputIterator>
117  void write(InputIterator in, size_t len) {
118  // Writes that are larger than the backing store are allowed,
119  // but only the last part of the buffer will be written.
120  if (len > capacity()) {
121  in += len - capacity();
122  len = capacity();
123  }
124 
125  std::copy(in, in + len, end());
126  advance_tail(len);
127  }
128 };
129 
141 template<typename T>
142 class Fifo
143 {
144  public:
145  typedef T value_type;
146 
147  public:
148  Fifo(size_t size)
149  : buf(size) {}
150 
151  bool empty() const { return buf.empty(); }
152  size_t size() const { return buf.size(); }
153  size_t capacity() const { return buf.capacity(); }
154 
155  void flush() { buf.flush(); }
156 
157  template <class OutputIterator>
158  void peek(OutputIterator out, size_t len) const { buf.peek(out, len); }
159  template <class OutputIterator>
160  void read(OutputIterator out, size_t len) { buf.read(out, len); }
161 
162  template <class InputIterator>
163  void write(InputIterator in, size_t len) {
164  panic_if(size() + len > capacity(),
165  "Trying to overfill FIFO buffer.\n");
166  buf.write(in, len);
167  }
168 
169  private:
171 };
172 
173 
174 template <typename T>
175 void
176 arrayParamOut(CheckpointOut &cp, const std::string &name,
177  const CircleBuf<T> &param)
178 {
179  std::vector<T> temp(param.size());
180  param.peek(temp.begin(), temp.size());
181  arrayParamOut(cp, name, temp);
182 }
183 
184 template <typename T>
185 void
186 arrayParamIn(CheckpointIn &cp, const std::string &name,
187  CircleBuf<T> &param)
188 {
189  std::vector<T> temp;
190  arrayParamIn(cp, name, temp);
191 
192  param.flush();
193  param.write(temp.cbegin(), temp.size());
194 }
195 
196 template <typename T>
197 void
198 arrayParamOut(CheckpointOut &cp, const std::string &name,
199  const Fifo<T> &param)
200 {
201  std::vector<T> temp(param.size());
202  param.peek(temp.begin(), temp.size());
203  arrayParamOut(cp, name, temp);
204 }
205 
206 template <typename T>
207 void
208 arrayParamIn(CheckpointIn &cp, const std::string &name,
209  Fifo<T> &param)
210 {
211  std::vector<T> temp;
212  arrayParamIn(cp, name, temp);
213 
214  fatal_if(param.capacity() < temp.size(),
215  "Trying to unserialize data into too small FIFO\n");
216 
217  param.flush();
218  param.write(temp.cbegin(), temp.size());
219 }
220 
221 #endif // __BASE_CIRCLEBUF_HH__
void write(InputIterator in, size_t len)
Definition: circlebuf.hh:163
iterator begin()
Iterators.
const std::string & name()
Definition: trace.cc:54
Circular buffer backed by a vector though a CircularQueue.
Definition: circlebuf.hh:59
Bitfield< 23, 0 > offset
Definition: types.hh:154
uint32_t size() const
Definition: cprintf.cc:42
T value_type
Definition: circlebuf.hh:145
size_t capacity() const
STL vector class.
Definition: stl.hh:40
void peek(OutputIterator out, off_t offset, size_t len) const
Copy buffer contents without advancing the read pointer.
Definition: circlebuf.hh:91
void advance_tail()
Increases the tail by one.
iterator end()
CircleBuf(size_t size)
Definition: circlebuf.hh:62
void peek(OutputIterator out, size_t len) const
Copy buffer contents without advancing the read pointer.
Definition: circlebuf.hh:79
Bitfield< 18, 16 > len
CircleBuf< value_type > buf
Definition: circlebuf.hh:170
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
void write(InputIterator in, size_t len)
Add elements to the end of the ring buffers and advance.
Definition: circlebuf.hh:117
void arrayParamOut(CheckpointOut &cp, const std::string &name, const CircleBuf< T > &param)
Definition: circlebuf.hh:176
bool empty() const
Definition: circlebuf.hh:151
void pop_front(size_t num_elem=1)
Circularly increase the head pointer.
void read(OutputIterator out, size_t len)
Copy buffer contents and advance the read pointer.
Definition: circlebuf.hh:105
size_t capacity() const
Definition: circlebuf.hh:153
Fifo(size_t size)
Definition: circlebuf.hh:148
void flush()
Remove all the elements in the queue.
std::ostream CheckpointOut
Definition: serialize.hh:68
Circular queue.
size_t size() const
Definition: circlebuf.hh:152
void peek(OutputIterator out, size_t len) const
Definition: circlebuf.hh:158
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > &param)
Definition: circlebuf.hh:186
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:185
void read(OutputIterator out, size_t len)
Definition: circlebuf.hh:160
Simple FIFO implementation backed by a circular buffer.
Definition: circlebuf.hh:142
void flush()
Definition: circlebuf.hh:155

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13