/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: fifo_buffer.h 1.4 89/08/07 $"

/*
 * FIFO definitions for buffered I/O.
 */

#ifndef _SYS_FIFO_BUFFER_H_

/* $Log:	fifo_buffer.h,v $
 */

/*
 * The following structure can be used by device
 * drivers that perform buffered I/O.  It provides 
 * a description of a simple fifo buffer.  There is a 
 * collection of functions and macros prefixed with 
 * 'fifo' for manipulating the fifo.  Note that they 
 * don't provide for wrap-around and that the fifo 
 * is empty when 'consumer == producer' and full 
 * when 'producer == limit'.
 */
struct fifo {
	caddr_t start;  		/* Address of the buffer for this
					 * fifo */
	caddr_t limit;  		/* Address of one byte beyond
					 * the last byte in the buffer */
	caddr_t consumer;		/* Next readable fifo data byte */
	caddr_t producer;		/* Next writable fifo byte location */
};

typedef struct fifo fifo_t;

/*
 * The following macro initializes the 'struct fifo'
 * addressed by 'fp' to describe a fifo of 'length' 
 * bytes located at address 'buffer'.  
 * 
 * It is provided instead of elegant allocation functions,
 * allowing the invoker to address concerns regarding static
 * vs. dynamic allocations, buffer alignment, and any 
 * boundaries restrictions.
 */
#define fifo_init(fp, buffer, length)	{ \
		(fp)->start = (fp)->consumer = (fp)->producer = (buffer); \
		(fp)->limit = (buffer) + (length); }

/*
 * The following macros calculate the data available
 * in the fifo addressed by 'fp' for consumer operations 
 * and the space available for producer operations.
 */
#define fifo_c_avail(fp)	((fp)->producer - (fp)->consumer)
#define fifo_p_avail(fp)	((fp)->limit - (fp)->producer)

/*
 * The following macros reset the fifo addressed by 'fp'
 * to its empty state.  In addition, fifo_clear zeroes 
 * the entire buffer.
 */
#define fifo_reset(fp)		((fp)->producer = (fp)->consumer = (fp)->start)
#define fifo_clear(fp)		{bzero((fp)->start, (fp)->limit - (fp)->start);\
				  fifo_reset(fp); }

#ifdef INKERNEL

#ifdef notyet
/*
 * External interfaces available in fifo.c
 */
extern void fifo_c_incr();
extern void fifo_p_incr();
extern int fifo_copyin();
extern int fifo_copyout();
extern void fifo_bcopyin();
extern void fifo_bcopyout();
extern void fifo_pad();
#endif /* notyet */

#endif /*INKERNEL*/

#define _SYS_FIFO_BUFFER_H_
#endif /*_SYS_FIFO_BUFFER_H_*/
