1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* Mach Operating System
* Copyright (c) 1992,1991,1990 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* Author: David B. Golub, Carnegie Mellon University
* Date: 7/90
*
* Circular buffers for TTY
*/
#include <string.h>
#include <device/cirbuf.h>
#include <kern/kalloc.h>
/* read at c_cf, write at c_cl */
/* if c_cf == c_cl, buffer is empty */
/* if c_cl == c_cf - 1, buffer is full */
#if DEBUG
int cb_check_enable = 0;
#define CB_CHECK(cb) if (cb_check_enable) cb_check(cb)
void
cb_check(register struct cirbuf *cb)
{
if (!(cb->c_cf >= cb->c_start && cb->c_cf < cb->c_end))
panic("cf %x out of range [%x..%x)",
cb->c_cf, cb->c_start, cb->c_end);
if (!(cb->c_cl >= cb->c_start && cb->c_cl < cb->c_end))
panic("cl %x out of range [%x..%x)",
cb->c_cl, cb->c_start, cb->c_end);
if (cb->c_cf <= cb->c_cl) {
if (!(cb->c_cc == cb->c_cl - cb->c_cf))
panic("cc %x should be %x",
cb->c_cc,
cb->c_cl - cb->c_cf);
}
else {
if (!(cb->c_cc == cb->c_end - cb->c_cf
+ cb->c_cl - cb->c_start))
panic("cc %x should be %x",
cb->c_cc,
cb->c_end - cb->c_cf +
cb->c_cl - cb->c_start);
}
}
#else /* DEBUG */
#define CB_CHECK(cb)
#endif /* DEBUG */
/*
* Put one character in circular buffer.
*/
int putc(
int c,
register struct cirbuf *cb)
{
register char *ow, *nw;
ow = cb->c_cl;
nw = ow+1;
if (nw == cb->c_end)
nw = cb->c_start;
if (nw == cb->c_cf)
return 1; /* not entered */
*ow = c;
cb->c_cl = nw;
cb->c_cc++;
CB_CHECK(cb);
return 0;
}
/*
* Get one character from circular buffer.
*/
int getc(register struct cirbuf *cb)
{
register unsigned char *nr;
register int c;
nr = (unsigned char *)cb->c_cf;
if (nr == (unsigned char *)cb->c_cl) {
CB_CHECK(cb);
return -1; /* empty */
}
c = *nr;
nr++;
if (nr == (unsigned char *)cb->c_end)
nr = (unsigned char *)cb->c_start;
cb->c_cf = (char *)nr;
cb->c_cc--;
CB_CHECK(cb);
return c;
}
/*
* Get lots of characters.
* Return number moved.
*/
int
q_to_b( register struct cirbuf *cb,
register char *cp,
register int count)
{
char * ocp = cp;
register int i;
while (count != 0) {
if (cb->c_cl == cb->c_cf)
break; /* empty */
if (cb->c_cl < cb->c_cf)
i = cb->c_end - cb->c_cf;
else
i = cb->c_cl - cb->c_cf;
if (i > count)
i = count;
memcpy(cp, cb->c_cf, i);
cp += i;
count -= i;
cb->c_cf += i;
cb->c_cc -= i;
if (cb->c_cf == cb->c_end)
cb->c_cf = cb->c_start;
CB_CHECK(cb);
}
CB_CHECK(cb);
return cp - ocp;
}
/*
* Add character array to buffer and return number of characters
* NOT entered.
*/
int
b_to_q( register char * cp,
int count,
register struct cirbuf *cb)
{
register int i;
register char *lim;
while (count != 0) {
lim = cb->c_cf - 1;
if (lim < cb->c_start)
lim = cb->c_end - 1;
if (cb->c_cl == lim)
break;
if (cb->c_cl < lim)
i = lim - cb->c_cl;
else
i = cb->c_end - cb->c_cl;
if (i > count)
i = count;
memcpy(cb->c_cl, cp, i);
cp += i;
count -= i;
cb->c_cc += i;
cb->c_cl += i;
if (cb->c_cl == cb->c_end)
cb->c_cl = cb->c_start;
CB_CHECK(cb);
}
CB_CHECK(cb);
return count;
}
/*
* Return number of contiguous characters up to a character
* that matches the mask.
*/
int
ndqb( register struct cirbuf *cb,
register int mask)
{
register char *cp, *lim;
if (cb->c_cl < cb->c_cf)
lim = cb->c_end;
else
lim = cb->c_cl;
if (mask == 0)
return (lim - cb->c_cf);
cp = cb->c_cf;
while (cp < lim) {
if (*cp & mask)
break;
cp++;
}
return (cp - cb->c_cf);
}
/*
* Flush characters from circular buffer.
*/
void
ndflush(register struct cirbuf *cb,
register int count)
{
register int i;
while (count != 0) {
if (cb->c_cl == cb->c_cf)
break; /* empty */
if (cb->c_cl < cb->c_cf)
i = cb->c_end - cb->c_cf;
else
i = cb->c_cl - cb->c_cf;
if (i > count)
i = count;
count -= i;
cb->c_cf += i;
cb->c_cc -= i;
if (cb->c_cf == cb->c_end)
cb->c_cf = cb->c_start;
CB_CHECK(cb);
}
CB_CHECK(cb);
}
/*
* Empty a circular buffer.
*/
void cb_clear(struct cirbuf *cb)
{
cb->c_cf = cb->c_start;
cb->c_cl = cb->c_start;
cb->c_cc = 0;
}
/*
* Allocate character space for a circular buffer.
*/
void
cb_alloc(
register struct cirbuf *cb,
int buf_size)
{
register char *buf;
buf = (char *)kalloc(buf_size);
cb->c_start = buf;
cb->c_end = buf + buf_size;
cb->c_cf = buf;
cb->c_cl = buf;
cb->c_cc = 0;
cb->c_hog = buf_size - 1;
CB_CHECK(cb);
}
/*
* Free character space for a circular buffer.
*/
void
cb_free(register struct cirbuf *cb)
{
int size;
size = cb->c_end - cb->c_start;
kfree((vm_offset_t)cb->c_start, size);
}
|