如何优雅地实现环形缓冲区?
时间:2021-10-18 16:43:35
手机看文章
扫描二维码
随时随地手机看文章
[导读]循环缓冲区是嵌入式软件工程师在日常开发过程中的关键组件。多年来,互联网上出现了许多不同的循环缓冲区实现和示例。我非常喜欢这个模块,可以GitHub上找到这个开源的CBUF.h模块。地址:https://github.com/barraq/BRBrain/blob/master/f...
循环缓冲区是嵌入式软件工程师在日常开发过程中的关键组件。多年来,互联网上出现了许多不同的循环缓冲区实现和示例。我非常喜欢这个模块,可以GitHub上找到这个开源的 CBUF.h 模块。地址:
https://github.com/barraq/BRBrain/blob/master/firmware/CBUF.hCBUF.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 




