typedef struct{void buffer[2];//void buffer[2] 无类型指针,指向不同数据类型的缓冲Buffvolatile uint8_t writeIndex;//volatile uint8_t writeIndex 缓冲写索引volatile uint8_t readIndex;//volatile uint8_t readIndex 缓冲读索引volatile uint8_t readAvaliable[2];//volatile uint8_t readAvaliable[2] 可读标志位} PingPongBuffer_t;/初始化存储管理模块/void PingPongBuffer_Init(PingPongBuffer_t ppbuf, void buf0, void buf1);/读取:获取的缓存/bool PingPongBuffer_GetReadBuf(PingPongBuffer_t ppbuf, void pReadBuf);/读标志:设置读标志/void PingPongBuffer_SetReadDone(PingPongBuffer_t ppbuf);/写入:获取可写位置/void PingPongBuffer_GetWriteBuf(PingPongBuffer_t ppbuf, void pWriteBuf);/设置写完成标志 /void PingPongBuffer_SetWriteDone(PingPongBuffer_t ppbuf);读写接口的实现所谓ping-pong buffer,也就是定义两个buffer当有数据进来的时候,负责写入buffer的进程就寻找第一个没有被占用而且可写的buffer,进行写入,写好之后,将占用flag释放,同时设置一个flag提示此buffer已经可读,然后再接下去找另外一个可写的buffer,写入新的数据而读入的进程也是一直对buffer状态进行检测,一旦发现没有被占用,而且已经可以被读,就把这个buffer的数据取出来,然后标志为可写/ @brief Ping-pong buffer initialization @param ppbuf: Pointer to the ping-pong buffer structure @param buf0: Pointer to the first buffer @param buf1: Pointer to the second buffer @retval None/void PingPongBuffer_Init(PingPongBuffer_t ppbuf, void buf0, void buf1){memset(ppbuf, 0, sizeof(PingPongBuffer_t));ppbuf->buffer[0] = buf0;ppbuf->buffer[1] = buf1;}/当设备有数据来时1.负责读取buffer的进程就寻找第一个没有被占用而且可读取的buff(ppbuf->readAvaliable==1)2.记录该buff的序号index(等读完后-对该buff设置一个flag提示此buffer已经不可读false)3.读取该索引的buff;将该索引buffer设置不可读// @brief Get a readable buffer @param ppbuf: Pointer to the ping-pong buffer structure @param pReadBuf: Pointer to the pointer to the buffer to be read @retval Returns true if there is a buffer to be read/bool PingPongBuffer_GetReadBuf(PingPongBuffer_t ppbuf, void pReadBuf){/负责读取buffer的进程就寻找第一个没有被占用而且可读取的buff/if(ppbuf->readAvaliable[0]){ppbuf->readIndex = 0;}else if(ppbuf->readAvaliable[1]){ppbuf->readIndex = 1;}else{return false;}/读取该索引的buff/pReadBuf = ppbuf->buffer[ppbuf->readIndex];return true;}/ @brief Notify buffer read completion @param ppbuf: Pointer to the ping-pong buffer structure @retval None/void PingPongBuffer_SetReadDone(PingPongBuffer_t ppbuf){ /将该索引buffer设置不可读/ppbuf->readAvaliable[ppbuf->readIndex] = false;}/当缓存数据要输出时,缓冲区0的数据放入缓冲区1,这时缓冲区0可接收下次数据工作区可从缓冲区1拿数据然后将缓存0的可读取标志置为true1.如果写索引和读索引都指向一个buff时,将另外一个buff给写索引,并将该索引buff写出如果写索引和读索引不一样,将该索引buff写出2.将该索引的可读标志设置为可读; 将可写的索引指向另外一个缓存// @brief Get writable buffer @param ppbuf: Pointer to the ping-pong buffer structure @param pWriteBuf: Pointer to the pointer to the buffer to be wriye @retval None/void PingPongBuffer_GetWriteBuf(PingPongBuffer_t ppbuf, void pWriteBuf){/如果写索引和读索引都指向一个buff时,将另外一个buff给写索引,并将该索引buff写出/if(ppbuf->writeIndex == ppbuf->readIndex){ppbuf->writeIndex = !ppbuf->readIndex;}/将该索引buff写出/pWriteBuf = ppbuf->buffer[ppbuf->writeIndex];}/ @brief Notify buffer write completion @param ppbuf: Pointer to the ping-pong buffer structure @retval None/void PingPongBuffer_SetWriteDone(PingPongBuffer_t ppbuf){ /将该索引的可读标志设置为可读/ppbuf->readAvaliable[ppbuf->writeIndex] = true;/将可写的索引指向另外一个缓存/ppbuf->writeIndex = !ppbuf->writeIndex;}3.应用内存申请和释放写入读取(图片来源网络,侵删)
1、Ping Pong Buffer 原理分析简图描述:A Ping Pong Buffer is a double buffer that is used to speed up a device that can overlap the I/O operation with the data processing operation. One buffer is used to hold a block of data so that a reader device will see a complete (old) version of the data, while in the other buffer a writer device is creating a new (partial) version of data. When the new block of data is complete and self consistent, the reader and the writer device will alternate the two buffers. As a result, the usage of double buffer increases the overall throughput of a device and helps to prevent eventual bottlenecks.————————————————乒乓缓存是一种双缓存机制,用来加速同时存在I/O操作以及数据处理操作的设备一个缓存用来保存旧版本的数据供读设备读取,与此同时,另一个缓存保存写设备产生的新版本数据当新数据完成时,读设备和写设备将会交换两个缓存,双缓存机制将会提高设备的吞吐量,最终有助于避免瓶颈的产生 2、C++代码实现相关结构体创建 、读和写接口的创建
0 评论