优雅地用宏实现环形缓冲区
之前写的环行缓冲区文章
柔性数组和环形队列之间的故事
C语言,环形队列
循环缓冲区是嵌入式软件工程师在日常开发过程中的关键组件。
多年来,互联网上出现了许多不同的循环缓冲区实现和示例。我非常喜欢这个模块,可以GitHub上找到这个开源的 CBUF.h 模块。
地址:https://github.com/barraq/BRBrain/blob/master/firmware/CBUF.h
CBUF.h 模块使用宏实现循环缓冲区,具体源码如下所示;
#if !defined( CBUF_H )
#define CBUF_H /**< Include Guard *//* ---- Include Files ---------------------------------------------------- *//* ---- Constants and Types ---------------------------------------------- *//**
* Initializes the circular buffer for use.
*/ #define CBUF_Init( cbuf ) cbuf.m_getIdx = cbuf.m_putIdx = 0/**
* Returns the number of elements which are currently contained in the * circular buffer.
*/#define CBUF_Len( cbuf ) ((typeof( cbuf.m_putIdx ))(( cbuf.m_putIdx ) - ( cbuf.m_getIdx )))/**
* Appends an element to the end of the circular buffer
*/#define CBUF_Push( cbuf, elem ) (cbuf.m_entry)[ cbuf.m_putIdx++ & (( cbuf##_SIZE ) - 1 )] = (elem)/**
* Retrieves an element from the beginning of the circular buffer
*/#define CBUF_Pop( cbuf ) (cbuf.m_entry)[ cbuf.m_getIdx++ & (( cbuf##_SIZE ) - 1 )]/**
* Retrieves the i'th element from the beginning of the circular buffer
*/#define CBUF_Get( cbuf, idx ) (cbuf.m_entry)[( cbuf.m_getIdx + idx ) & (( cbuf##_SIZE ) - 1 )]/**
* Retrieves the i'th element from the end of the circular buffer
*/#define CBUF_GetEnd( cbuf, idx ) (cbuf.m_entry)[( cbuf.m_putIdx - idx - 1 ) & (( cbuf##_SIZE ) - 1 )]/**
* Determines if the circular buffer is empty
*/#define CBUF_IsEmpty( cbuf ) ( CBUF_Len( cbuf ) == 0 )/**
* Determines if the circular buffer is full.
*/#define CBUF_IsFull( cbuf ) ( CBUF_Len( cbuf ) == ( cbuf##_SIZE ))/**
* Determines if the circular buffer is currenly overflowed or underflowed.
*/#define CBUF_Error( cbuf ) ( CBUF_Len( cbuf ) > cbuf##_SIZE )#if defined( __cplusplus )template < class IndexType, unsigned Size, class EntryType >
class CBUF
{
public:CBUF(){m_getIdx = m_putIdx = 0;}IndexType Len() const { return m_putIdx - m_getIdx; }bool IsEmpty() const { return Len() == 0; }bool IsFull() const { return Len() == Size; }bool Error() const { return Len() > Size; }void Push( EntryType val ) {m_entry[ m_putIdx++ & ( Size - 1 )] = val;}EntryType Pop(){return m_entry[ m_getIdx++ & ( Size - 1 )];}private:volatile IndexType m_getIdx;volatile IndexType m_putIdx;EntryType m_entry[ Size ];};#endif // __cplusplus/* ---- Variable Externs ------------------------------------------------- */
/* ---- Function Prototypes ---------------------------------------------- *//** @} */#endif // CBUF_H
现在一般我不喜欢以这种方式使用宏,但实现已被证明是快速、高效且工作相对良好的,这是很难争论的。
循环缓冲区的设置非常简单。首先,需要定义循环缓冲区的大小。这是通过定义宏 myQ_SIZE
来完成的,同时记住缓冲区大小需要是 2 的幂。
然后通过创建一个 myQ
类型的变量来声明循环缓冲区。例如,如果 myQ_SIZE
定义为 64 字节,则可以定义 UART 的发送和接收缓冲区,如下面的图 1 所示。
在此示例中,myQ
被定义为静态以限制缓冲区的范围并声明为易失性,因为它们在中断内被修改。定义循环缓冲区只是第一步。为了分配缓冲区,必须将这些变量传递给 CBUF_INIT
宏,如下图 2 所示。
除了这个初始设置之外,缓冲区相当简单且易于使用。例如,可以使用 CBUF_PUSH
将通过串行接口接收 UART
接收的字符推送到循环缓冲区,如图 3 所示。
开发人员不仅希望将数据推送到循环缓冲区上,还希望从缓冲区弹出或获取数据。看到这一点的一个简单示例是需要获取字符并通过 UART 传输的串行发送器。图 4 中可以看到一个示例传输函数。
在健壮的应用程序中,还应检查循环缓冲区长度和溢出状态。CBUF 模块确实提供了能够检查这些重要指标的宏。
要记住的一个重要问题是,如果需要对 CBUF 本身进行任何调试,这是不可能的。无法为宏设置断点,因此如果出现问题,则需要对模块进行功能化以逐步执行和调试。
多年来使用这个模块虽然我没有发现任何问题。循环缓冲区是在嵌入式系统中与串行设备通信的一个重要方面。
循环缓冲区也很好理解,应该创建它们以便它们可以模块化并从一个应用程序到下一个应用程序重复使用。
到目前为止,CBUF
模块已被证明是这样一个模块,所以在这里,我强烈推荐一下这个模块。好了,今天的文章就到这里,我们下期再见。
再贴上代码的注释部分
/****************************************************************************
*
* Since this code originated from code which is public domain, I
* hereby declare this code to be public domain as well.
*
****************************************************************************/
/**
*
* @file CBUF.h
*
* @brief This file contains global definitions for circular buffer
* manipulation.
*
* These macros implement a circular buffer which employs get and put
* pointers, in such a way that mutual exclusion is not required
* (assumes one reader & one writer).
*
* It requires that the circular buffer size be a power of two, and the
* size of the buffer needs to smaller than the index. So an 8 bit index
* supports a circular buffer upto ( 1 << 7 ) = 128 entries, and a 16 bit index
* supports a circular buffer upto ( 1 << 15 ) = 32768 entries.
*
* The basis for these routines came from an article in Jack Ganssle's
* Embedded Muse: http://www.ganssle.com/tem/tem110.pdf
*
* In order to offer the most amount of flexibility for embedded environments
* you need to define a macro for the size.
*
* First, you need to name your circular buffer. For this example, we'll
* call it myQ.
*
* The size macro that needs to be defined will be the name of the
* circular buffer followed by _SIZE. The size must be a power of two
* and it needs to fit in the get/put indicies. i.e. if you use an
* 8 bit index, then the maximum supported size would be 128.
*
* The structure which defines the circular buffer needs to have 3 members
* m_getIdx, m_putIdx, and m_entry.
*
* m_getIdx and m_putIdx need to be unsigned integers of the same size.
*
* m_entry needs to be an array of xxx_SIZE entries, or a pointer to an
* array of xxx_SIZE entries. The type of each entry is entirely up to the
* caller.
*
* #define myQ_SIZE 64
*
* volatile struct
* {
* uint8_t m_getIdx;
* uint8_t m_putIdx;
* uint8_t m_entry[ myQ_SIZE ];
*
* } myQ;
*
* You could then use
*
* CBUF_Push( myQ, 'x' );
*
* to add a character to the circular buffer, or
*
* ch = CBUF_Pop( myQ );
*
* to retrieve an element from the buffer.
*
* If you happen to prefer to use C++ instead, there is a templatized
* version which requires no macros. You just declare 3 template parameters:
*
* - The type that should be used for the index
* - The size of the circular buffer
* - The type that should be used for the entry
*
* For example:
*
* CBUF< uint8_t, 64, char > myQ;
*
****************************************************************************/
推荐阅读:
专辑|Linux文章汇总
专辑|程序人生
专辑|C语言
我的知识小密圈
关注公众号,后台回复「1024」获取学习资料网盘链接。
欢迎点赞,关注,转发,在看,您的每一次鼓励,我都将铭记于心~
优雅地用宏实现环形缓冲区相关推荐
- c语言数组怎么环形阵列,C语言 用于大阵列的无复制线程安全环形缓冲区
对于大数组(10 ^ 7个元素)上的信号处理,我使用与环形缓冲区连接的不同线程.遗憾的是,只需要太多时间将数据复制到缓冲区和从缓冲区复制数据.当前实现基于boost :: lockfree :: sp ...
- STM32串口开发之环形缓冲区
01.简介 在之前的文章<stm32 串口详解>中,我们讲解了串口的基本应用,使用串口中断接收数据,串口中断发送回包(一般可以使用非中断形式发送回包,在数据接收不频繁的应用中.串口接收中断 ...
- 使用无锁队列(环形缓冲区)注意事项
环形缓冲区是生产者和消费者模型中常用的数据结构.生产者将数据放入数组的尾端,而消费者从数组的另一端移走数据,当达到数组的尾部时,生产者绕回到数组的头部.如果只有一个生产者和一个消费者,那么就可以做到免 ...
- 杰杰带你解读【机智云】环形缓冲区源码
前言 大家晚上好,我是杰杰,上个星期,研究了一下机智云的源码,也不能说是研究吧,就是看了看,人家既然能拿来做商业用,还是有很厉害的地方的,如果还不知道什么叫环形缓冲区(环形队列)的同学,请看--STM ...
- STM32进阶之串口环形缓冲区实现
文章目录 队列的概念 队列的特点 队列的常见两种形式 普通队列 环形队列 从队列到串口缓冲区的实现 定义一个结构体: 初始化 写入环形缓冲区的代码实现: 读取缓冲区的数据的代码实现: 测试效果 补充 ...
- 通用环形缓冲区 LwRB 使用指南
什么是 LwRB? LwRB 是一个开源.通用环形缓冲区库,为嵌入式系统进行了优化.源码点击这里(Github). LwRB 特性 使用 ANSI C99 编写 FIFO (先进先出) 无动态内存分配 ...
- 无锁环形缓冲区的详细解释
由以下博客的分析可以知道,内核的kfifo使用了很多技巧以实现其高效性.比如,通过限定写入的数据不能溢出和内存屏障实现在单线程写单线程读的情况下不使用锁.因为锁是使用在共享资源可能存在冲突的情况下.还 ...
- 架构设计:生产者/消费者模式 第6页:环形缓冲区的实现
2019独角兽企业重金招聘Python工程师标准>>> ◇判断"空"和"满" 上述的操作并不复杂,不过有一个小小的麻烦:空环和满环的时候,R和 ...
- 环形缓冲区: ringbuf.c
#cat aa.c /*ringbuf .c*/ #include<stdio.h> #include<ctype.h>#define NMAX 8 int iput = 0; ...
最新文章
- 04741计算机网络原理知识点,04741计算机网络原理知识点整理.doc
- 细颗粒度Singleton模式实现
- php表单时间转换为时间戳-175
- mysql特有语法_MySQL详细的基础语法
- 人口预测和阻尼-增长模型_使用分类模型预测利率-第2部分
- java线程安全问题原因及解决办法
- hue集成mysql报错_hue集成hive访问报database is locked
- 来看看优酷是如何测试 App 响应式布局的!
- vue ui没反应如何解决?
- Menubutton按钮弹出菜单
- LRC软件测试简历,C语言 LRC歌词文件解析
- Python Selenium 调用IE浏览器失败Unexpected error launching Internet Explorer解决方法
- Guava库学习:学习Guava Cache(二)Guava caches(2)
- Multilingual预训练的那些套路
- Echarts地图合并提取
- 俺也去了WinHec..............
- 详解Linux 和 GNU 系统的关系
- 《哲学100问》读书感想:为什么要做一个道德的人
- 图片翻译如何操作?图片翻译的方法分享.
- csc函数(csc函数值)