MFC:
[dragonfly.git] / usr.bin / telnet / ring.c
blob6f7f0618a0d75852fe3c78e2baf5c0a2d1f677ef
1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)ring.c 8.2 (Berkeley) 5/30/95
34 * $FreeBSD: src/usr.bin/telnet/ring.c,v 1.3.6.1 2002/04/13 11:07:13 markm Exp $
35 * $DragonFly: src/usr.bin/telnet/ring.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
39 * This defines a structure for a ring buffer.
41 * The circular buffer has two parts:
42 *(((
43 * full: [consume, supply)
44 * empty: [supply, consume)
45 *]]]
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
53 #ifdef size_t
54 #undef size_t
55 #endif
57 #include <sys/types.h>
58 #ifndef FILIO_H
59 #include <sys/ioctl.h>
60 #endif
61 #include <sys/socket.h>
63 #include "ring.h"
64 #include "general.h"
66 /* Internal macros */
68 #if !defined(MIN)
69 #define MIN(a,b) (((a)<(b))? (a):(b))
70 #endif /* !defined(MIN) */
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 static u_long 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))
97 /* Buffer state transition routines */
99 int
100 ring_init(Ring *ring, unsigned char *buffer, int count)
102 memset((char *)ring, 0, sizeof *ring);
104 ring->size = count;
106 ring->supply = ring->consume = ring->bottom = buffer;
108 ring->top = ring->bottom+ring->size;
111 return 1;
114 /* Mark routines */
117 * Mark the most recently supplied byte.
120 void
121 ring_mark(Ring *ring)
123 ring->mark = ring_decrement(ring, ring->supply, 1);
127 * Is the ring pointing to the mark?
131 ring_at_mark(Ring *ring)
133 if (ring->mark == ring->consume) {
134 return 1;
135 } else {
136 return 0;
141 * Clear any mark set on the ring.
144 void
145 ring_clear_mark(Ring *ring)
147 ring->mark = 0;
151 * Add characters from current segment to ring buffer.
153 void
154 ring_supplied(Ring *ring, int count)
156 ring->supply = ring_increment(ring, ring->supply, count);
157 ring->supplytime = ++ring_clock;
161 * We have just consumed "c" bytes.
163 void
164 ring_consumed(Ring *ring, int count)
166 if (count == 0) /* don't update anything */
167 return;
169 if (ring->mark &&
170 (ring_subtract(ring, ring->mark, ring->consume) < count)) {
171 ring->mark = 0;
173 ring->consume = ring_increment(ring, ring->consume, count);
174 ring->consumetime = ++ring_clock;
176 * Try to encourage "ring_empty_consecutive()" to be large.
178 if (ring_empty(ring)) {
179 ring->consume = ring->supply = ring->bottom;
185 /* Buffer state query routines */
188 /* Number of bytes that may be supplied */
190 ring_empty_count(Ring *ring)
192 if (ring_empty(ring)) { /* if empty */
193 return ring->size;
194 } else {
195 return ring_subtract(ring, ring->consume, ring->supply);
199 /* number of CONSECUTIVE bytes that may be supplied */
201 ring_empty_consecutive(Ring *ring)
203 if ((ring->consume < ring->supply) || ring_empty(ring)) {
205 * if consume is "below" supply, or empty, then
206 * return distance to the top
208 return ring_subtract(ring, ring->top, ring->supply);
209 } else {
211 * else, return what we may.
213 return ring_subtract(ring, ring->consume, ring->supply);
217 /* Return the number of bytes that are available for consuming
218 * (but don't give more than enough to get to cross over set mark)
222 ring_full_count(Ring *ring)
224 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
225 if (ring_full(ring)) {
226 return ring->size; /* nothing consumed, but full */
227 } else {
228 return ring_subtract(ring, ring->supply, ring->consume);
230 } else {
231 return ring_subtract(ring, ring->mark, ring->consume);
236 * Return the number of CONSECUTIVE bytes available for consuming.
237 * However, don't return more than enough to cross over set mark.
240 ring_full_consecutive(Ring *ring)
242 if ((ring->mark == 0) || (ring->mark == ring->consume)) {
243 if ((ring->supply < ring->consume) || ring_full(ring)) {
244 return ring_subtract(ring, ring->top, ring->consume);
245 } else {
246 return ring_subtract(ring, ring->supply, ring->consume);
248 } else {
249 if (ring->mark < ring->consume) {
250 return ring_subtract(ring, ring->top, ring->consume);
251 } else { /* Else, distance to mark */
252 return ring_subtract(ring, ring->mark, ring->consume);
258 * Move data into the "supply" portion of of the ring buffer.
260 void
261 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
263 int i;
265 while (count) {
266 i = MIN(count, ring_empty_consecutive(ring));
267 memcpy(ring->supply, buffer, i);
268 ring_supplied(ring, i);
269 count -= i;
270 buffer += i;