xref: /386bsd/usr/src/kernel/include/ringbuf.h (revision a2142627)
1 /*
2  * Copyright (c) 1989, 1990, 1991, 1992 William F. Jolitz, TeleMuse
3  * 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 software is a component of "386BSD" developed by
16 	William F. Jolitz, TeleMuse.
17  * 4. Neither the name of the developer nor the name "386BSD"
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  *	$Id: ringbuf.h,v 1.1 94/07/27 15:42:02 bill Exp Locker: bill $
22  *
23  * Ring buffers provide a contiguous, dense storage for
24  * character data used by the tty driver.
25  */
26 
27 #define	RBSZ 1024
28 
29 struct ringb {
30 	int	rb_sz;		/* ring buffer size */
31 	char	*rb_hd;	  	/* head of buffer segment to be read */
32 	char	*rb_tl;	  	/* tail of buffer segment to be written */
33 	char	rb_buf[RBSZ];	/* segment contents */
34 };
35 
36 /* sucessive location in ring buffer */
37 #define	RB_SUCC(rbp, p) \
38 		((p) >= (rbp)->rb_buf + RBSZ - 1 ? (rbp)->rb_buf : (p) + 1)
39 
40 /* roll over end of ring to bottom if needed */
41 #define	RB_ROLLOVER(rbp, p) \
42 		((p) > (rbp)->rb_buf + RBSZ - 1 ? (rbp)->rb_buf : (p))
43 
44 /* predecessor location in ring buffer */
45 #define	RB_PRED(rbp, p) \
46 		((p) <= (rbp)->rb_buf ? (rbp)->rb_buf + RBSZ - 1 : (p) - 1)
47 
48 /* length of data in ring buffer */
49 #define	RB_LEN(rp) \
50 		((rp)->rb_hd <= (rp)->rb_tl ? (rp)->rb_tl - (rp)->rb_hd : \
51 		RBSZ - ((rp)->rb_hd - (rp)->rb_tl))
52 
53 /* maximum amount of data that can be inserted into the buffer contiguously */
54 #define	RB_CONTIGPUT(rp) \
55 		(RB_PRED(rp, (rp)->rb_hd) < (rp)->rb_tl ?  \
56 			(rp)->rb_buf + RBSZ - (rp)->rb_tl : \
57 			RB_PRED(rp, (rp)->rb_hd) - (rp)->rb_tl)
58 
59 /* maximum amount of data that can be remove from buffer contiguously */
60 #define	RB_CONTIGGET(rp) \
61 		((rp)->rb_hd <= (rp)->rb_tl ? (rp)->rb_tl - (rp)->rb_hd : \
62 		(rp)->rb_buf + RBSZ - (rp)->rb_hd)
63 
64 /* prototypes */
65 #ifdef KERNEL
66 int putc(unsigned, struct ringb *);
67 int getc(struct ringb *);
68 int nextc(char **, struct ringb *);
69 int ungetc(unsigned, struct ringb *);
70 int unputc(struct ringb *);
71 void initrb(struct ringb *);
72 void catb(struct ringb *, struct ringb *);
73 #endif
74