功能:
用C实现的环形队列,只支持单写单读(即不支持多线程),队列中的数据块的大小和个数可以自定义(可以传结构体)。
文件:
debug.h -> 调试用
k_fifo.c -> 环形队列实现
k_fifo.h -> 头文件
main.c -> 测试用例
代码:
//k_fifo.c
#include
#include
#include
#include "k_fifo.h"
#include "debug.h"
K_FIFO* fifo_create(int chunk_size, int chunk_count)
{
if (chunk_size <= 0 || chunk_count <= 0)
{
DEBUG_ERROR("input is illegal");
return NULL;
}
K_FIFO* fifo_ptr = (K_FIFO*)(malloc(sizeof(K_FIFO)));
if (fifo_ptr == NULL)
{
DEBUG_ERROR("malloc failed");
return NULL;
}
memset(fifo_ptr, 0, sizeof(K_FIFO));
fifo_ptr->write_ptr = 0;
fifo_ptr->read_ptr = 0;
fifo_ptr->flag = FIFO_STATUS_EMPTY;
fifo_ptr->chunk_count = chunk_count;
fifo_ptr->chunk_size = chunk_size;
fifo_ptr->data = (char*)calloc(chunk_count, chunk_size);
if (fifo_ptr->data == NULL)
{
DEBUG_ERROR("calloc failed");
free(fifo_ptr);
return NULL;
}
DEBUG_INFO("fifo_create success");
return fifo_ptr;
}
void fifo_destroy(K_FIFO* fifo_ptr)
{
if (fifo_ptr == NULL)
{
DEBUG_ERROR("fifo_ptr is NULL");
return;
}
if (fifo_ptr->data != NULL)
{
free(fifo_ptr->data);
fifo_ptr->data = NULL;
}
free(fifo_ptr);
fifo_ptr == NULL;
DEBUG_INFO("fifo_destroy success");
}
int fifo_in_a_chunk(K_FIFO* fifo_ptr, char* chunk_in, int chunk_size)
{
if ( fifo_ptr == NULL || chunk_in == NULL || chunk_size < 0)
{
DEBUG_ERROR("input illegal");
return -1;
}
// chunk_size太大
if ( chunk_size > fifo_ptr->chunk_size)
{
DEBUG_ERROR("chunk_size -> %d", chunk_size);
return -1;
}
if (fifo_ptr->flag != FIFO_STATUS_FULL)
{
memset(fifo_ptr->data + fifo_ptr->write_ptr * fifo_ptr->chunk_size, 0, sizeof(fifo_ptr->chunk_size));
memcpy(fifo_ptr->data + fifo_ptr->write_ptr * fifo_ptr->chunk_size, chunk_in, chunk_size);
fifo_ptr->write_ptr ++;
fifo_ptr->write_ptr %= fifo_ptr->chunk_count;
if((fifo_ptr->write_ptr - fifo_ptr->read_ptr) == -1 ||
(fifo_ptr->write_ptr == fifo_ptr->chunk_size - 1 && fifo_ptr->read_ptr == 0))
{
fifo_ptr->flag = FIFO_STATUS_FULL;
}
else
{
fifo_ptr->flag = FIFO_STATUS_NORMAL;
}
}
// 队列满,写入错误,警告之
else
{
DEBUG_WARN("fifo is full, write failed");
return -1;
}
}
int fifo_out_a_chunk(K_FIFO* fifo_ptr, char* chunk_out, int *chunk_size)
{
if ( fifo_ptr == NULL || chunk_out == NULL || chunk_size == 0)
{
DEBUG_ERROR("input illegal");
return -1;
}
if(fifo_ptr->flag != FIFO_STATUS_EMPTY)
{
memcpy(chunk_out, fifo_ptr->data + fifo_ptr->read_ptr * fifo_ptr->chunk_size, fifo_ptr->chunk_size);
fifo_ptr->read_ptr ++;
fifo_ptr->read_ptr %= fifo_ptr->chunk_count;
if(fifo_ptr->read_ptr == fifo_ptr->write_ptr)
{
fifo_ptr->flag = FIFO_STATUS_EMPTY;
}
else
{
fifo_ptr->flag = FIFO_STATUS_NORMAL;
}
}
// 队列空,读取错误,警告之
else
{
DEBUG_WARN("fifo is empty, read failed");
return -1;
}
return 0;
}
void fifo_dump_a_chunk(K_FIFO* fifo_ptr, int chunk_index)
{
if (fifo_ptr->chunk_count <= chunk_index || chunk_index < 0)
{
DEBUG_ERROR("chunk_index is illegal");
return;
}
DEBUG_INFO("chunk_count -> %d chunk_index ->%d", fifo_ptr->chunk_count, chunk_index);
dprintfbin(fifo_ptr->data + fifo_ptr->chunk_size * chunk_index, fifo_ptr->chunk_size);
}
void fifo_dump_all(K_FIFO* fifo_ptr)
{
if (fifo_ptr == NULL)
{
DEBUG_ERROR("input is illegal");
return;
}
DEBUG_INFO("====>START<====");
DEBUG_INFO("chunk_count -> %d", fifo_ptr->chunk_count);
DEBUG_INFO("chunk_size -> %d", fifo_ptr->chunk_size);
DEBUG_INFO("read_ptr -> %d", fifo_ptr->read_ptr);
DEBUG_INFO("write_ptr -> %d", fifo_ptr->write_ptr);
DEBUG_INFO("flag -> %d", fifo_ptr->flag);
int index = 0;
// r --- w
if (fifo_ptr->write_ptr > fifo_ptr->read_ptr)
{
for(index = fifo_ptr->read_ptr; index < fifo_ptr->write_ptr; index++)
{
fifo_dump_a_chunk(fifo_ptr, index);
}
}
// w --- r
else if (fifo_ptr->write_ptr < fifo_ptr->read_ptr)
{
for(index = fifo_ptr->read_ptr; index < fifo_ptr->chunk_count; index++)
{
fifo_dump_a_chunk(fifo_ptr, index);
}
for(index = 0; index < fifo_ptr->write_ptr; index++)
{
fifo_dump_a_chunk(fifo_ptr, index);
}
}
DEBUG_INFO("=====>END<=====\n");
}
#ifndef _K_DEBUG_H_
#define _K_DEBUG_H_
#ifdef __cplusplus
extern "C" {
#endif
#include
#define __FILE_NAME__(x) strrchr(x,'/')?(strrchr(x,'/')+1):x
#define __DEBUG__
#ifdef __DEBUG__
#define DEBUG_ERROR(format,...) printf("[libmsgm: ERROR] File: %s, Line: %05d: "format"\n", __FILE_NAME__(__FILE__), __LINE__, ##__VA_ARGS__)
#define DEBUG_WARN(format,...) printf("[libmsgm: WARN ] File: %s, Line: %05d: "format"\n", __FILE_NAME__(__FILE__), __LINE__, ##__VA_ARGS__)
#define DEBUG_INFO(format,...) printf("[libmsgm: INFO ] File: %s, Line: %05d: "format"\n", __FILE_NAME__(__FILE__), __LINE__, ##__VA_ARGS__)
#else
#define DEBUG_ERROR(format,...)
#define DEBUG_WARN(format,...)
#define DEBUG_INFO(format,...)
#endif
#define dprintfbin(buf, size) \
do {\
int i; \
for (i = 0; i < size - 1; i++){ \
if (0 == i % 16){ \
if (0 != i)\
printf("\n");\
printf("0x%04x: ", i); \
}\
printf("%02x ", ((char*)buf)[i]);\
}\
printf("%02x\n", ((char*)buf)[i]); \
} while(0)
#ifdef __cplusplus
}
#endif
#endif
//k_fifo.h
#ifndef __K_FIFO_H__
#define __K_FIFO_H__
#ifdef __cplusplus
extern "C" {
#endif
#define FIFO_STATUS_EMPTY 0x00 // 闃熷垪鐘舵€佷负绌?
#define FIFO_STATUS_FULL 0x01 // 闃熷垪鐘舵€佷负婊?
#define FIFO_STATUS_NORMAL 0x02 // 闃熷垪鐘舵€佹甯?
typedef struct _K_FIFO{
char *data;
int read_ptr;
int write_ptr;
int chunk_size;
int chunk_count;
int flag;
}K_FIFO;
/*********************************************
1. 创建环形队列,队列中每一个块的数据大小固定
2. chunk_size -> 数据块的大小
3. chunk_count -> 数据块的个数
4. 返回值 -> NULL代表失败
*********************************************/
K_FIFO* fifo_create(int chunk_size, int chunk_count);
/*********************************************
1. 销毁环形队列
2. fifo_ptr -> fifo_create的返回值
*********************************************/
void fifo_destroy(K_FIFO* fifo_ptr);
/*********************************************
1. 向队列中插入一个数据块
2. fifo_ptr -> fifo_create的返回值
3. chunk_in -> 数据块
4. chunk_size -> 数据块大小
5. 返回值 -> 0代表成功,-1代表失败
*********************************************/
int fifo_in_a_chunk(K_FIFO* fifo_ptr, char* chunk_in, int chunk_size);
/*********************************************
1. 从队列中取出一个数据块
2. fifo_ptr -> fifo_create的返回值
3. chunk_out -> 取出的数据块
4. chunk_size -> 取出的数据块大小
5. 返回值 -> 0代表成功,-1代表失败
*********************************************/
int fifo_out_a_chunk(K_FIFO* fifo_ptr, char* chunk_out, int *chunk_size);
//debug
void fifo_dump_a_chunk(K_FIFO* fifo_ptr, int chunk_index);
void fifo_dump_all(K_FIFO* fifo_ptr);
#ifdef __cplusplus
}
#endif
#endif
//main.c
#include
#include "k_fifo.h"
#define MY_CHUNK_SIZE 4
#define MY_CHUNK_COUNT 4
typedef struct _CHUNK_DATA
{
char str[MY_CHUNK_SIZE];
}CHUNK_DATA;
int main()
{
K_FIFO *fifo_ptr = NULL;
fifo_ptr = fifo_create(MY_CHUNK_SIZE, MY_CHUNK_COUNT);
CHUNK_DATA data_in = {"123"};
CHUNK_DATA data_in1 = {"456"};
CHUNK_DATA data_in2 = {"789"};
CHUNK_DATA data_out = {"456"};
int data_out_size = 0;
fifo_dump_all(fifo_ptr);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in), MY_CHUNK_SIZE);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in1), MY_CHUNK_SIZE);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in), MY_CHUNK_SIZE);
fifo_out_a_chunk(fifo_ptr, (char*)(&data_out), &data_out_size);
fifo_out_a_chunk(fifo_ptr, (char*)(&data_out), &data_out_size);
fifo_out_a_chunk(fifo_ptr, (char*)(&data_out), &data_out_size);
fifo_dump_all(fifo_ptr);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in1), MY_CHUNK_SIZE);
fifo_out_a_chunk(fifo_ptr, (char*)(&data_out), &data_out_size);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in2), MY_CHUNK_SIZE);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in1), MY_CHUNK_SIZE);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in1), MY_CHUNK_SIZE);
fifo_in_a_chunk(fifo_ptr, (char*)(&data_in1), MY_CHUNK_SIZE);
fifo_dump_all(fifo_ptr);
fifo_destroy(fifo_ptr);
}