当前位置:首页 > 嵌入式 > 嵌入式大杂烩
[导读]循环缓冲区是嵌入式软件工程师在日常开发过程中的关键组件。多年来,互联网上出现了许多不同的循环缓冲区实现和示例。我非常喜欢这个模块,可以GitHub上找到这个开源的CBUF.h模块。地址:https://github.com/barraq/BRBrain/blob/master/f...

循环缓冲区是嵌入式软件工程师在日常开发过程中的关键组件。

多年来,互联网上出现了许多不同的循环缓冲区实现和示例。我非常喜欢这个模块,可以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  
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭