|
Packit |
709fb3 |
/* definitions for a simple ring buffer
|
|
Packit |
709fb3 |
Copyright (C) 2006, 2009-2017 Free Software Foundation, Inc.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
709fb3 |
it under the terms of the GNU General Public License as published by
|
|
Packit |
709fb3 |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
709fb3 |
(at your option) any later version.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
709fb3 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
709fb3 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
709fb3 |
GNU General Public License for more details.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
You should have received a copy of the GNU General Public License
|
|
Packit |
709fb3 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include <stdbool.h>
|
|
Packit |
709fb3 |
#include "verify.h"
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
enum { I_RING_SIZE = 4 };
|
|
Packit |
709fb3 |
verify (1 <= I_RING_SIZE);
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
/* When ir_empty is true, the ring is empty.
|
|
Packit |
709fb3 |
Otherwise, ir_data[B..F] are defined, where B..F is the contiguous
|
|
Packit |
709fb3 |
range of indices, modulo I_RING_SIZE, from back to front, inclusive.
|
|
Packit |
709fb3 |
Undefined elements of ir_data are always set to ir_default_val.
|
|
Packit |
709fb3 |
Popping from an empty ring aborts.
|
|
Packit |
709fb3 |
Pushing onto a full ring returns the displaced value.
|
|
Packit |
709fb3 |
An empty ring has F==B and ir_empty == true.
|
|
Packit |
709fb3 |
A ring with one entry still has F==B, but now ir_empty == false. */
|
|
Packit |
709fb3 |
struct I_ring
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
int ir_data[I_RING_SIZE];
|
|
Packit |
709fb3 |
int ir_default_val;
|
|
Packit |
709fb3 |
unsigned int ir_front;
|
|
Packit |
709fb3 |
unsigned int ir_back;
|
|
Packit |
709fb3 |
bool ir_empty;
|
|
Packit |
709fb3 |
};
|
|
Packit |
709fb3 |
typedef struct I_ring I_ring;
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
void i_ring_init (I_ring *ir, int ir_default_val);
|
|
Packit |
709fb3 |
int i_ring_push (I_ring *ir, int val);
|
|
Packit |
709fb3 |
int i_ring_pop (I_ring *ir);
|
|
Packit |
709fb3 |
bool i_ring_empty (I_ring const *ir) _GL_ATTRIBUTE_PURE;
|