2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
9 * usr/src/cmd/cmd-inet/usr.bin/telnet/ring.c
13 * Copyright (c) 1988, 1993
14 * The Regents of the University of California. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 static char sccsid
[] = "@(#)ring.c 8.1 (Berkeley) 6/6/93";
50 * This defines a structure for a ring buffer.
52 * The circular buffer has two parts:
54 * full: [consume, supply)
55 * empty: [supply, consume)
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <sys/sysmacros.h>
72 #define ring_subtract(d, a, b) (((a)-(b) >= 0)? \
73 (a)-(b): (((a)-(b))+(d)->size))
75 #define ring_increment(d, a, c) (((a)+(c) < (d)->top)? \
76 (a)+(c) : (((a)+(c))-(d)->size))
78 #define ring_decrement(d, a, c) (((a)-(c) >= (d)->bottom)? \
79 (a)-(c) : (((a)-(c))-(d)->size))
83 * The following is a clock, used to determine full, empty, etc.
85 * There is some trickiness here. Since the ring buffers are initialized
86 * to ZERO on allocation, we need to make sure, when interpreting the
87 * clock, that when the times are EQUAL, then the buffer is FULL.
89 ulong_t ring_clock
= 0;
92 #define ring_empty(d) (((d)->consume == (d)->supply) && \
93 ((d)->consumetime >= (d)->supplytime))
94 #define ring_full(d) (((d)->supply == (d)->consume) && \
95 ((d)->supplytime > (d)->consumetime))
101 /* Buffer state transition routines */
104 ring_init(ring
, buffer
, count
)
106 unsigned char *buffer
;
109 (void) memset(ring
, 0, sizeof (*ring
));
113 ring
->supply
= ring
->consume
= ring
->bottom
= buffer
;
115 ring
->top
= ring
->bottom
+ring
->size
;
125 * Mark the most recently supplied byte.
132 ring
->mark
= ring_decrement(ring
, ring
->supply
, 1);
136 * Is the ring pointing to the mark?
143 if (ring
->mark
== ring
->consume
) {
151 * Clear any mark set on the ring.
155 ring_clear_mark(ring
)
162 * Add characters from current segment to ring buffer.
165 ring_supplied(ring
, count
)
169 ring
->supply
= ring_increment(ring
, ring
->supply
, count
);
170 ring
->supplytime
= ++ring_clock
;
174 * We have just consumed "c" bytes.
177 ring_consumed(ring
, count
)
181 if (count
== 0) /* don't update anything */
185 (ring_subtract(ring
, ring
->mark
, ring
->consume
) < count
)) {
189 if (ring
->consume
< ring
->clearto
&&
190 ring
->clearto
<= ring
->consume
+ count
)
192 else if (ring
->consume
+ count
> ring
->top
&&
193 ring
->bottom
<= ring
->clearto
&&
194 ring
->bottom
+ ((ring
->consume
+ count
) - ring
->top
))
197 ring
->consume
= ring_increment(ring
, ring
->consume
, count
);
198 ring
->consumetime
= ++ring_clock
;
200 * Try to encourage "ring_empty_consecutive()" to be large.
202 if (ring_empty(ring
)) {
203 ring
->consume
= ring
->supply
= ring
->bottom
;
209 /* Buffer state query routines */
212 /* Number of bytes that may be supplied */
214 ring_empty_count(ring
)
217 if (ring_empty(ring
)) { /* if empty */
220 return (ring_subtract(ring
, ring
->consume
, ring
->supply
));
224 /* number of CONSECUTIVE bytes that may be supplied */
226 ring_empty_consecutive(ring
)
229 if ((ring
->consume
< ring
->supply
) || ring_empty(ring
)) {
231 * if consume is "below" supply, or empty, then
232 * return distance to the top
234 return (ring_subtract(ring
, ring
->top
, ring
->supply
));
237 * else, return what we may.
239 return (ring_subtract(ring
, ring
->consume
, ring
->supply
));
244 * Return the number of bytes that are available for consuming
245 * (but don't give more than enough to get to cross over set mark)
249 ring_full_count(ring
)
252 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
253 if (ring_full(ring
)) {
254 return (ring
->size
); /* nothing consumed, but full */
256 return (ring_subtract(ring
, ring
->supply
,
260 return (ring_subtract(ring
, ring
->mark
, ring
->consume
));
265 * Return the number of CONSECUTIVE bytes available for consuming.
266 * However, don't return more than enough to cross over set mark.
269 ring_full_consecutive(ring
)
272 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
273 if ((ring
->supply
< ring
->consume
) || ring_full(ring
)) {
274 return (ring_subtract(ring
, ring
->top
, ring
->consume
));
276 return (ring_subtract(ring
, ring
->supply
,
280 if (ring
->mark
< ring
->consume
) {
281 return (ring_subtract(ring
, ring
->top
, ring
->consume
));
282 } else { /* Else, distance to mark */
283 return (ring_subtract(ring
, ring
->mark
, ring
->consume
));
289 * Move data into the "supply" portion of of the ring buffer.
292 ring_supply_data(ring
, buffer
, count
)
294 unsigned char *buffer
;
300 i
= MIN(count
, ring_empty_consecutive(ring
));
301 (void) memcpy(ring
->supply
, buffer
, i
);
302 ring_supplied(ring
, i
);
311 * Move data from the "consume" portion of the ring buffer
314 ring_consume_data(ring
, buffer
, count
)
316 unsigned char *buffer
;
322 i
= MIN(count
, ring_full_consecutive(ring
));
323 memcpy(buffer
, ring
->consume
, i
);
324 ring_consumed(ring
, i
);
332 ring_encrypt(ring
, encryptor
)
336 unsigned char *s
, *c
;
338 if (ring_empty(ring
) || ring
->clearto
== ring
->supply
)
341 if ((c
= ring
->clearto
) == NULL
)
347 (*encryptor
)(c
, ring
->top
- c
);
348 (*encryptor
)(ring
->bottom
, s
- ring
->bottom
);
350 (*encryptor
)(c
, s
- c
);
352 ring
->clearto
= ring
->supply
;
359 if (!ring_empty(ring
))
360 ring
->clearto
= ring
->supply
;