xref: /dragonfly/usr.bin/telnet/ring.c (revision 984263bc)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  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
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.
20  *
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.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD: src/usr.bin/telnet/ring.c,v 1.3.6.1 2002/04/13 11:07:13 markm Exp $");
37 
38 #ifndef lint
39 static const char sccsid[] = "@(#)ring.c	8.2 (Berkeley) 5/30/95";
40 #endif
41 
42 /*
43  * This defines a structure for a ring buffer.
44  *
45  * The circular buffer has two parts:
46  *(((
47  *	full:	[consume, supply)
48  *	empty:	[supply, consume)
49  *]]]
50  *
51  */
52 
53 #include	<errno.h>
54 #include	<stdio.h>
55 #include	<string.h>
56 
57 #ifdef	size_t
58 #undef	size_t
59 #endif
60 
61 #include	<sys/types.h>
62 #ifndef	FILIO_H
63 #include	<sys/ioctl.h>
64 #endif
65 #include	<sys/socket.h>
66 
67 #include	"ring.h"
68 #include	"general.h"
69 
70 /* Internal macros */
71 
72 #if	!defined(MIN)
73 #define	MIN(a,b)	(((a)<(b))? (a):(b))
74 #endif	/* !defined(MIN) */
75 
76 #define	ring_subtract(d,a,b)	(((a)-(b) >= 0)? \
77 					(a)-(b): (((a)-(b))+(d)->size))
78 
79 #define	ring_increment(d,a,c)	(((a)+(c) < (d)->top)? \
80 					(a)+(c) : (((a)+(c))-(d)->size))
81 
82 #define	ring_decrement(d,a,c)	(((a)-(c) >= (d)->bottom)? \
83 					(a)-(c) : (((a)-(c))-(d)->size))
84 
85 
86 /*
87  * The following is a clock, used to determine full, empty, etc.
88  *
89  * There is some trickiness here.  Since the ring buffers are initialized
90  * to ZERO on allocation, we need to make sure, when interpreting the
91  * clock, that when the times are EQUAL, then the buffer is FULL.
92  */
93 static u_long ring_clock = 0;
94 
95 
96 #define	ring_empty(d) (((d)->consume == (d)->supply) && \
97 				((d)->consumetime >= (d)->supplytime))
98 #define	ring_full(d) (((d)->supply == (d)->consume) && \
99 				((d)->supplytime > (d)->consumetime))
100 
101 /* Buffer state transition routines */
102 
103 int
104 ring_init(Ring *ring, unsigned char *buffer, int count)
105 {
106     memset((char *)ring, 0, sizeof *ring);
107 
108     ring->size = count;
109 
110     ring->supply = ring->consume = ring->bottom = buffer;
111 
112     ring->top = ring->bottom+ring->size;
113 
114 
115     return 1;
116 }
117 
118 /* Mark routines */
119 
120 /*
121  * Mark the most recently supplied byte.
122  */
123 
124 void
125 ring_mark(Ring *ring)
126 {
127     ring->mark = ring_decrement(ring, ring->supply, 1);
128 }
129 
130 /*
131  * Is the ring pointing to the mark?
132  */
133 
134 int
135 ring_at_mark(Ring *ring)
136 {
137     if (ring->mark == ring->consume) {
138 	return 1;
139     } else {
140 	return 0;
141     }
142 }
143 
144 /*
145  * Clear any mark set on the ring.
146  */
147 
148 void
149 ring_clear_mark(Ring *ring)
150 {
151     ring->mark = 0;
152 }
153 
154 /*
155  * Add characters from current segment to ring buffer.
156  */
157 void
158 ring_supplied(Ring *ring, int count)
159 {
160     ring->supply = ring_increment(ring, ring->supply, count);
161     ring->supplytime = ++ring_clock;
162 }
163 
164 /*
165  * We have just consumed "c" bytes.
166  */
167 void
168 ring_consumed(Ring *ring, int count)
169 {
170     if (count == 0)	/* don't update anything */
171 	return;
172 
173     if (ring->mark &&
174 		(ring_subtract(ring, ring->mark, ring->consume) < count)) {
175 	ring->mark = 0;
176     }
177     ring->consume = ring_increment(ring, ring->consume, count);
178     ring->consumetime = ++ring_clock;
179     /*
180      * Try to encourage "ring_empty_consecutive()" to be large.
181      */
182     if (ring_empty(ring)) {
183 	ring->consume = ring->supply = ring->bottom;
184     }
185 }
186 
187 
188 
189 /* Buffer state query routines */
190 
191 
192 /* Number of bytes that may be supplied */
193 int
194 ring_empty_count(Ring *ring)
195 {
196     if (ring_empty(ring)) {	/* if empty */
197 	    return ring->size;
198     } else {
199 	return ring_subtract(ring, ring->consume, ring->supply);
200     }
201 }
202 
203 /* number of CONSECUTIVE bytes that may be supplied */
204 int
205 ring_empty_consecutive(Ring *ring)
206 {
207     if ((ring->consume < ring->supply) || ring_empty(ring)) {
208 			    /*
209 			     * if consume is "below" supply, or empty, then
210 			     * return distance to the top
211 			     */
212 	return ring_subtract(ring, ring->top, ring->supply);
213     } else {
214 				    /*
215 				     * else, return what we may.
216 				     */
217 	return ring_subtract(ring, ring->consume, ring->supply);
218     }
219 }
220 
221 /* Return the number of bytes that are available for consuming
222  * (but don't give more than enough to get to cross over set mark)
223  */
224 
225 int
226 ring_full_count(Ring *ring)
227 {
228     if ((ring->mark == 0) || (ring->mark == ring->consume)) {
229 	if (ring_full(ring)) {
230 	    return ring->size;	/* nothing consumed, but full */
231 	} else {
232 	    return ring_subtract(ring, ring->supply, ring->consume);
233 	}
234     } else {
235 	return ring_subtract(ring, ring->mark, ring->consume);
236     }
237 }
238 
239 /*
240  * Return the number of CONSECUTIVE bytes available for consuming.
241  * However, don't return more than enough to cross over set mark.
242  */
243 int
244 ring_full_consecutive(Ring *ring)
245 {
246     if ((ring->mark == 0) || (ring->mark == ring->consume)) {
247 	if ((ring->supply < ring->consume) || ring_full(ring)) {
248 	    return ring_subtract(ring, ring->top, ring->consume);
249 	} else {
250 	    return ring_subtract(ring, ring->supply, ring->consume);
251 	}
252     } else {
253 	if (ring->mark < ring->consume) {
254 	    return ring_subtract(ring, ring->top, ring->consume);
255 	} else {	/* Else, distance to mark */
256 	    return ring_subtract(ring, ring->mark, ring->consume);
257 	}
258     }
259 }
260 
261 /*
262  * Move data into the "supply" portion of of the ring buffer.
263  */
264 void
265 ring_supply_data(Ring *ring, unsigned char *buffer, int count)
266 {
267     int i;
268 
269     while (count) {
270 	i = MIN(count, ring_empty_consecutive(ring));
271 	memcpy(ring->supply, buffer, i);
272 	ring_supplied(ring, i);
273 	count -= i;
274 	buffer += i;
275     }
276 }
277 
278