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
45 static char sccsid
[] = "@(#)ring.c 8.1 (Berkeley) 6/6/93";
48 * This defines a structure for a ring buffer.
50 * The circular buffer has two parts:
52 * full: [consume, supply)
53 * empty: [supply, consume)
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <sys/sysmacros.h>
70 #define ring_subtract(d, a, b) (((a)-(b) >= 0)? \
71 (a)-(b): (((a)-(b))+(d)->size))
73 #define ring_increment(d, a, c) (((a)+(c) < (d)->top)? \
74 (a)+(c) : (((a)+(c))-(d)->size))
76 #define ring_decrement(d, a, c) (((a)-(c) >= (d)->bottom)? \
77 (a)-(c) : (((a)-(c))-(d)->size))
81 * The following is a clock, used to determine full, empty, etc.
83 * There is some trickiness here. Since the ring buffers are initialized
84 * to ZERO on allocation, we need to make sure, when interpreting the
85 * clock, that when the times are EQUAL, then the buffer is FULL.
87 ulong_t ring_clock
= 0;
90 #define ring_empty(d) (((d)->consume == (d)->supply) && \
91 ((d)->consumetime >= (d)->supplytime))
92 #define ring_full(d) (((d)->supply == (d)->consume) && \
93 ((d)->supplytime > (d)->consumetime))
99 /* Buffer state transition routines */
102 ring_init(ring
, buffer
, count
)
104 unsigned char *buffer
;
107 (void) memset(ring
, 0, sizeof (*ring
));
111 ring
->supply
= ring
->consume
= ring
->bottom
= buffer
;
113 ring
->top
= ring
->bottom
+ring
->size
;
123 * Mark the most recently supplied byte.
130 ring
->mark
= ring_decrement(ring
, ring
->supply
, 1);
134 * Is the ring pointing to the mark?
141 if (ring
->mark
== ring
->consume
) {
149 * Clear any mark set on the ring.
153 ring_clear_mark(ring
)
160 * Add characters from current segment to ring buffer.
163 ring_supplied(ring
, count
)
167 ring
->supply
= ring_increment(ring
, ring
->supply
, count
);
168 ring
->supplytime
= ++ring_clock
;
172 * We have just consumed "c" bytes.
175 ring_consumed(ring
, count
)
179 if (count
== 0) /* don't update anything */
183 (ring_subtract(ring
, ring
->mark
, ring
->consume
) < count
)) {
187 if (ring
->consume
< ring
->clearto
&&
188 ring
->clearto
<= ring
->consume
+ count
)
190 else if (ring
->consume
+ count
> ring
->top
&&
191 ring
->bottom
<= ring
->clearto
&&
192 ring
->bottom
+ ((ring
->consume
+ count
) - ring
->top
))
195 ring
->consume
= ring_increment(ring
, ring
->consume
, count
);
196 ring
->consumetime
= ++ring_clock
;
198 * Try to encourage "ring_empty_consecutive()" to be large.
200 if (ring_empty(ring
)) {
201 ring
->consume
= ring
->supply
= ring
->bottom
;
207 /* Buffer state query routines */
210 /* Number of bytes that may be supplied */
212 ring_empty_count(ring
)
215 if (ring_empty(ring
)) { /* if empty */
218 return (ring_subtract(ring
, ring
->consume
, ring
->supply
));
222 /* number of CONSECUTIVE bytes that may be supplied */
224 ring_empty_consecutive(ring
)
227 if ((ring
->consume
< ring
->supply
) || ring_empty(ring
)) {
229 * if consume is "below" supply, or empty, then
230 * return distance to the top
232 return (ring_subtract(ring
, ring
->top
, ring
->supply
));
235 * else, return what we may.
237 return (ring_subtract(ring
, ring
->consume
, ring
->supply
));
242 * Return the number of bytes that are available for consuming
243 * (but don't give more than enough to get to cross over set mark)
247 ring_full_count(ring
)
250 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
251 if (ring_full(ring
)) {
252 return (ring
->size
); /* nothing consumed, but full */
254 return (ring_subtract(ring
, ring
->supply
,
258 return (ring_subtract(ring
, ring
->mark
, ring
->consume
));
263 * Return the number of CONSECUTIVE bytes available for consuming.
264 * However, don't return more than enough to cross over set mark.
267 ring_full_consecutive(ring
)
270 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
271 if ((ring
->supply
< ring
->consume
) || ring_full(ring
)) {
272 return (ring_subtract(ring
, ring
->top
, ring
->consume
));
274 return (ring_subtract(ring
, ring
->supply
,
278 if (ring
->mark
< ring
->consume
) {
279 return (ring_subtract(ring
, ring
->top
, ring
->consume
));
280 } else { /* Else, distance to mark */
281 return (ring_subtract(ring
, ring
->mark
, ring
->consume
));
287 * Move data into the "supply" portion of of the ring buffer.
290 ring_supply_data(ring
, buffer
, count
)
292 unsigned char *buffer
;
298 i
= MIN(count
, ring_empty_consecutive(ring
));
299 (void) memcpy(ring
->supply
, buffer
, i
);
300 ring_supplied(ring
, i
);
309 * Move data from the "consume" portion of the ring buffer
312 ring_consume_data(ring
, buffer
, count
)
314 unsigned char *buffer
;
320 i
= MIN(count
, ring_full_consecutive(ring
));
321 memcpy(buffer
, ring
->consume
, i
);
322 ring_consumed(ring
, i
);
330 ring_encrypt(ring
, encryptor
)
334 unsigned char *s
, *c
;
336 if (ring_empty(ring
) || ring
->clearto
== ring
->supply
)
339 if ((c
= ring
->clearto
) == NULL
)
345 (*encryptor
)(c
, ring
->top
- c
);
346 (*encryptor
)(ring
->bottom
, s
- ring
->bottom
);
348 (*encryptor
)(c
, s
- c
);
350 ring
->clearto
= ring
->supply
;
357 if (!ring_empty(ring
))
358 ring
->clearto
= ring
->supply
;