Merge ktlim@zizzer:/bk/newmem
[gem5.git] / src / base / circlebuf.cc
1 /*
2 * Copyright (c) 2002-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 * Authors: Nathan Binkert
29 */
30
31 #include <algorithm>
32 #include <string>
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "base/circlebuf.hh"
39 #include "base/cprintf.hh"
40 #include "base/intmath.hh"
41
42 using namespace std;
43
44 CircleBuf::CircleBuf(int l)
45 : _rollover(false), _buflen(l), _size(0), _start(0), _stop(0)
46 {
47 _buf = new char[_buflen];
48 }
49
50 CircleBuf::~CircleBuf()
51 {
52 if (_buf)
53 delete [] _buf;
54 }
55
56 void
57 CircleBuf::dump()
58 {
59 cprintf("start = %10d, stop = %10d, buflen = %10d\n",
60 _start, _stop, _buflen);
61 fflush(stdout);
62 ::write(STDOUT_FILENO, _buf, _buflen);
63 ::write(STDOUT_FILENO, "<\n", 2);
64 }
65
66 void
67 CircleBuf::flush()
68 {
69 _start = 0;
70 _stop = 0;
71 _rollover = false;
72 }
73
74 void
75 CircleBuf::read(char *b, int len)
76 {
77 _size -= len;
78 if (_size < 0)
79 _size = 0;
80
81 if (_stop > _start) {
82 len = min(len, _stop - _start);
83 memcpy(b, _buf + _start, len);
84 _start += len;
85 }
86 else {
87 int endlen = _buflen - _start;
88 if (endlen > len) {
89 memcpy(b, _buf + _start, len);
90 _start += len;
91 }
92 else {
93 memcpy(b, _buf + _start, endlen);
94 _start = min(len - endlen, _stop);
95 memcpy(b + endlen, _buf, _start);
96 }
97 }
98 }
99
100 void
101 CircleBuf::read(int fd, int len)
102 {
103 _size -= len;
104 if (_size < 0)
105 _size = 0;
106
107 if (_stop > _start) {
108 len = min(len, _stop - _start);
109 ::write(fd, _buf + _start, len);
110 _start += len;
111 }
112 else {
113 int endlen = _buflen - _start;
114 if (endlen > len) {
115 ::write(fd, _buf + _start, len);
116 _start += len;
117 }
118 else {
119 ::write(fd, _buf + _start, endlen);
120 _start = min(len - endlen, _stop);
121 ::write(fd, _buf, _start);
122 }
123 }
124 }
125
126 void
127 CircleBuf::read(int fd)
128 {
129 _size = 0;
130
131 if (_stop > _start) {
132 ::write(fd, _buf + _start, _stop - _start);
133 }
134 else {
135 ::write(fd, _buf + _start, _buflen - _start);
136 ::write(fd, _buf, _stop);
137 }
138
139 _start = _stop;
140 }
141
142 void
143 CircleBuf::read(ostream &out)
144 {
145 _size = 0;
146
147 if (_stop > _start) {
148 out.write(_buf + _start, _stop - _start);
149 }
150 else {
151 out.write(_buf + _start, _buflen - _start);
152 out.write(_buf, _stop);
153 }
154
155 _start = _stop;
156 }
157
158 void
159 CircleBuf::readall(int fd)
160 {
161 if (_rollover)
162 ::write(fd, _buf + _stop, _buflen - _stop);
163
164 ::write(fd, _buf, _stop);
165 _start = _stop;
166 }
167
168 void
169 CircleBuf::write(char b)
170 {
171 write(&b, 1);
172 }
173
174 void
175 CircleBuf::write(const char *b)
176 {
177 write(b, strlen(b));
178 }
179
180 void
181 CircleBuf::write(const char *b, int len)
182 {
183 if (len <= 0)
184 return;
185
186 _size += len;
187 if (_size > _buflen)
188 _size = _buflen;
189
190 int old_start = _start;
191 int old_stop = _stop;
192
193 if (len >= _buflen) {
194 _start = 0;
195 _stop = _buflen;
196 _rollover = true;
197 memcpy(_buf, b + (len - _buflen), _buflen);
198 return;
199 }
200
201 if (_stop + len <= _buflen) {
202 memcpy(_buf + _stop, b, len);
203 _stop += len;
204 } else {
205 int end_len = _buflen - old_stop;
206 _stop = len - end_len;
207 memcpy(_buf + old_stop, b, end_len);
208 memcpy(_buf, b + end_len, _stop);
209 _rollover = true;
210 }
211
212 if (old_start > old_stop && old_start < _stop ||
213 old_start < old_stop && _stop < old_stop)
214 _start = _stop + 1;
215 }