C语言实现基于数组的环形缓冲队列

背景

在C语言编程场景中,常常需要对一段不定长数据进行缓存。这里提出一种基于数组的环形缓冲队列,解决上述场景的问题。

原理

如下图所示,首先定义数据长度为buf[8],初始化指针为in/out

其中in为缓冲数据的输入指针

out为缓冲数据的输出指针

缓存内写入一个数据

in++

如下图所示

缓存内读出一个数据

out++

如下图所示

这时判断in==out为true,所以表明缓存内没有数据。

如果把上述缓冲数组换成二维数组,代码可以编写如下

代码

buf_queue.c


/******************************************************************************

*******************************************************************************/

#include "buf_queue.h"
#include "stdio.h"
#include "string.h"

/******************************************************************************

******************************************************************************/
BUF_QUEUE bufQueue;


void InitQueue(BUF_QUEUE *pBufQueue)
{
    INT16U i;
    INT32U tmp;
    INT8U *p;
    p = (INT8U *)pBufQueue;
    tmp = sizeof(BUF_QUEUE) / sizeof(INT8U);
    for (i = 0; i < tmp; i++)
    {
        *p++ = 0;
    }
}

INT16U RxFromQueue(BUF_QUEUE *pBufQueue, INT8U *dst, INT16U len, INT8U *type)
{
    INT16U i;
    if (pBufQueue->In != pBufQueue->Out)
    {
		for(i = 0; i < len; i++)
		{
			*(dst + i) = pBufQueue->datBuf[pBufQueue->Out].buf[i];
		}
		*type = pBufQueue->datBuf[pBufQueue->Out].buf_type;
        pBufQueue->Out++;
        pBufQueue->Out &= (RX_SIZE_LEN-1);
        return 0;
    }
    return 1;
}

INT16U WxToQueue(BUF_QUEUE *pBufQueue, INT8U *dat, INT16U len, INT8U type)
{
    INT16U tmp, i;
    tmp = (pBufQueue->In+1) & (RX_SIZE_LEN-1);
    if (tmp != pBufQueue->Out)
    {
    	memset(pBufQueue->datBuf[pBufQueue->In].buf, 0, sizeof(pBufQueue->datBuf[pBufQueue->In].buf));
		for(i = 0; i < len; i++)
		{
			pBufQueue->datBuf[pBufQueue->In].buf[i] = *(dat + i);
		}
		pBufQueue->datBuf[pBufQueue->In].buf_type = type;
        pBufQueue->In++;
        pBufQueue->In &= (RX_SIZE_LEN - 1);
        return 0;
    }
    return 1;//
}
void clearQueue(BUF_QUEUE *pBufQueue)
{
	InitQueue(pBufQueue);
}
//---------------------------------------------------------------------------


buf_queue.h


/******************************************************************************

******************************************************************************/
#ifndef _DEF_QUEUE_CLASS_H_
#define _DEF_QUEUE_CLASS_H_

/******************************************************************************
*
******************************************************************************/
#include "stm32f4xx_hal.h"
#include "common.h"

/******************************************************************************
******************************************************************************/
#define RX_SIZE_LEN           32
#define MAX_BUF_LEN           100
#define BUF_TYPE_FROM_NB      1
#define BUF_TYPE_FROM_SENSOR  2


/******************************************************************************                                    *
******************************************************************************/
typedef struct 
{
	INT8U  buf_type;
    INT8U  buf[MAX_BUF_LEN];
} DAT_BUF;
// 
typedef struct 
{
    INT16U In;
    INT16U Out;
    DAT_BUF datBuf[RX_SIZE_LEN];
} BUF_QUEUE;

/******************************************************************************                                   *
******************************************************************************/
// 
extern void   InitQueue(BUF_QUEUE *pBufQueue);
extern INT16U RxFromQueue(BUF_QUEUE *pBufQueue, INT8U *dst, INT16U len, INT8U *type);
extern INT16U WxToQueue(BUF_QUEUE *pBufQueue, INT8U *dat, INT16U len, INT8U type);
extern void   clearQueue(BUF_QUEUE *pBufQueue);
extern BUF_QUEUE bufQueue;

#endif

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大牛攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值