うんこーどだけどどこかで役に立つかも…立たないか(´д`)
【追記】使う度にfifo_init()する仕様です、さーせんm(__)m
tinyfifo.c
/* Tiny FIFO */
#include <stdio.h>
#define TINY_FIFO_SIZE 5
typedef unsigned char fifo_t;
typedef enum tiny_fifo_code {
FIFO_COMPLETE,
FIFO_ERROR
} fifo_e;
static struct s_tiny_fifo_cntx {
fifo_t buff[TINY_FIFO_SIZE];
int count;
int length;
} tiny_fifo;
static void fifo_init(void)
{
tiny_fifo.count = 0;
tiny_fifo.length = 0;
}
static int fifo_count(void)
{
return tiny_fifo.length - tiny_fifo.count;
}
static fifo_e fifo_set(fifo_t x)
{
fifo_e result = FIFO_ERROR;
if (tiny_fifo.length < TINY_FIFO_SIZE) {
tiny_fifo.buff[tiny_fifo.length++] = x;
result = FIFO_COMPLETE;
}
return result;
}
static fifo_e fifo_get(fifo_t *x)
{
fifo_e result = FIFO_ERROR;
if (x != NULL) {
if (tiny_fifo.count < tiny_fifo.length) {
*x = tiny_fifo.buff[tiny_fifo.count++];
result = FIFO_COMPLETE;
}
}
return result;
}
/* for TEST */
static fifo_e fifo_set_(fifo_t x)
{
fifo_e result = fifo_set(x);
if (result == FIFO_COMPLETE) {
printf("Set %d.\n", x);
}
else {
printf("ERROR : Set %d.\n", x);
}
return result;
}
static fifo_e fifo_get_(fifo_t *x)
{
fifo_e result = fifo_get(x);
if (result == FIFO_COMPLETE) {
printf("Get %d.\n", *x);
}
else {
printf("ERROR : Get.\n");
}
return result;
}
int main(int argc, char* argv[])
{
fifo_t x;
fifo_init();
fifo_set_(1);
fifo_set_(3);
fifo_get_(&x);
fifo_set_(8);
fifo_set_(9);
fifo_set_(2);
fifo_set_(6);
while (fifo_count()) {
fifo_get_(&x);
}
fifo_get_(&x);
getchar();
return 0;
}