2 * Copyright (c) 2003 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.
36 #include "base/circlebuf.hh"
37 #include "base/cprintf.hh"
38 #include "base/intmath.hh"
42 CircleBuf::CircleBuf(int l
)
43 : _rollover(false), _buflen(l
), _size(0), _start(0), _stop(0)
45 _buf
= new char[_buflen
];
48 CircleBuf::~CircleBuf()
57 cprintf("start = %10d, stop = %10d, buflen = %10d\n",
58 _start
, _stop
, _buflen
);
60 ::write(STDOUT_FILENO
, _buf
, _buflen
);
61 ::write(STDOUT_FILENO
, "<\n", 2);
73 CircleBuf::read(char *b
, int len
)
80 len
= min(len
, _stop
- _start
);
81 memcpy(b
, _buf
+ _start
, len
);
85 int endlen
= _buflen
- _start
;
87 memcpy(b
, _buf
+ _start
, len
);
91 memcpy(b
, _buf
+ _start
, endlen
);
92 _start
= min(len
- endlen
, _stop
);
93 memcpy(b
+ endlen
, _buf
, _start
);
99 CircleBuf::read(int fd
, int len
)
105 if (_stop
> _start
) {
106 len
= min(len
, _stop
- _start
);
107 ::write(fd
, _buf
+ _start
, len
);
111 int endlen
= _buflen
- _start
;
113 ::write(fd
, _buf
+ _start
, len
);
117 ::write(fd
, _buf
+ _start
, endlen
);
118 _start
= min(len
- endlen
, _stop
);
119 ::write(fd
, _buf
, _start
);
125 CircleBuf::read(int fd
)
129 if (_stop
> _start
) {
130 ::write(fd
, _buf
+ _start
, _stop
- _start
);
133 ::write(fd
, _buf
+ _start
, _buflen
- _start
);
134 ::write(fd
, _buf
, _stop
);
141 CircleBuf::read(ostream
&out
)
145 if (_stop
> _start
) {
146 out
.write(_buf
+ _start
, _stop
- _start
);
149 out
.write(_buf
+ _start
, _buflen
- _start
);
150 out
.write(_buf
, _stop
);
157 CircleBuf::readall(int fd
)
160 ::write(fd
, _buf
+ _stop
, _buflen
- _stop
);
162 ::write(fd
, _buf
, _stop
);
167 CircleBuf::write(char b
)
173 CircleBuf::write(const char *b
)
179 CircleBuf::write(const char *b
, int len
)
188 int old_start
= _start
;
189 int old_stop
= _stop
;
191 if (len
>= _buflen
) {
195 memcpy(_buf
, b
+ (len
- _buflen
), _buflen
);
199 if (_stop
+ len
<= _buflen
) {
200 memcpy(_buf
+ _stop
, b
, len
);
203 int end_len
= _buflen
- old_stop
;
204 _stop
= len
- end_len
;
205 memcpy(_buf
+ old_stop
, b
, end_len
);
206 memcpy(_buf
, b
+ end_len
, _stop
);
210 if (old_start
> old_stop
&& old_start
< _stop
||
211 old_start
< old_stop
&& _stop
< old_stop
)