circlebuf.hh (2665:a124942bacb8) circlebuf.hh (11008:be3b60b52b31)
1/*
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
2 * Copyright (c) 2015 ARM Limited
3 * All rights reserved
4 *
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 *
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

--- 7 unchanged lines hidden (view full) ---

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 *
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

--- 7 unchanged lines hidden (view full) ---

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 *
28 * Authors: Nathan Binkert
37 * Authors: Andreas Sandberg
29 */
30
38 */
39
31#ifndef __CIRCLEBUF_HH__
32#define __CIRCLEBUF_HH__
40#ifndef __BASE_CIRCLEBUF_HH__
41#define __BASE_CIRCLEBUF_HH__
33
42
34#include <iosfwd>
43#include <algorithm>
44#include <cassert>
45#include <vector>
35
46
47#include "base/misc.hh"
48#include "sim/serialize.hh"
49
50/**
51 * Circular buffer backed by a vector
52 *
53 * The data in the cricular buffer is stored in a standard
54 * vector. _start designates the first element in the buffer and _stop
55 * points to the last element + 1 (i.e., the position of the next
56 * insertion). The _stop index may be outside the range of the backing
57 * store, which means that the actual index must be calculated as
58 * _stop % capacity.
59 *
60 * Invariants:
61 * <ul>
62 * <li>_start <= _stop
63 * <li>_start < capacity
64 * <li>_stop < 2 * capacity
65 * </ul>
66 */
67template<typename T>
36class CircleBuf
37{
68class CircleBuf
69{
70 public:
71 typedef T value_type;
72
73 public:
74 explicit CircleBuf(size_t size)
75 : buf(size), _start(0), _stop(0) {}
76
77 /** Is the buffer empty? */
78 bool empty() const { return _stop == _start; }
79 /**
80 * Return the maximum number of elements that can be stored in
81 * the buffer at any one time.
82 */
83 size_t capacity() const { return buf.size(); }
84 /** Return the number of elements stored in the buffer. */
85 size_t size() const { return _stop - _start; }
86
87 /**
88 * Remove all the elements in the buffer.
89 *
90 * Note: This does not actually remove elements from the backing
91 * store.
92 */
93 void flush() {
94 _start = 0;
95 _stop = 0;
96 }
97
98 /**
99 * Copy buffer contents without advancing the read pointer
100 *
101 * @param out Output iterator/pointer
102 * @param len Number of elements to copy
103 */
104 template <class OutputIterator>
105 void peek(OutputIterator out, size_t len) const {
106 panic_if(len > size(),
107 "Trying to read past end of circular buffer.\n");
108
109 if (_start + len <= buf.size()) {
110 std::copy(buf.begin() + _start,
111 buf.begin() + _start + len,
112 out);
113 } else {
114 const size_t head_size(buf.size() - _start);
115 const size_t tail_size(len - head_size);
116 std::copy(buf.begin() + _start, buf.end(),
117 out);
118 std::copy(buf.begin(), buf.begin() + tail_size,
119 out + head_size);
120 }
121 }
122
123
124 /**
125 * Copy buffer contents and advance the read pointer
126 *
127 * @param out Output iterator/pointer
128 * @param len Number of elements to read
129 */
130 template <class OutputIterator>
131 void read(OutputIterator out, size_t len) {
132 peek(out, len);
133
134 _start += len;
135 normalize();
136 }
137
138 /**
139 * Add elements to the end of the ring buffers and advance.
140 *
141 * @param in Input iterator/pointer
142 * @param len Number of elements to read
143 */
144 template <class InputIterator>
145 void write(InputIterator in, size_t len) {
146 // Writes that are larger than the backing store are allowed,
147 // but only the last part of the buffer will be written.
148 if (len > buf.size()) {
149 in += len - buf.size();
150 len = buf.size();
151 }
152
153 const size_t next(_stop % buf.size());
154 const size_t head_len(std::min(buf.size() - next, len));
155
156 std::copy(in, in + head_len, buf.begin() + next);
157 std::copy(in + head_len, in + len, buf.begin());
158
159 _stop += len;
160 // We may have written past the old _start pointer. Readjust
161 // the _start pointer to remove the oldest entries in that
162 // case.
163 if (size() > buf.size())
164 _start = _stop - buf.size();
165
166 normalize();
167 }
168
38 protected:
169 protected:
39 char *_buf;
40 bool _rollover;
41 int _buflen;
42 int _size;
43 int _start;
44 int _stop;
170 /**
171 * Normalize the start and stop pointers to ensure that pointer
172 * invariants hold after updates.
173 */
174 void normalize() {
175 if (_start >= buf.size()) {
176 _stop -= buf.size();
177 _start -= buf.size();
178 }
45
179
180 assert(_start < buf.size());
181 assert(_stop < 2 * buf.size());
182 assert(_start <= _stop);
183 }
184
185 protected:
186 std::vector<value_type> buf;
187 size_t _start;
188 size_t _stop;
189
190};
191
192
193/**
194 * Simple FIFO implementation backed by a circular buffer.
195 *
196 * This class provides the same basic functionallity as the circular
197 * buffer with the folling differences:
198 * <ul>
199 * <li>Writes are checked to ensure that overflows can't happen.
200 * <li>Unserialization ensures that the data in the checkpoint fits
201 * in the buffer.
202 * </ul>
203 */
204template<typename T>
205class Fifo
206{
46 public:
207 public:
47 explicit CircleBuf(int l);
48 ~CircleBuf();
208 typedef T value_type;
49
209
50 bool empty() const { return _size == 0; }
51 int size() const { return _size; }
52 void dump();
53 void flush();
54 void read(char *b, int len);
55 void read(int fd, int len);
56 void read(int fd);
57 void read(std::ostream &out);
58 void readall(int fd);
59 void write(char b);
60 void write(const char *b);
61 void write(const char *b, int len);
210 public:
211 Fifo(size_t size)
212 : buf(size) {}
213
214 bool empty() const { return buf.empty(); }
215 size_t size() const { return buf.size(); }
216 size_t capacity() const { return buf.capacity(); }
217
218 void flush() { buf.flush(); }
219
220 template <class OutputIterator>
221 void peek(OutputIterator out, size_t len) const { buf.peek(out, len); }
222 template <class OutputIterator>
223 void read(OutputIterator out, size_t len) { buf.read(out, len); }
224
225 template <class InputIterator>
226 void write(InputIterator in, size_t len) {
227 panic_if(size() + len > capacity(),
228 "Trying to overfill FIFO buffer.\n");
229 buf.write(in, len);
230 }
231
232 private:
233 CircleBuf<value_type> buf;
62};
63
234};
235
64#endif // __CIRCLEBUF_HH__
236
237template <typename T>
238static void
239arrayParamOut(CheckpointOut &cp, const std::string &name,
240 const CircleBuf<T> &param)
241{
242 std::vector<T> temp(param.size());
243 param.peek(temp.begin(), temp.size());
244 arrayParamOut(cp, name, temp);
245}
246
247template <typename T>
248static void
249arrayParamIn(CheckpointIn &cp, const std::string &name,
250 CircleBuf<T> &param)
251{
252 std::vector<T> temp;
253 arrayParamIn(cp, name, temp);
254
255 param.flush();
256 param.write(temp.cbegin(), temp.size());
257}
258
259template <typename T>
260static void
261arrayParamOut(CheckpointOut &cp, const std::string &name,
262 const Fifo<T> &param)
263{
264 std::vector<T> temp(param.size());
265 param.peek(temp.begin(), temp.size());
266 arrayParamOut(cp, name, temp);
267}
268
269template <typename T>
270static void
271arrayParamIn(CheckpointIn &cp, const std::string &name,
272 Fifo<T> &param)
273{
274 std::vector<T> temp;
275 arrayParamIn(cp, name, temp);
276
277 fatal_if(param.capacity() < temp.size(),
278 "Trying to unserialize data into too small FIFO\n");
279
280 param.flush();
281 param.write(temp.cbegin(), temp.size());
282}
283
284#endif // __BASE_CIRCLEBUF_HH__