2 * Copyright (c) 2015,2017-2018 ARM Limited
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.
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.
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.
37 * Authors: Andreas Sandberg
40 #ifndef __BASE_CIRCLEBUF_HH__
41 #define __BASE_CIRCLEBUF_HH__
47 #include "base/circular_queue.hh"
48 #include "base/logging.hh"
49 #include "sim/serialize.hh"
52 * Circular buffer backed by a vector though a CircularQueue.
54 * The data in the cricular buffer is stored in a standard
59 class CircleBuf : public CircularQueue<T>
62 explicit CircleBuf(size_t size)
63 : CircularQueue<T>(size) {}
64 using CircularQueue<T>::empty;
65 using CircularQueue<T>::size;
66 using CircularQueue<T>::capacity;
67 using CircularQueue<T>::begin;
68 using CircularQueue<T>::end;
69 using CircularQueue<T>::pop_front;
70 using CircularQueue<T>::advance_tail;
73 * Copy buffer contents without advancing the read pointer
75 * @param out Output iterator/pointer
76 * @param len Number of elements to copy
78 template <class OutputIterator>
79 void peek(OutputIterator out, size_t len) const {
84 * Copy buffer contents without advancing the read pointer
86 * @param out Output iterator/pointer
87 * @param offset Offset into the ring buffer
88 * @param len Number of elements to copy
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");
95 std::copy(begin() + offset, begin() + offset + len, out);
99 * Copy buffer contents and advance the read pointer
101 * @param out Output iterator/pointer
102 * @param len Number of elements to read
104 template <class OutputIterator>
105 void read(OutputIterator out, size_t len) {
111 * Add elements to the end of the ring buffers and advance.
113 * @param in Input iterator/pointer
114 * @param len Number of elements to read
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();
125 std::copy(in, in + len, end());
131 * Simple FIFO implementation backed by a circular buffer.
133 * This class provides the same basic functionallity as the circular
134 * buffer with the folling differences:
136 * <li>Writes are checked to ensure that overflows can't happen.
137 * <li>Unserialization ensures that the data in the checkpoint fits
145 typedef T value_type;
151 bool empty() const { return buf.empty(); }
152 size_t size() const { return buf.size(); }
153 size_t capacity() const { return buf.capacity(); }
155 void flush() { buf.flush(); }
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); }
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");
170 CircleBuf<value_type> buf;
174 template <typename T>
176 arrayParamOut(CheckpointOut &cp, const std::string &name,
177 const CircleBuf<T> ¶m)
179 std::vector<T> temp(param.size());
180 param.peek(temp.begin(), temp.size());
181 arrayParamOut(cp, name, temp);
184 template <typename T>
186 arrayParamIn(CheckpointIn &cp, const std::string &name,
190 arrayParamIn(cp, name, temp);
193 param.write(temp.cbegin(), temp.size());
196 template <typename T>
198 arrayParamOut(CheckpointOut &cp, const std::string &name,
199 const Fifo<T> ¶m)
201 std::vector<T> temp(param.size());
202 param.peek(temp.begin(), temp.size());
203 arrayParamOut(cp, name, temp);
206 template <typename T>
208 arrayParamIn(CheckpointIn &cp, const std::string &name,
212 arrayParamIn(cp, name, temp);
214 fatal_if(param.capacity() < temp.size(),
215 "Trying to unserialize data into too small FIFO\n");
218 param.write(temp.cbegin(), temp.size());
221 #endif // __BASE_CIRCLEBUF_HH__