gem5  v20.1.0.0
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 
38 #ifndef __BASE_CIRCLEBUF_HH__
39 #define __BASE_CIRCLEBUF_HH__
40 
41 #include <algorithm>
42 #include <cassert>
43 #include <vector>
44 
45 #include "base/circular_queue.hh"
46 #include "base/logging.hh"
47 #include "sim/serialize.hh"
48 
56 template<typename T>
57 class CircleBuf : public CircularQueue<T>
58 {
59  public:
60  explicit CircleBuf(size_t size)
61  : CircularQueue<T>(size) {}
69 
76  template <class OutputIterator>
77  void peek(OutputIterator out, size_t len) const {
78  peek(out, 0, len);
79  }
80 
88  template <class OutputIterator>
89  void peek(OutputIterator out, off_t offset, size_t len) const {
90  panic_if(offset + len > size(),
91  "Trying to read past end of circular buffer.\n");
92 
93  std::copy(begin() + offset, begin() + offset + len, out);
94  }
95 
102  template <class OutputIterator>
103  void read(OutputIterator out, size_t len) {
104  peek(out, len);
105  pop_front(len);
106  }
107 
114  template <class InputIterator>
115  void write(InputIterator in, size_t len) {
116  // Writes that are larger than the backing store are allowed,
117  // but only the last part of the buffer will be written.
118  if (len > capacity()) {
119  in += len - capacity();
120  len = capacity();
121  }
122 
123  std::copy(in, in + len, end());
124  advance_tail(len);
125  }
126 };
127 
139 template<typename T>
140 class Fifo
141 {
142  public:
143  typedef T value_type;
144 
145  public:
146  Fifo(size_t size)
147  : buf(size) {}
148 
149  bool empty() const { return buf.empty(); }
150  size_t size() const { return buf.size(); }
151  size_t capacity() const { return buf.capacity(); }
152 
153  void flush() { buf.flush(); }
154 
155  template <class OutputIterator>
156  void peek(OutputIterator out, size_t len) const { buf.peek(out, len); }
157  template <class OutputIterator>
158  void read(OutputIterator out, size_t len) { buf.read(out, len); }
159 
160  template <class InputIterator>
161  void write(InputIterator in, size_t len) {
162  panic_if(size() + len > capacity(),
163  "Trying to overfill FIFO buffer.\n");
164  buf.write(in, len);
165  }
166 
167  private:
169 };
170 
171 
172 template <typename T>
173 void
174 arrayParamOut(CheckpointOut &cp, const std::string &name,
175  const CircleBuf<T> &param)
176 {
177  std::vector<T> temp(param.size());
178  param.peek(temp.begin(), temp.size());
179  arrayParamOut(cp, name, temp);
180 }
181 
182 template <typename T>
183 void
184 arrayParamIn(CheckpointIn &cp, const std::string &name,
185  CircleBuf<T> &param)
186 {
187  std::vector<T> temp;
188  arrayParamIn(cp, name, temp);
189 
190  param.flush();
191  param.write(temp.cbegin(), temp.size());
192 }
193 
194 template <typename T>
195 void
196 arrayParamOut(CheckpointOut &cp, const std::string &name,
197  const Fifo<T> &param)
198 {
199  std::vector<T> temp(param.size());
200  param.peek(temp.begin(), temp.size());
201  arrayParamOut(cp, name, temp);
202 }
203 
204 template <typename T>
205 void
206 arrayParamIn(CheckpointIn &cp, const std::string &name,
207  Fifo<T> &param)
208 {
209  std::vector<T> temp;
210  arrayParamIn(cp, name, temp);
211 
212  fatal_if(param.capacity() < temp.size(),
213  "Trying to unserialize data into too small FIFO\n");
214 
215  param.flush();
216  param.write(temp.cbegin(), temp.size());
217 }
218 
219 #endif // __BASE_CIRCLEBUF_HH__
Fifo::Fifo
Fifo(size_t size)
Definition: circlebuf.hh:146
CircularQueue::advance_tail
void advance_tail()
Increases the tail by one.
Definition: circular_queue.hh:704
serialize.hh
Fifo::capacity
size_t capacity() const
Definition: circlebuf.hh:151
CircleBuf::peek
void peek(OutputIterator out, off_t offset, size_t len) const
Copy buffer contents without advancing the read pointer.
Definition: circlebuf.hh:89
CircularQueue
Circular queue.
Definition: circular_queue.hh:86
CircleBuf::CircleBuf
CircleBuf(size_t size)
Definition: circlebuf.hh:60
CircularQueue::begin
iterator begin()
Iterators.
Definition: circular_queue.hh:755
std::vector
STL vector class.
Definition: stl.hh:37
CircularQueue::flush
void flush()
Remove all the elements in the queue.
Definition: circular_queue.hh:536
Fifo::flush
void flush()
Definition: circlebuf.hh:153
Fifo::write
void write(InputIterator in, size_t len)
Definition: circlebuf.hh:161
Fifo::read
void read(OutputIterator out, size_t len)
Definition: circlebuf.hh:158
cp
Definition: cprintf.cc:40
Fifo::size
size_t size() const
Definition: circlebuf.hh:150
circular_queue.hh
Fifo
Simple FIFO implementation backed by a circular buffer.
Definition: circlebuf.hh:140
Fifo::peek
void peek(OutputIterator out, size_t len) const
Definition: circlebuf.hh:156
CircularQueue::size
uint32_t size() const
Definition: circular_queue.hh:633
CircularQueue::end
iterator end()
Definition: circular_queue.hh:784
arrayParamOut
void arrayParamOut(CheckpointOut &cp, const std::string &name, const CircleBuf< T > &param)
Definition: circlebuf.hh:174
Fifo::value_type
T value_type
Definition: circlebuf.hh:143
name
const std::string & name()
Definition: trace.cc:50
CircleBuf::write
void write(InputIterator in, size_t len)
Add elements to the end of the ring buffers and advance.
Definition: circlebuf.hh:115
CircularQueue::pop_front
void pop_front(size_t num_elem=1)
Circularly increase the head pointer.
Definition: circular_queue.hh:663
CircleBuf::peek
void peek(OutputIterator out, size_t len) const
Copy buffer contents without advancing the read pointer.
Definition: circlebuf.hh:77
CircleBuf::read
void read(OutputIterator out, size_t len)
Copy buffer contents and advance the read pointer.
Definition: circlebuf.hh:103
CircularQueue::empty
bool empty() const
Is the queue empty?
Definition: circular_queue.hh:734
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
CircularQueue::capacity
size_t capacity() const
Definition: circular_queue.hh:628
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
logging.hh
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
arrayParamIn
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > &param)
Definition: circlebuf.hh:184
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:219
CheckpointIn
Definition: serialize.hh:67
CircleBuf
Circular buffer backed by a vector though a CircularQueue.
Definition: circlebuf.hh:57
Fifo::empty
bool empty() const
Definition: circlebuf.hh:149
Fifo::buf
CircleBuf< value_type > buf
Definition: circlebuf.hh:168
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17