1:、实现一
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#define RET_SUCESS (u32)0
#define RET_FAILED (u32)1
#define true (u8)1
#define false (u8)0
typedef unsigned char bool;
typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned short u16;
typedef struct fifo
{
u8 *pBuf;
u32 elementSize;
u32 count;
int head;
int tail;
bool isEmpty;
bool isFull;
}FIFO;
typedef struct fifo_ctrl
{
void (*init)(void);
void (*add)(int);
void (*get)(int);
} FIFO_CTRL;
FIFO* fifoInit(u32 count, u32 elemetSize)
{
FIFO *pFifo = (FIFO*)malloc(sizeof(FIFO));
assert(pFifo != NULL);
pFifo->count = count;
pFifo->elementSize = elemetSize;
pFifo->pBuf = (u8*)malloc(sizeof(u8)*elemetSize*count);
assert(pFifo != NULL);
pFifo->head = 0;
pFifo->tail = 0;
pFifo->isEmpty = true;
pFifo->isFull = false;
};
u32 fifoPut(FIFO * pFifo, u8* pBuff)
{
if (pFifo->isFull == 1)
{
//printf(" =>put eeeor, fifo is full!\n");
return RET_FAILED;
}
else
{
memcpy((u8*)(pFifo->pBuf + pFifo->head*pFifo->elementSize), pBuff, pFifo->elementSize);
}
//
pFifo->head++;
if(pFifo->head > (pFifo->count-1))
{
pFifo->head = 0;
}
if(pFifo->head == pFifo->tail)
{
pFifo->isFull = true;
}
if(pFifo->isEmpty)
{
pFifo->isEmpty = false;
}
return RET_SUCESS;
};
u32 fifoGet(FIFO * pFifo, u8* pBuff)
{
if (pFifo->isEmpty == 1)
{
//printf(" =>get error, fifo is empty!\n");
return RET_FAILED;
}
else
{
memcpy(pBuff, (u8*)(pFifo->pBuf + pFifo->tail*pFifo->elementSize), pFifo->elementSize);
memset((u8*)(pFifo->pBuf + pFifo->tail*pFifo->elementSize), 0, pFifo->elementSize);
}
//
pFifo->tail++;
if(pFifo->tail > (pFifo->count-1))
{
pFifo->tail = 0;
}
if(pFifo->tail == pFifo->head)
{
pFifo->isEmpty = true;
}
if(pFifo->isFull)
{
pFifo->isFull = false;
}
return RET_SUCESS;
};
typedef struct data_info
{
int idx;
char name[32];
}DATA_INFO;
int main(void)
{
int i = 0;
u32 ret = 0;
DATA_INFO dat = {0, "idx= "};
FIFO* pFifo = fifoInit(5, sizeof(DATA_INFO));
assert(pFifo != NULL);
//test get empty fifo
memset(&dat, 0, sizeof(DATA_INFO));
if(fifoGet(pFifo, (u8*)&dat) == 0)
{
printf("get [%d] [%s]\n", dat.idx, dat.name);
}
else
{
printf("get error!\n");
}
printf(" ----------- (get 1 ends)----------------------\n");
#if 1
for(i=1; i<=pFifo->count+1; i++)
{
dat.idx = i;
dat.name[5] = '0'+i;
if(fifoPut(pFifo, (u8*)&dat))
{
printf("put [%d] error!\n", i);
break;
}
else
{
printf("put [%d] ok!\n", i);
}
}
printf(" ----------- (put 5 ends)----------------------\n");
for(i=1; i<=2; i++)
{
memset(&dat, 0, sizeof(DATA_INFO));
if(fifoGet(pFifo, (u8*)&dat) == 0)
{
printf("get [%d] [%s]\n", dat.idx, dat.name);
}
else
{
printf("get error!\n");
}
}
printf(" ----------- (get 2 ends)----------------------\n");
for(i=1; i<=4; i++)
{
dat.idx = i;
dat.name[7] = '0'+i;
if(fifoPut(pFifo, (u8*)&dat))
{
printf("put error!\n");
//break;
}
else
{
printf("put ok!\n");
}
}
printf(" ----------- (put 4 ends)----------------------\n");
for(i=1; i<=7; i++)
{
memset(&dat, 0, sizeof(DATA_INFO));
if(fifoGet(pFifo, (u8*)&dat) == 0)
{
printf("get [%d] [%s]\n", dat.idx, dat.name);
}
else
{
printf("get error!\n");
}
}
printf(" ----------- (get 7 ends)----------------------\n");
#endif
return 0;
}
2、实现方法二 (更简洁一些)
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#define RET_SUCESS (u32)0
#define RET_FAILED (u32)1
typedef unsigned char u8;
typedef unsigned int u32;
typedef struct fifo
{
void *pBuf; //data point
u32 elementSize; //sigle data size
u32 maxElementCount; //max data numbers
u32 curElementCount; //current data numbers in fifo
u32 head;
u32 tail;
}FIFO;
FIFO* fifoInit(u32 count, u32 elemetSize)
{
FIFO *pFifo = (FIFO*)malloc(sizeof(FIFO));
assert(pFifo != NULL);
pFifo->maxElementCount = count;
pFifo->elementSize = elemetSize;
pFifo->pBuf = (void*)malloc(sizeof(u8)*elemetSize*count);
assert(pFifo != NULL);
pFifo->head = 0;
pFifo->tail = 0;
pFifo->curElementCount = 0;
}
u32 fifoPut(FIFO * pFifo, u8* pBuff)
{
if (pFifo->curElementCount == pFifo->maxElementCount)
{
//printf(" =>put error, fifo is full!\n");
return RET_FAILED;
}
else
{
memcpy((u8*)((u8*)(pFifo->pBuf) + pFifo->head*pFifo->elementSize), pBuff, pFifo->elementSize);
}
//
pFifo->curElementCount++;
pFifo->head++;
if(pFifo->head > (pFifo->maxElementCount-1))
{
pFifo->head = 0;
}
return RET_SUCESS;
}
u32 fifoGet(FIFO * pFifo, u8* pBuff)
{
if (pFifo->curElementCount == 0)
{
//printf(" =>get error, fifo is empty!\n");
return RET_FAILED;
}
else
{
memcpy(pBuff, (u8*)(pFifo->pBuf + pFifo->tail*pFifo->elementSize), pFifo->elementSize);
memset((u8*)((u8*)(pFifo->pBuf) + pFifo->tail*pFifo->elementSize), 0, pFifo->elementSize);
}
//
pFifo->curElementCount--;
pFifo->tail++;
if(pFifo->tail > (pFifo->maxElementCount-1))
{
pFifo->tail = 0;
}
return RET_SUCESS;
}