Get rid of lots of obsolete mentions of stat_sdb_t.
[gem5.git] / base / circlebuf.cc
1 /*
2 * Copyright (c) 2003 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
29 #include <algorithm>
30 #include <string>
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include "base/circlebuf.hh"
37 #include "base/cprintf.hh"
38 #include "base/intmath.hh"
39
40 using namespace std;
41
42 CircleBuf::CircleBuf(int l)
43 : rollover(false), buflen(l), size(0), start(0), stop(0)
44 { buf = new char[buflen]; }
45
46 CircleBuf::~CircleBuf()
47 { if (buf) delete [] buf; }
48
49 void
50 CircleBuf::dump()
51 {
52 cprintf("start = %10d, stop = %10d, buflen = %10d\n", start, stop, buflen);
53 fflush(stdout);
54 ::write(STDOUT_FILENO, buf, buflen);
55 ::write(STDOUT_FILENO, "<\n", 2);
56 }
57
58 void
59 CircleBuf::flush()
60 {
61 start = 0;
62 stop = 0;
63 rollover = false;
64 }
65
66 void
67 CircleBuf::read(char *b, int len)
68 {
69 size -= len;
70 if (size < 0)
71 size = 0;
72
73 if (stop > start) {
74 len = min(len, stop - start);
75 memcpy(b, buf + start, len);
76 start += len;
77 }
78 else {
79 int endlen = buflen - start;
80 if (endlen > len) {
81 memcpy(b, buf + start, len);
82 start += len;
83 }
84 else {
85 memcpy(b, buf + start, endlen);
86 start = min(len - endlen, stop);
87 memcpy(b + endlen, buf, start);
88 }
89 }
90 }
91
92 void
93 CircleBuf::read(int fd, int len)
94 {
95 size -= len;
96 if (size < 0)
97 size = 0;
98
99 if (stop > start) {
100 len = min(len, stop - start);
101 ::write(fd, buf + start, len);
102 start += len;
103 }
104 else {
105 int endlen = buflen - start;
106 if (endlen > len) {
107 ::write(fd, buf + start, len);
108 start += len;
109 }
110 else {
111 ::write(fd, buf + start, endlen);
112 start = min(len - endlen, stop);
113 ::write(fd, buf, start);
114 }
115 }
116 }
117
118 void
119 CircleBuf::read(int fd)
120 {
121 size = 0;
122
123 if (stop > start) {
124 ::write(fd, buf + start, stop - start);
125 }
126 else {
127 ::write(fd, buf + start, buflen - start);
128 ::write(fd, buf, stop);
129 }
130
131 start = stop;
132 }
133
134 void
135 CircleBuf::readall(int fd)
136 {
137 if (rollover)
138 ::write(fd, buf + stop, buflen - stop);
139
140 ::write(fd, buf, stop);
141 start = stop;
142 }
143
144 void
145 CircleBuf::write(char b)
146 { write(&b, 1); }
147
148 void
149 CircleBuf::write(const char *b)
150 { write(b, strlen(b)); }
151
152 void
153 CircleBuf::write(const char *b, int len)
154 {
155 if (len <= 0)
156 return;
157
158 size += len;
159 if (size > buflen)
160 size = buflen;
161
162 int old_start = start;
163 int old_stop = stop;
164
165 if (len >= buflen) {
166 start = 0;
167 stop = buflen;
168 rollover = true;
169 memcpy(buf, b + (len - buflen), buflen);
170 return;
171 }
172
173 if (stop + len <= buflen) {
174 memcpy(buf + stop, b, len);
175 stop += len;
176 } else {
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);
181 rollover = true;
182 }
183
184 if (old_start > old_stop && old_start < stop ||
185 old_start < old_stop && stop < old_stop)
186 start = stop + 1;
187 }