使用C语言实现缓冲区,提供三个函数,初始化,读写缓冲区。
代码如下:
ring_bffer.c文件
/**********************************************************************
* Copyright (c), 2012~2016 All Rights Reserved
* @file
* @brief
* @author
* @date 2016/05/23
* @version 1.0.0
* @par Revision History:
* Date Author Notes
* 2015/5/23 wangzhenghui file created
********************************************************************/
#include "ring_buffer.h"
#include "string.h"
/********************************************************************
** Function Name : ring_buffer_count
** Description : NONE
** Input para : NONE
** Output para : NONE
** Return Value : NONE
** Version : V0.1
** Author : wangzhenghui
*******************************************************************/
static int ring_buffer_count(struct ring_buffer_t *rb)
{
if(rb->wr_index >= rb->rd_index)
{
return (rb->wr_index - rb->rd_index);
}
else
{
return ((rb->wr_index + rb->buffer_size) - rb->rd_index);
}
}
/********************************************************************
** Function Name : ring_buffer_init
** Description : NONE
** Input para : NONE
** Output para : NONE
** Return Value : NONE
** Version : V0.1
** Author : wangzhenghui
*******************************************************************/
void ring_buffer_init(struct ring_buffer_t *rb,unsigned char *pool,unsigned short size)
{
/* initialize read and write index */
rb->rd_index = 0;
rb->wr_index = 0;
/* set buffer pool and size */
rb->buffer_ptr = pool;
rb->buffer_size = size;
}
/********************************************************************
** Function Name : ring_buffer_write
** Description : 填充环形缓冲区
** Input para :
** Output para : NONE
** Return Value : NONE
** Version : V0.1
** Author : wangzhenghui
*******************************************************************/
unsigned short ring_buffer_write(struct ring_buffer_t *rb,unsigned char *psrc,unsigned short wr_len)
{
unsigned short free_count = 0;
signed short over_flow_cnt = 0;
free_count = rb->buffer_size - ring_buffer_count(rb);
if(free_count > 0)
{
if(wr_len > free_count)
{
wr_len = free_count;
}
over_flow_cnt = rb->wr_index + wr_len - rb->buffer_size;
if(over_flow_cnt <= 0)
{
memcpy((unsigned char *)&rb->buffer_ptr[rb->wr_index],psrc,wr_len);
rb->wr_index += wr_len;
}
else
{
memcpy(rb->buffer_ptr + rb->wr_index ,psrc,wr_len - over_flow_cnt);
memcpy(rb->buffer_ptr + rb->wr_index + (wr_len - over_flow_cnt) ,psrc,over_flow_cnt);
rb->wr_index = over_flow_cnt;
}
}
else
{
wr_len = 0;
}
return wr_len;
}
/********************************************************************
** Function Name : ring_buffer_read
** Description : 读环形缓冲区
** Input para :
** Output para : NONE
** Return Value : NONE
** Version : V0.1
** Author : wangzhenghui
*******************************************************************/
unsigned short ring_buffer_read(struct ring_buffer_t *rb,unsigned char *pdest,unsigned short rd_len)
{
unsigned short valid_count = 0;
signed short over_flow_cnt = 0;
valid_count = ring_buffer_count(rb);
if(valid_count > 0)
{
if(rd_len > valid_count)
{
rd_len = valid_count;
}
over_flow_cnt = rb->rd_index + rd_len - rb->buffer_size;
if(over_flow_cnt <= 0)
{
memcpy(pdest,(unsigned char *)&rb->buffer_ptr[rb->rd_index],rd_len);
rb->rd_index += rd_len;
}
else
{
memcpy(pdest,(unsigned char *)&rb->buffer_ptr[rb->rd_index],rd_len - over_flow_cnt);
memcpy(pdest + rd_len - over_flow_cnt,rb->buffer_ptr,over_flow_cnt);
rb->rd_index = over_flow_cnt;
}
}
else
{
rd_len = 0;
pdest[0] = 0;
}
return rd_len;
}
/**********************************************************************
* Copyright (c), 2012~2016 xxx All Rights Reserved * @file * @brief * @author * @date 2016/05/23 * @version 1.0.0 * @par Revision History: * Date Author Notes * 2015/5/23 wangzhenghui file created ********************************************************************/ #ifndef _RING_BUFFER_H_ #define _RING_BUFFER_H_ #ifdef __cplusplus extern "C" { #endif // __cplusplus struct ring_buffer_t { unsigned char *buffer_ptr; unsigned short buffer_size; unsigned short rd_index; unsigned short wr_index; }; /******************************************************************** ** Function Name : ring_buffer_init ** Description : NONE ** Input para : NONE ** Output para : NONE ** Return Value : NONE ** Version : V0.1 ** Author : wangzhenghui *******************************************************************/ extern void ring_buffer_init(struct ring_buffer_t *rb,unsigned char *pool,unsigned short size); /******************************************************************** ** Function Name : ring_buffer_write ** Description : NONE ** Input para : NONE ** Output para : NONE ** Return Value : NONE ** Version : V0.1 ** Author : wangzhenghui *******************************************************************/ extern unsigned short ring_buffer_write(struct ring_buffer_t *rb,unsigned char *pdest,unsigned short rd_len); /******************************************************************** ** Function Name : ring_buffer_read ** Description : NONE ** Input para : NONE ** Output para : NONE ** Return Value : NONE ** Version : V0.1 ** Author : wangzhenghui *******************************************************************/ extern unsigned short ring_buffer_read(struct ring_buffer_t *rb,unsigned char *pdest,unsigned short rd_len); #ifdef __cplusplus } // extern "C" #endif // __cplusplus #endif //_RING_BUFFER_H_