| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/** \file |
| 6 |
\brief Functions to handle a fifo (first in, first out) structure |
| 7 |
*/ |
| 8 |
|
| 9 |
/****************************************************************************/ |
| 10 |
/* */ |
| 11 |
/* File: fifo.c */ |
| 12 |
/* */ |
| 13 |
/* Purpose: general purpose first in first out queue */ |
| 14 |
/* */ |
| 15 |
/* Author: Peter Bastian */ |
| 16 |
/* Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */ |
| 17 |
/* Universitaet Heidelberg */ |
| 18 |
/* Im Neuenheimer Feld 368 */ |
| 19 |
/* 6900 Heidelberg */ |
| 20 |
/* */ |
| 21 |
/* History: 30.01.92 begin, ug version 2.0 */ |
| 22 |
/* */ |
| 23 |
/* Revision: 07.09.95 */ |
| 24 |
/* */ |
| 25 |
/****************************************************************************/ |
| 26 |
|
| 27 |
/****************************************************************************/ |
| 28 |
/* */ |
| 29 |
/* include files */ |
| 30 |
/* system include files */ |
| 31 |
/* application include files */ |
| 32 |
/* */ |
| 33 |
/****************************************************************************/ |
| 34 |
|
| 35 |
#include <config.h> |
| 36 |
#include <cstdio> |
| 37 |
|
| 38 |
#include "ugtypes.h" |
| 39 |
#include "fifo.h" |
| 40 |
|
| 41 |
USING_UG_NAMESPACE |
| 42 |
|
| 43 |
/****************************************************************************/ |
| 44 |
/* */ |
| 45 |
/* definition of variables global to this source file only (static!) */ |
| 46 |
/* */ |
| 47 |
/****************************************************************************/ |
| 48 |
|
| 49 |
|
| 50 |
/****************************************************************************/ |
| 51 |
/** \brief Initialize fifo data structure |
| 52 |
|
| 53 |
\param myfifo - pointer to a fifo record |
| 54 |
\param buffer - pointer to a memory area for the fifo |
| 55 |
\param size - size of the buffer in bytes |
| 56 |
|
| 57 |
This function initializes 'fifo' (first in, first out) data structure. |
| 58 |
|
| 59 |
RETURN VALUE: |
| 60 |
.n size of fifo record |
| 61 |
.n 0 if size lower or equal to zero |
| 62 |
*/ |
| 63 |
/****************************************************************************/ |
| 64 |
|
| 65 |
INT NS_PREFIX fifo_init (FIFO *myfifo, void *buffer, INT size) |
| 66 |
{ |
| 67 |
myfifo->size = size / sizeof(void *); |
| 68 |
if (myfifo->size<=0) return(0); |
| 69 |
myfifo->elements = (void **) buffer; |
| 70 |
myfifo->start = myfifo->end = myfifo->used = 0; |
| 71 |
return(myfifo->size); |
| 72 |
} |
| 73 |
|
| 74 |
/****************************************************************************/ |
| 75 |
/** \brief Reset a previously initialized FIFO structure |
| 76 |
|
| 77 |
\param myfifo - pointer to FIFO structure |
| 78 |
|
| 79 |
This function resets a previously initialized FIFO structure. |
| 80 |
*/ |
| 81 |
/****************************************************************************/ |
| 82 |
|
| 83 |
void NS_PREFIX fifo_clear (FIFO *myfifo) |
| 84 |
{ |
| 85 |
myfifo->start = myfifo->end = myfifo->used = 0; |
| 86 |
} |
| 87 |
|
| 88 |
/****************************************************************************/ |
| 89 |
/** \brief Test if fifo is empty |
| 90 |
|
| 91 |
\param myfifo - pointer to fifo structure |
| 92 |
|
| 93 |
This function tests if fifo is empty. |
| 94 |
|
| 95 |
RETURN VALUE: |
| 96 |
.n 0 if fifo is not empty |
| 97 |
.n 1 if fifo is empty |
| 98 |
*/ |
| 99 |
/****************************************************************************/ |
| 100 |
|
| 101 |
INT NS_PREFIX fifo_empty (const FIFO *myfifo) |
| 102 |
{ |
| 103 |
if (myfifo->used==0) |
| 104 |
return(1); |
| 105 |
else |
| 106 |
return(0); |
| 107 |
} |
| 108 |
|
| 109 |
/****************************************************************************/ |
| 110 |
/** \brief Test if fifo is full |
| 111 |
|
| 112 |
\param myfifo - pointer to FIFO structure |
| 113 |
|
| 114 |
This function tests if fifo is full. |
| 115 |
|
| 116 |
RETURN VALUE: |
| 117 |
.n 0 if fifo is not empty |
| 118 |
.n 1 if fifo is empty |
| 119 |
*/ |
| 120 |
/****************************************************************************/ |
| 121 |
|
| 122 |
INT NS_PREFIX fifo_full (const FIFO *myfifo) |
| 123 |
{ |
| 124 |
if (myfifo->used==myfifo->size) |
| 125 |
return(1); |
| 126 |
else |
| 127 |
return(0); |
| 128 |
} |
| 129 |
|
| 130 |
/****************************************************************************/ |
| 131 |
/** \brief Insert an element in the fifo |
| 132 |
|
| 133 |
\param myfifo - pointer to FIFO structure |
| 134 |
\param newelement - pointer to the new element (any type!) |
| 135 |
|
| 136 |
This function inserts an element in the fifo. |
| 137 |
|
| 138 |
RETURN VALUE: |
| 139 |
.n 1 if error occurred. |
| 140 |
.n 0 if OK |
| 141 |
*/ |
| 142 |
/****************************************************************************/ |
| 143 |
|
| 144 |
INT NS_PREFIX fifo_in (FIFO *myfifo, void *newelement) |
| 145 |
{ |
| 146 |
if (myfifo->used<myfifo->size) |
| 147 |
{ |
| 148 |
(myfifo->elements)[myfifo->end] = newelement; |
| 149 |
myfifo->end = (myfifo->end+1)%myfifo->size; |
| 150 |
myfifo->used++; |
| 151 |
return(0); |
| 152 |
} |
| 153 |
else |
| 154 |
return(1); |
| 155 |
} |
| 156 |
|
| 157 |
/****************************************************************************/ |
| 158 |
/** \brief Get an element from the fifo |
| 159 |
|
| 160 |
\param myfifo - pointer to fifo structure |
| 161 |
|
| 162 |
This function gets an element from the fifo. |
| 163 |
|
| 164 |
RETURN VALUE: |
| 165 |
.n pointer to the element to take out from the fifo |
| 166 |
.n NULL if fifo is empty |
| 167 |
*/ |
| 168 |
/****************************************************************************/ |
| 169 |
|
| 170 |
void *NS_PREFIX fifo_out (FIFO *myfifo) |
| 171 |
{ |
| 172 |
INT i; |
| 173 |
|
| 174 |
if (myfifo->used==0) |
| 175 |
return(NULL); |
| 176 |
else |
| 177 |
{ |
| 178 |
i = myfifo->start; |
| 179 |
myfifo->start = (myfifo->start+1)%myfifo->size; |
| 180 |
myfifo->used--; |
| 181 |
return (myfifo->elements[i]); |
| 182 |
} |
| 183 |
} |