博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ring buffer
阅读量:6680 次
发布时间:2019-06-25

本文共 4694 字,大约阅读时间需要 15 分钟。

#ifndef __RING_BUFFER_H__#define __RING_BUFFER_H__//------------------------------------------------------------------------------//         External functions//------------------------------------------------------------------------------void BSP_Lock( void );void BSP_Unlock( void );typedef struct{  unsigned char * buffer;  unsigned int capacity;  unsigned int pointer;  unsigned int count;} ring_buffer_t;ring_buffer_t *ring_buffer_create( unsigned int size );void ring_buffer_delete( ring_buffer_t *ring_buffer );void ring_buffer_init( ring_buffer_t *ring_buffer, unsigned char * buffer,  unsigned int capacity );void ring_buffer_clear( ring_buffer_t *ring_buffer );unsigned int ring_buffer_readable( ring_buffer_t *ring_buffer );unsigned int ring_buffer_writable( ring_buffer_t *ring_buffer );void ring_buffer_flush( ring_buffer_t *ring_buffer, unsigned int bytes );unsigned int ring_buffer_read( ring_buffer_t *ring_buffer, unsigned char *data,  unsigned int length );unsigned int ring_buffer_write( ring_buffer_t *ring_buffer,  const unsigned char *data, unsigned int length );unsigned int ring_buffer_full( ring_buffer_t *ring_buffer );unsigned int ring_buffer_empty( ring_buffer_t *ring_buffer );#endif /* __RING_BUFFER_H__ */
#include "ring_buffer.h"#include 
#include
unsigned int ring_buffer_readable( ring_buffer_t *ring_buffer ){ return ring_buffer->count;}unsigned int ring_buffer_writable( ring_buffer_t *ring_buffer ){ return ring_buffer->capacity - ring_buffer->count;}unsigned int ring_buffer_full( ring_buffer_t *ring_buffer ){ return ring_buffer->count == ring_buffer->capacity;}unsigned int ring_buffer_empty( ring_buffer_t *ring_buffer ){ return ring_buffer->count == 0;}unsigned int ring_buffer_read( ring_buffer_t *ring_buffer, unsigned char *data, unsigned int length ){ unsigned int buffer_readable = ring_buffer_readable( ring_buffer ); unsigned int bytes_read = length; if ( bytes_read > buffer_readable ) bytes_read = buffer_readable; if ( bytes_read == 0 ) return 0; // -----------RRRRRRRRRRRR if ( ring_buffer->pointer + bytes_read <= ring_buffer->capacity ) memcpy( data, ring_buffer->buffer + ring_buffer->pointer, bytes_read ); else // RRRRRR--------------RRRRR { unsigned int upper = ring_buffer->capacity - ring_buffer->pointer; unsigned int lower = bytes_read - upper; memcpy( data, ring_buffer->buffer + ring_buffer->pointer, upper ); memcpy( data + upper, ring_buffer->buffer, lower ); } BSP_Lock( ); ring_buffer->pointer = ( ring_buffer->pointer + bytes_read ) % ring_buffer->capacity; ring_buffer->count -= bytes_read; BSP_Unlock( ); return bytes_read;}unsigned int ring_buffer_write( ring_buffer_t *ring_buffer, const unsigned char *data, unsigned int length ){ unsigned int bytes_written = length; unsigned int buffer_writable = ring_buffer_writable( ring_buffer ); if ( bytes_written > buffer_writable ) bytes_written = buffer_writable; if ( bytes_written == 0 ) return 0; unsigned int write_position = ( ring_buffer->pointer + ring_buffer->count ) % ring_buffer->capacity; // --------WWWWWWWW--- if ( write_position + bytes_written <= ring_buffer->capacity ) memcpy( ring_buffer->buffer + write_position, data, bytes_written ); else // WWWWWWW-------WWWW { unsigned int upper = ring_buffer->capacity - write_position; unsigned int lower = bytes_written - upper; memcpy( ring_buffer->buffer + write_position, data, upper ); memcpy( ring_buffer->buffer, data + upper, lower ); } BSP_Lock( ); ring_buffer->count += bytes_written; BSP_Unlock( ); return bytes_written;}void ring_buffer_flush( ring_buffer_t *ring_buffer, unsigned int length ){ // we can't flush more bytes than there are BSP_Lock( ); if ( length > (unsigned int) ring_buffer->count ) length = ring_buffer->count; ring_buffer->count -= length; ring_buffer->pointer = ( ring_buffer->pointer + length ); ring_buffer->pointer %= ring_buffer->capacity; BSP_Unlock( );}void ring_buffer_clear( ring_buffer_t *ring_buffer ){ BSP_Lock( ); ring_buffer->count = 0; ring_buffer->pointer = 0; BSP_Unlock( );}void ring_buffer_init( ring_buffer_t *ring_buffer, unsigned char * buffer, unsigned int capacity ){ ring_buffer->buffer = buffer; ring_buffer->capacity = capacity; ring_buffer_clear( ring_buffer );}ring_buffer_t * ring_buffer_create( unsigned int size ){ void * p = malloc( sizeof(ring_buffer_t) + size ); ring_buffer_t *ring_buffer = (ring_buffer_t *) p; if ( ring_buffer == NULL ) return NULL; ring_buffer->capacity = size; ring_buffer_clear( ring_buffer ); return ring_buffer;}void ring_buffer_delete( ring_buffer_t *ring_buffer ){ free( ring_buffer );}

 

转载地址:http://jfnao.baihongyu.com/

你可能感兴趣的文章
开博说明
查看>>
Scala方法定义,方法和函数的区别,将方法转换成函数
查看>>
Hbase 读写 原理
查看>>
详解JDBC驱动的四种类型
查看>>
第十一次作业
查看>>
关于Android 2.2设置短信本机号码和头像的解决办法
查看>>
JZ-C-47
查看>>
【算法随记三】小半径中值模糊的急速实现(16MB图7.5ms实现) + Photoshop中蒙尘和划痕算法解读。...
查看>>
TPCC-MySQL(转自imysql.com)
查看>>
理解AngularJS的作用域Scope
查看>>
Android 中Webview 自适应屏幕
查看>>
ubuntu 为rabbitmq安装web插件管理界面(为了远程查看rabbitmq) 分类...
查看>>
redis的lists类型
查看>>
js面试题1
查看>>
阿铭每日一题 day 4 20180114
查看>>
股市鬼才操盘20年总结的20条投资心得(转)
查看>>
转载:C#中的泛型
查看>>
1.4 注册系统的逻辑与结构
查看>>
NOIP模拟2017.6.11解题报告
查看>>
rpm安装mysql
查看>>