2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
38 #include "base/circlebuf.hh"
39 #include "base/cprintf.hh"
40 #include "base/intmath.hh"
44 CircleBuf::CircleBuf(int l
)
45 : _rollover(false), _buflen(l
), _size(0), _start(0), _stop(0)
47 _buf
= new char[_buflen
];
50 CircleBuf::~CircleBuf()
59 cprintf("start = %10d, stop = %10d, buflen = %10d\n",
60 _start
, _stop
, _buflen
);
62 ::write(STDOUT_FILENO
, _buf
, _buflen
);
63 ::write(STDOUT_FILENO
, "<\n", 2);
75 CircleBuf::read(char *b
, int len
)
82 len
= min(len
, _stop
- _start
);
83 memcpy(b
, _buf
+ _start
, len
);
87 int endlen
= _buflen
- _start
;
89 memcpy(b
, _buf
+ _start
, len
);
93 memcpy(b
, _buf
+ _start
, endlen
);
94 _start
= min(len
- endlen
, _stop
);
95 memcpy(b
+ endlen
, _buf
, _start
);
101 CircleBuf::read(int fd
, int len
)
107 if (_stop
> _start
) {
108 len
= min(len
, _stop
- _start
);
109 ::write(fd
, _buf
+ _start
, len
);
113 int endlen
= _buflen
- _start
;
115 ::write(fd
, _buf
+ _start
, len
);
119 ::write(fd
, _buf
+ _start
, endlen
);
120 _start
= min(len
- endlen
, _stop
);
121 ::write(fd
, _buf
, _start
);
127 CircleBuf::read(int fd
)
131 if (_stop
> _start
) {
132 ::write(fd
, _buf
+ _start
, _stop
- _start
);
135 ::write(fd
, _buf
+ _start
, _buflen
- _start
);
136 ::write(fd
, _buf
, _stop
);
143 CircleBuf::read(ostream
&out
)
147 if (_stop
> _start
) {
148 out
.write(_buf
+ _start
, _stop
- _start
);
151 out
.write(_buf
+ _start
, _buflen
- _start
);
152 out
.write(_buf
, _stop
);
159 CircleBuf::readall(int fd
)
162 ::write(fd
, _buf
+ _stop
, _buflen
- _stop
);
164 ::write(fd
, _buf
, _stop
);
169 CircleBuf::write(char b
)
175 CircleBuf::write(const char *b
)
181 CircleBuf::write(const char *b
, int len
)
190 int old_start
= _start
;
191 int old_stop
= _stop
;
193 if (len
>= _buflen
) {
197 memcpy(_buf
, b
+ (len
- _buflen
), _buflen
);
201 if (_stop
+ len
<= _buflen
) {
202 memcpy(_buf
+ _stop
, b
, len
);
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
);
212 if (old_start
> old_stop
&& old_start
< _stop
||
213 old_start
< old_stop
&& _stop
< old_stop
)