1 /* $NetBSD: ring.c,v 1.12 2003/07/14 15:56:29 itojun Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)ring.c 8.2 (Berkeley) 5/30/95";
37 __RCSID("$NetBSD: ring.c,v 1.12 2003/07/14 15:56:29 itojun Exp $");
42 * This defines a structure for a ring buffer.
44 * The circular buffer has two parts:
46 * full: [consume, supply)
47 * empty: [supply, consume)
56 #include <sys/types.h>
57 #include <sys/ioctl.h>
58 #include <sys/socket.h>
66 #define MIN(a,b) (((a)<(b))? (a):(b))
67 #endif /* !defined(MIN) */
69 #define ring_subtract(d,a,b) (((a)-(b) >= 0)? \
70 (a)-(b): (((a)-(b))+(d)->size))
72 #define ring_increment(d,a,c) (((a)+(c) < (d)->top)? \
73 (a)+(c) : (((a)+(c))-(d)->size))
75 #define ring_decrement(d,a,c) (((a)-(c) >= (d)->bottom)? \
76 (a)-(c) : (((a)-(c))-(d)->size))
80 * The following is a clock, used to determine full, empty, etc.
82 * There is some trickiness here. Since the ring buffers are initialized
83 * to ZERO on allocation, we need to make sure, when interpreting the
84 * clock, that when the times are EQUAL, then the buffer is FULL.
86 static u_long ring_clock
= 0;
89 #define ring_empty(d) (((d)->consume == (d)->supply) && \
90 ((d)->consumetime >= (d)->supplytime))
91 #define ring_full(d) (((d)->supply == (d)->consume) && \
92 ((d)->supplytime > (d)->consumetime))
98 /* Buffer state transition routines */
101 ring_init(Ring
*ring
, unsigned char *buffer
, int count
)
103 memset((char *)ring
, 0, sizeof *ring
);
107 ring
->supply
= ring
->consume
= ring
->bottom
= buffer
;
109 ring
->top
= ring
->bottom
+ring
->size
;
113 #endif /* ENCRYPTION */
121 * Mark the most recently supplied byte.
125 ring_mark(Ring
*ring
)
127 ring
->mark
= ring_decrement(ring
, ring
->supply
, 1);
131 * Is the ring pointing to the mark?
135 ring_at_mark(Ring
*ring
)
137 if (ring
->mark
== ring
->consume
) {
145 * Clear any mark set on the ring.
149 ring_clear_mark(Ring
*ring
)
155 * Add characters from current segment to ring buffer.
158 ring_supplied(Ring
*ring
, int count
)
160 ring
->supply
= ring_increment(ring
, ring
->supply
, count
);
161 ring
->supplytime
= ++ring_clock
;
165 * We have just consumed "c" bytes.
168 ring_consumed(Ring
*ring
, int count
)
170 if (count
== 0) /* don't update anything */
174 (ring_subtract(ring
, ring
->mark
, ring
->consume
) < count
)) {
178 if (ring
->consume
< ring
->clearto
&&
179 ring
->clearto
<= ring
->consume
+ count
)
181 else if (ring
->consume
+ count
> ring
->top
&&
182 ring
->bottom
<= ring
->clearto
&&
183 ring
->bottom
+ ((ring
->consume
+ count
) - ring
->top
))
185 #endif /* ENCRYPTION */
186 ring
->consume
= ring_increment(ring
, ring
->consume
, count
);
187 ring
->consumetime
= ++ring_clock
;
189 * Try to encourage "ring_empty_consecutive()" to be large.
191 if (ring_empty(ring
)) {
192 ring
->consume
= ring
->supply
= ring
->bottom
;
198 /* Buffer state query routines */
201 /* Number of bytes that may be supplied */
203 ring_empty_count(Ring
*ring
)
205 if (ring_empty(ring
)) { /* if empty */
208 return ring_subtract(ring
, ring
->consume
, ring
->supply
);
212 /* number of CONSECUTIVE bytes that may be supplied */
214 ring_empty_consecutive(Ring
*ring
)
216 if ((ring
->consume
< ring
->supply
) || ring_empty(ring
)) {
218 * if consume is "below" supply, or empty, then
219 * return distance to the top
221 return ring_subtract(ring
, ring
->top
, ring
->supply
);
224 * else, return what we may.
226 return ring_subtract(ring
, ring
->consume
, ring
->supply
);
230 /* Return the number of bytes that are available for consuming
231 * (but don't give more than enough to get to cross over set mark)
235 ring_full_count(Ring
*ring
)
237 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
238 if (ring_full(ring
)) {
239 return ring
->size
; /* nothing consumed, but full */
241 return ring_subtract(ring
, ring
->supply
, ring
->consume
);
244 return ring_subtract(ring
, ring
->mark
, ring
->consume
);
249 * Return the number of CONSECUTIVE bytes available for consuming.
250 * However, don't return more than enough to cross over set mark.
253 ring_full_consecutive(Ring
*ring
)
255 if ((ring
->mark
== 0) || (ring
->mark
== ring
->consume
)) {
256 if ((ring
->supply
< ring
->consume
) || ring_full(ring
)) {
257 return ring_subtract(ring
, ring
->top
, ring
->consume
);
259 return ring_subtract(ring
, ring
->supply
, ring
->consume
);
262 if (ring
->mark
< ring
->consume
) {
263 return ring_subtract(ring
, ring
->top
, ring
->consume
);
264 } else { /* Else, distance to mark */
265 return ring_subtract(ring
, ring
->mark
, ring
->consume
);
271 * Move data into the "supply" portion of of the ring buffer.
274 ring_supply_data(Ring
*ring
, unsigned char *buffer
, int count
)
279 i
= MIN(count
, ring_empty_consecutive(ring
));
280 memmove(ring
->supply
, buffer
, i
);
281 ring_supplied(ring
, i
);
290 * Move data from the "consume" portion of the ring buffer
293 ring_consume_data(Ring
*ring
, unsigned char *buffer
, int count
)
298 i
= MIN(count
, ring_full_consecutive(ring
));
299 memmove(buffer
, ring
->consume
, i
);
300 ring_consumed(ring
, i
);
309 ring_encrypt(Ring
*ring
, void (*encryptor
)(unsigned char *, int))
311 unsigned char *s
, *c
;
313 if (ring_empty(ring
) || ring
->clearto
== ring
->supply
)
316 if (!(c
= ring
->clearto
))
322 (*encryptor
)(c
, ring
->top
- c
);
323 (*encryptor
)(ring
->bottom
, s
- ring
->bottom
);
325 (*encryptor
)(c
, s
- c
);
327 ring
->clearto
= ring
->supply
;
331 ring_clearto(Ring
*ring
)
334 if (!ring_empty(ring
))
335 ring
->clearto
= ring
->supply
;
339 #endif /* ENCRYPTION */