为什么80%的码农都做不了架构师?>>>   

Ring buffer streaming in general - how to implement

1997-06-19, Jarno Elonen <elonen@iki.fi> | URN:NBN:fi-fe20031153

This is a small note I wrote in 1997 while implementing a ring buffer. I couldn't find any good tutorial with suitably detailed instructions (e.g. source code in C) from the web back then - hope this fixes the lack. :)

Notations

LP = Load Pointer = CP = Consume Pointer =

Loaded, not usable area (always 1 byte) =
Loaded and usable area =

RBE = ring buffer end address (first byte which may not be consumed)
RBS = ring buffer start address (first byte which MAY be consumed)

The principle: CP never equals LP: there's always a 1 byte gap.
(Another way is to have an extra variable to tell wether the buffer is full or empty when LP and CP point to the same address. This method is used in the sample code below.)

Initialization

The first byte of the stream must be read in to the gap between CP and LP before starting the actual streaming.

Possible situations during buffering and consuming

Consume pointer in the beginning
(CP=RBS)
Most usual case
(CP>RBS, LP<EBS)
Wrap around
Buffer empty (LP=CP+1)
Buffer full (CP=LP+1)

Update cycle in C-like pseudo code

if (CP==RBE && LP!=RBS)CP = RBSif (LP==RBE && CP!=RBS)LP = RBSif (LP>CP)
{maxLoadBytes = RBE - LPmaxConsumeBytes = LP - CP - 1
}
else
{maxLoadBytes = CP - LP -1maxConsumeBytes = RBE - CP// Or if the consumer knows how to wrap around:// maxConsumeBytes = (RBE - CP) + (LP - RBS) - 1

...and after consuming or loading:

LP += maxLoadBytesCP += maxConsumeBytes

Implementation in C++

Here's a small C++ ring buffer class:

class Ring_Buffer
{
public:Ring_Buffer( void* buffer, unsigned int buffer_size ){m_buff = (unsigned char*)buffer;m_buff_end = m_buff + buffer_size;m_load_ptr = m_consume_ptr = m_buff;m_max_load = buffer_size;m_max_consume = m_data_in_buffer = 0;Update_State();}// Try to add data to the buffer. After the call, 'len' contains// the amount of bytes actually buffered.void Buffer_Data( const void* data, unsigned int& len ){if ( len > (unsigned int)m_max_load )len = (unsigned int)m_max_load;memcpy( m_load_ptr, data, len );m_load_ptr += len;m_data_in_buffer += len;Update_State();}// Request 'len' bytes from the buffer. After the call,// 'len' contains the amount of bytes actually copied.void Get_Data( void* outData, unsigned int& len ){if ( len > (unsigned int)m_max_consume )len = (unsigned int)m_max_consume;memcpy( outData, m_consume_ptr, len );m_consume_ptr += len;m_data_in_buffer -= len;Update_State();}// Tries to skip len bytes. After the call,// 'len' contains the realized skip.void SkipData( unsigned int& len ){unsigned int requestedSkip = len;for ( int i=0; i<2; ++i ) // This may wrap  so try it twice{int skip = (int)len;if ( skip > m_max_consume )skip = m_max_consume;m_consume_ptr += skip;m_data_in_buffer -= skip;len -= skip;Update_State();}len = requestedSkip - len;}// The amount of data the buffer can currently receive on one Buffer_Data() call.inline unsigned int Free_Space()    { return (unsigned int)m_max_load; }// The total amount of data in the buffer. Note that it may not be continuous: you may need// two successive calls to Get_Data() to get it all.inline unsigned int Buffered_Bytes() { return (unsigned int)m_data_in_buffer; }private:void Update_State(){if (m_consume_ptr == m_buff_end)  m_consume_ptr = m_buff;if (m_load_ptr == m_buff_end)     m_load_ptr = m_buff;if (m_load_ptr == m_consume_ptr){if ( m_data_in_buffer > 0 ){m_max_load = 0;m_max_consume = m_buff_end - m_consume_ptr;}else{m_max_load = m_buff_end - m_load_ptr;m_max_consume = 0;}}else if ( m_load_ptr > m_consume_ptr ){m_max_load = m_buff_end - m_load_ptr;m_max_consume = m_load_ptr - m_consume_ptr;}else{m_max_load = m_consume_ptr - m_load_ptr;m_max_consume = m_buff_end - m_consume_ptr;}}unsigned char *m_load_ptr, *m_consume_ptr;unsigned char *m_buff_end;unsigned char *m_buff;int m_max_load, m_max_consume, m_data_in_buffer;
};

转载于:https://my.oschina.net/lyr/blog/82013

Ring buffer streaming in general - how to imple...相关推荐

  1. SQL Server 环形缓冲区(Ring Buffer) -- 介绍

    SQL Server 环形缓冲区(Ring Buffer) -- 介绍 以下关于Ring Buffer的介绍转载自: http://zh.wikipedia.org/wiki/%E7%92%B0%E5 ...

  2. leetcode 622. Design Circular Queue | 622. 设计循环队列(Ring Buffer)

    题目 https://leetcode.com/problems/design-circular-queue/ 题解 Ring Buffer 的实现,rear 指向新插入的位置,front 指向最旧的 ...

  3. Java 环形缓冲器(Ring Buffer)

    环形缓冲器(Ring Buffer):环形队列,这里使用数组实现,但并未用上环形功能,因为设置了队满直接出队清空队列,如果只读取部分数据,又或者想要覆盖冲写,则可以用上环形功能 package cha ...

  4. SQL Server 环形缓冲区(Ring Buffer) -- 环形缓冲在AlwaysOn的应用

    SQL Server 环形缓冲区(Ring Buffer) -- 环形缓冲在AlwaysOn的应用 可以从SQL Server环形缓冲区得到一些诊断AlwaysOn的信息,或从sys.dm_os_ri ...

  5. 解析Disruptor:写入ring buffer

    原文地址http://mechanitis.blogspot.com/2011/07/dissecting-disruptor-writing-to-ring.html 这是Disruptor end ...

  6. linux+循环buffer,说说循环缓冲区(Ring Buffer)

    关于循环缓冲区(Ring Buffer)的概念,其实来自于Linux内核(Maybe),是为解决某些特殊情况下的竞争问题提供了一种免锁的方法.这种特殊的情况就是当生产者和消费者都只有一个,而在其它情况 ...

  7. Ring Buffer (circular Buffer)环形缓冲区简介

    https://blog.csdn.net/langeldep/article/details/8888582 关于环形缓冲区的知识,请看这里 http://en.wikipedia.org/wiki ...

  8. Linux ftrace 1.1、ring buffer

    1.简介 ringbuffer是trace框架的一个基础,所有的trace原始数据都是通过ringbuffer记录的.ringbuffer的作用主要有几个: 1.存储在内存中,速度非常快,对系统性能的 ...

  9. 网卡的 Ring Buffer 详解

    网卡的 Ring Buffer 详解 1. 网卡处理数据包流程 网卡处理网络数据流程图: 图片来自参考链接1 上图中虚线步骤的解释: DMA 将 NIC 接收的数据包逐个写入 sk_buff ,一个数 ...

  10. Ring Buffer 的应用

    origin: http://blog.codingnow.com/2012/02/ring_buffer.html 这是一篇命题作文,源于今天在微薄上的一系列讨(好吧,也可以说是吵架).其实方案没有 ...

最新文章

  1. 张亚勤2020寄语哥伦比亚大学毕业生:引领未知时代
  2. stm8s103k3 周期 捕获_STM8S103K3 - 主流基本型系列8位MCU,具有8 KB Flash、16 MHz CPU和集成EEPROM - STMicroelectronics...
  3. django 1.8 官方文档翻译: 3-4-2 内建显示视图
  4. 【Flink】requested virtual cores for application master 1 exceeds the maximum number of virtual cores
  5. 【数学建模】基于matlab GUI最小二乘法曲线拟合【含Matlab源码 492期】
  6. 沃德天,Python竟然还能做实时翻译
  7. 大学生 大创 软著申请指南(图文含模板)(专用于大创,这篇就够了!纯干货!)
  8. Anycubic Vyper 3D打印机串口屏改造开源项目之串口屏项目启动篇(一)
  9. RK3399 Thermal (温度控制)
  10. JScript服务器运行,jscript模拟的“控制台”程序Web服务器教程
  11. 江在川上曰:云服务器上的flask项目部署(Ubuntu+Flask+Gunicorn+Supervisor+Nginx+Anaconda)
  12. PHP是专为后端,后端开发PHP入门必备
  13. 《生命不息,折腾不止》 罗永浩著
  14. C语言可以敲哪些小游戏,C语言可以写哪些小游戏?
  15. 计算机主机房,计算机机房建设组成及划分标准
  16. oracle数据库left用法,ORACLE 左联接 left join
  17. linux 开机自动加载mod,linux 添加elasticsearch 开机重启(自启动)
  18. SitePoint播客#144:免费增补Schmeemium
  19. Paper简读 - ChatGPT相关的GPT-1、GPT-2、GPT-3
  20. 【Zilliz专场】力扣第 271 场周赛复盘

热门文章

  1. SPSS篇—卡方检验
  2. Excel取消合并单元格时在每个单元格中保留内容,你会批量操作吗?
  3. 计算机网络提供信息传输服务,【判断题】计算机网络通常被划分为通信子网和资源子网,通信子网提供信息传输服务,资源子网提供共享资源。...
  4. YOLOX安装及训练
  5. android 8.0 无法接受到静态广播
  6. python设计查询余额程序_Python 小案例实战 —— 简易银行存取款查询系统
  7. 8类网线利弊_超6类7类8类网线进来挨打 6类线全面测评 网速和传输速率测试
  8. 如何在两台电脑之间共享文件
  9. 谷歌浏览器Network详解
  10. 近期每日学习与工作时间安排