📄 fifo.c
字号:
/******************************************************************************
**
** COPYRIGHT (C) 2001 Intel Corporation
**
** FILENAME: FIFO.c
**
** PURPOSE: A general purpose implementation of a FIFO
**
** $Modtime: 7/17/03 1:01p $
**
******************************************************************************/
/*
*******************************************************************************
* HEADER FILES
*******************************************************************************
*/
#include <stdio.h>
#include "systypes.h"
#include "DM_Errors.h"
#include "mallocx.h"
#define FIFO_GLOBALS
#include "FIFO.h"
/*
*******************************************************************************
*
* FUNCTION: FIFOalloc
*
* DESCRIPTION: Set up a FIFO.
*
* INPUT PARAMETERS:
* size - Size of requested FIFO in bytes.
* onFirstCharFnP - Pointer to a function to be executed if a FIFOput call
* causes the number of characters in the fIFO to go from zero to one.
* onLastCharFnP - Pointer to a function to be executed if a FIFOget call
* causes the number of characters in the FIFO to go from one to zero.
*
* RETURNS: A pointer to refer to this instance.
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: None.
*
* CALLS: None.
*
* CALLED BY: Anyone.
*
* PROTOTYPE:
* FIFO_T *FIFOalloc(UINT size, FnPVOID onFirstCharFnP, FnPVOID onLastCharFnP)
********************************************************************************
*/
FIFO_T *FIFOalloc(UINT size, FnPVOID onFirstCharFnP, FnPVOID onLastCharFnP)
{
FIFO_T *r;
r = malloc(size+sizeof(FIFO_T));
if (r != NULL) {
r->size = size;
r->overwrite = TRUE;
r->overrun = FALSE;
r->onFirstCharFnP = onFirstCharFnP;
r->onLastCharFnP = onLastCharFnP;
r->head = r->tail = r->buffer;
}
return r;
}
/*
*******************************************************************************
*
* FUNCTION: FIFOput
*
* DESCRIPTION: Put a character into a FIFO.
*
* INPUT PARAMETERS: A pointer to the FIFO instance, and the character to put.
*
* RETURNS: NOERROR if there was room, ERROR if not.
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: None.
*
* CALLS: r->onFirstCharFnP, if this call causes the number of
* characters in the fIFO to go from zero to one.
*
* CALLED BY: Anyone
*
* PROTOTYPE: ERRORTYPE FIFOput(FIFO_T *r, UCHAR c)
*
*******************************************************************************
*/
ErrorT FIFOput(FIFO_T *r, UCHAR c)
{
// disabledifenabledrememberstate();
*r->head = c;
if (r->head == r->tail) {
if (r->head == r->buffer+r->size) r->head = r->buffer;
else ++r->head;
(*r->onFirstCharFnP)();
// enableifwasenabled();
return(FALSE);
}
if (r->head == r->buffer+r->size) {
if (r->tail == r->buffer) {
if (r->overwrite) {
r->head = r->buffer;
++r->tail;
}
r->overrun = TRUE;
// enableifwasenabled();
return (TRUE);
}
r->head = r->buffer;
// enableifwasenabled();
return(FALSE);
}
if (r->head == r->tail-1) {
if (r->overwrite) {
++r->head;
if (r->tail == r->buffer+r->size) r->tail = r->buffer;
else ++r->tail;
}
r->overrun = TRUE;
// enableifwasenabled();
return (TRUE);
}
++r->head;
// enableifwasenabled();
return(FALSE);
}
/*
*******************************************************************************
*
* FUNCTION: FIFOget
*
* DESCRIPTION: Get a character from a FIFO.
*
* INPUT PARAMETERS: A pointer to the FIFO instance.
*
* RETURNS: A character if available, otherwise EOF.
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: None.
*
* CALLS: r->onLastCharFnP, if this call causes the number of
* characters in the fIFO to go from one to zero.
*
* CALLED BY: Anyone
*
* PROTOTYPE: INT16 FIFOget(FIFO_T *r)
*
*******************************************************************************
*/
INT16 FIFOget(FIFO_T *r)
{
INT16 c;
// disabledifenabledrememberstate();
if (r->head==r->tail) c = EOF;
else {
c = *r->tail;
if (r->tail==r->buffer+r->size) r->tail = r->buffer;
else ++r->tail;
if (r->head==r->tail) (*r->onLastCharFnP)();
}
// enableifwasenabled();
return (c);
}
/*
*******************************************************************************
*
* FUNCTION: FIFOpeek
*
* DESCRIPTION: Nondistructive get a character from a FIFO.
*
* INPUT PARAMETERS: A pointer to the FIFO instance.
*
* RETURNS: A character if available, otherwise EOF.
* The character is NOT removed from the FIFO!
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: None.
*
* CALLS: None.
*
* CALLED BY: Anyone.
*
* PROTOTYPE: INT16 FIFOpeek(FIFO_T *r)
*
*******************************************************************************
*/
INT16 FIFOpeek(FIFO_T *r)
{
INT16 c;
// disabledifenabledrememberstate();
if (r->head==r->tail) c = EOF;
else c = *r->tail;
// enableifwasenabled();
return (c);
}
/*
*******************************************************************************
*
* FUNCTION: FIFOcharsavailable
*
* DESCRIPTION: Get the number of characters in a FIFO.
*
* INPUT PARAMETERS: A pointer to the FIFO instance.
*
* RETURNS: The number of characters in the FIFO.
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: None.
*
* CALLS: None.
*
* CALLED BY: Anyone.
*
* PROTOTYPE: UINT FIFOcharsavailable(FIFO_T *r)
*
*******************************************************************************
*/
UINT FIFOcharsAvailable(FIFO_T *r)
{
INT x;
// disabledifenabledrememberstate();
x = (INT)(r->head - r->tail);
// enableifwasenabled();
if (x >= 0) return (UINT)x;
else return (UINT)(r->size + 1 + x);
}
/*
*******************************************************************************
*
* FUNCTION: FIFOoverwrite
*
* DESCRIPTION: Set the overwrite mode of the FIFOput function.
* The default is overwrite TRUE - for a FIFO to throw the oldest character
* away to make room for the newest. FALSE causes FIFOput to not store the
* latest character in the FIFO if there is no room.
*
* INPUT PARAMETERS: A pointer to the FIFO instance,
* and a TRUE/FALSE for the overwite mode.
*
* RETURNS: Nothing.
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: None.
*
* CALLS: None.
*
* CALLED BY: Anyone
*
* PROTOTYPE: void FIFOoverwrite(FIFO_T *r, BOOL overwrite)
*
*******************************************************************************
*/
void FIFOoverwrite(FIFO_T *r, BOOL overwrite)
{
r->overwrite = overwrite;
}
/*
*******************************************************************************
*
* FUNCTION: FIFOoverrun
*
* DESCRIPTION: Has an overrun of the FIFO occurred?
*
* INPUT PARAMETERS: A pointer to the FIFO instance.
*
* RETURNS: TRUE if an overrun occurred, otherwise FALSE.
*
* GLOBAL EFFECTS: Clears the overrun status.
*
* ASSUMPTIONS: None.
*
* CALLS: None.
*
* CALLED BY: Anyone.
*
* PROTOTYPE: BOOL FIFOoverrun(FIFO_T *r)
*
*******************************************************************************
*/
BOOL FIFOoverrun(FIFO_T *r)
{
BOOL x;
// disabledifenabledrememberstate();
x = r->overrun;
r->overrun = FALSE;
// enableifwasenabled();
return (x);
}
/*
*******************************************************************************
*
* FUNCTION: FIFOoverrunchar
*
* DESCRIPTION: Get the character that was discarded on FIFO overflow.
*
* INPUT PARAMETERS: A pointer to the FIFO instance.
*
* RETURNS: The character that was discarded.
*
* GLOBAL EFFECTS: None.
*
* ASSUMPTIONS: An overrun just occured.
*
* CALLS: None.
*
* CALLED BY: Anyone.
*
* PROTOTYPE: UCHAR FIFOoverrunchar(FIFO_T *r)
*
*******************************************************************************
*/
UCHAR FIFOoverrunChar(FIFO_T *r)
{
return (*r->head);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -