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)
44 { buf
= new char[buflen
]; }
46 CircleBuf::~CircleBuf()
47 { if (buf
) delete [] buf
; }
52 cprintf("start = %10d, stop = %10d, buflen = %10d\n", start
, stop
, buflen
);
54 ::write(STDOUT_FILENO
, buf
, buflen
);
55 ::write(STDOUT_FILENO
, "<\n", 2);
67 CircleBuf::read(char *b
, int len
)
74 len
= min(len
, stop
- start
);
75 memcpy(b
, buf
+ start
, len
);
79 int endlen
= buflen
- start
;
81 memcpy(b
, buf
+ start
, len
);
85 memcpy(b
, buf
+ start
, endlen
);
86 start
= min(len
- endlen
, stop
);
87 memcpy(b
+ endlen
, buf
, start
);
93 CircleBuf::read(int fd
, int len
)
100 len
= min(len
, stop
- start
);
101 ::write(fd
, buf
+ start
, len
);
105 int endlen
= buflen
- start
;
107 ::write(fd
, buf
+ start
, len
);
111 ::write(fd
, buf
+ start
, endlen
);
112 start
= min(len
- endlen
, stop
);
113 ::write(fd
, buf
, start
);
119 CircleBuf::read(int fd
)
124 ::write(fd
, buf
+ start
, stop
- start
);
127 ::write(fd
, buf
+ start
, buflen
- start
);
128 ::write(fd
, buf
, stop
);
135 CircleBuf::readall(int fd
)
138 ::write(fd
, buf
+ stop
, buflen
- stop
);
140 ::write(fd
, buf
, stop
);
145 CircleBuf::write(char b
)
149 CircleBuf::write(const char *b
)
150 { write(b
, strlen(b
)); }
153 CircleBuf::write(const char *b
, int len
)
162 int old_start
= start
;
169 memcpy(buf
, b
+ (len
- buflen
), buflen
);
173 if (stop
+ len
<= buflen
) {
174 memcpy(buf
+ stop
, b
, len
);
177 int end_len
= buflen
- old_stop
;
178 stop
= len
- end_len
;
179 memcpy(buf
+ old_stop
, b
, end_len
);
180 memcpy(buf
, b
+ end_len
, stop
);
184 if (old_start
> old_stop
&& old_start
< stop
||
185 old_start
< old_stop
&& stop
< old_stop
)