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
36 #include "base/atomicio.hh"
37 #include "base/circlebuf.hh"
38 #include "base/cprintf.hh"
39 #include "base/intmath.hh"
43 CircleBuf::CircleBuf(int l
)
44 : _rollover(false), _buflen(l
), _size(0), _start(0), _stop(0)
46 _buf
= new char[_buflen
];
49 CircleBuf::~CircleBuf()
58 cprintf("start = %10d, stop = %10d, buflen = %10d\n",
59 _start
, _stop
, _buflen
);
61 atomic_write(STDOUT_FILENO
, _buf
, _buflen
);
62 atomic_write(STDOUT_FILENO
, "<\n", 2);
74 CircleBuf::read(char *b
, int len
)
81 len
= min(len
, _stop
- _start
);
82 memcpy(b
, _buf
+ _start
, len
);
86 int endlen
= _buflen
- _start
;
88 memcpy(b
, _buf
+ _start
, len
);
92 memcpy(b
, _buf
+ _start
, endlen
);
93 _start
= min(len
- endlen
, _stop
);
94 memcpy(b
+ endlen
, _buf
, _start
);
100 CircleBuf::read(int fd
, int len
)
106 if (_stop
> _start
) {
107 len
= min(len
, _stop
- _start
);
108 atomic_write(fd
, _buf
+ _start
, len
);
112 int endlen
= _buflen
- _start
;
114 atomic_write(fd
, _buf
+ _start
, len
);
118 atomic_write(fd
, _buf
+ _start
, endlen
);
119 _start
= min(len
- endlen
, _stop
);
120 atomic_write(fd
, _buf
, _start
);
126 CircleBuf::read(int fd
)
130 if (_stop
> _start
) {
131 atomic_write(fd
, _buf
+ _start
, _stop
- _start
);
134 atomic_write(fd
, _buf
+ _start
, _buflen
- _start
);
135 atomic_write(fd
, _buf
, _stop
);
142 CircleBuf::read(ostream
&out
)
146 if (_stop
> _start
) {
147 out
.write(_buf
+ _start
, _stop
- _start
);
150 out
.write(_buf
+ _start
, _buflen
- _start
);
151 out
.write(_buf
, _stop
);
158 CircleBuf::readall(int fd
)
161 atomic_write(fd
, _buf
+ _stop
, _buflen
- _stop
);
163 atomic_write(fd
, _buf
, _stop
);
168 CircleBuf::write(char b
)
174 CircleBuf::write(const char *b
)
180 CircleBuf::write(const char *b
, int len
)
189 int old_start
= _start
;
190 int old_stop
= _stop
;
192 if (len
>= _buflen
) {
196 memcpy(_buf
, b
+ (len
- _buflen
), _buflen
);
200 if (_stop
+ len
<= _buflen
) {
201 memcpy(_buf
+ _stop
, b
, len
);
204 int end_len
= _buflen
- old_stop
;
205 _stop
= len
- end_len
;
206 memcpy(_buf
+ old_stop
, b
, end_len
);
207 memcpy(_buf
, b
+ end_len
, _stop
);
211 if ((old_start
> old_stop
&& old_start
< _stop
) ||
212 (old_start
< old_stop
&& _stop
< old_stop
))