数据结构环状缓冲区(ring_buffer)的实现

上传人:kms****20 文档编号:46540020 上传时间:2018-06-27 格式:PDF 页数:7 大小:49.20KB
返回 下载 相关 举报
数据结构环状缓冲区(ring_buffer)的实现_第1页
第1页 / 共7页
数据结构环状缓冲区(ring_buffer)的实现_第2页
第2页 / 共7页
数据结构环状缓冲区(ring_buffer)的实现_第3页
第3页 / 共7页
数据结构环状缓冲区(ring_buffer)的实现_第4页
第4页 / 共7页
数据结构环状缓冲区(ring_buffer)的实现_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《数据结构环状缓冲区(ring_buffer)的实现》由会员分享,可在线阅读,更多相关《数据结构环状缓冲区(ring_buffer)的实现(7页珍藏版)》请在金锄头文库上搜索。

1、/* *Copyright (c) 2004-2010 xxxx. *All rights reserved. * *File Type : C+ Header File(*.h) *File Name : Utility/ring_buffer.h *Module: Utility *Author: Han Yongquan. * *This file defines a ring buffer class. * *This is an accumulated buffer, it can store any object or scattered data. *It is said tha

2、t, this buffer can receive some bytes of any length at one *time by push() operation, as long as there are enough free space. And you *can get some bytes of any length at one time by pop() operation, as long as *there are enough valid data in it. * *NOTE! *This buffer can only be used in case of onl

3、y one writing *thread to write data into it and only one reading thread to *read data from it. * *Memory view perhaps like this: * *(buffer head) |_| *|/| |_| *(writing thread)|_| | *|_| | *|_| (hole in the)| *|_(hole)_| / (middle of buffer)| (Looply) *|_| | *|_| | *|_|/| *|/| |_| *(writing thread)|

4、_| *|_(hole)_| (Looply) *|_| *|_| *(buffer end) |_|_| * */#ifndef _UTILITY_RING_BUFFER_H_ #define _UTILITY_RING_BUFFER_H_ /#pragma once#ifndef _cplusplus #error Please compile this file with C+ compiler! #endif/ _cplusplus/#pragma warning(push, 3)#include #include #ifndef _PLATFORM_WIN32_ #include #

5、else / !_PLATFORM_WIN32_ #include #endif / _PLATFORM_WIN32_#include “Utility/common_config.h“ #include “Utility/basic_type.h“#ifdef _MSC_VER #pragma pack(push, 4) #elif defined(_SUNPRO_CC) | defined(_SUNPRO_C) #pragma pack(8) #elif defined (_GNUG_) #pragma pack(8) #endifnamespace Utility /- /This is

6、 an accumulated buffer, it can store any object or scattered data. /It is said that, this buffer can receive some bytes of any length at one /time by push operation, as long as there are enough free space. And you can/get some bytes of any length at one time by pop operation, as long as /there are e

7、nough valid data in it. /All valid data must be located after m_popPos, and before m_pushPos. / /NOTE! /This buffer can only be used in case of only one writing /thread to write data into it and only one reading thread to /read data from it. / /N : Ring buffer Capacity(N bytes) /- template class Rin

8、gBuffer public: typedef size_t size_type;/* *Constructor m_pRingBuffer = new BYTEN; RingBuffer() delete m_pRingBuffer; RingBuffer(const RingBuffer m_pRingBuffer = new BYTEN; size_type rearLen = N - other.m_popPos; if (rearLen = other.m_count) :memmove(m_pRingBuffer, else :memmove(m_pRingBuffer,:memm

9、ove(m_pRingBuffer + rearLen, other.m_pRingBuffer, other.m_count - rearLen); m_pushPos = m_count = other.m_count; /m_popPos = 0; RingBuffer/ invoke copy constructor swap(temp);/ this-swap(); return (*this); /* *ACCESSORS */ bool is_full() const return (m_count = N); bool is_empty() const return (m_co

10、unt = 0); size_type size() const return m_count; size_type capacity() const return N; /* *Write some data with the specified length into this ring buffer. *param data : The buffer in which the data will be written into this ring buffer. *param length : The length of the data that will be written. *r

11、eturn : The real length that had been written into this ring buffer. *If there are no enough free space in the ring buffer or the specified *length is 0, return 0. */ size_type push(const BYTE *data, size_type length) assert(data != 0);if (length = 0 | length (N - m_count)/* length (total free space

12、 length) */ return 0;/* OK! length (rear free space length) */ :memmove( :memmove(m_pRingBuffer, data + rearLen, length - rearLen); m_pushPos = length - rearLen;/ adjust new push position / /If you want use this ring buffer in case of multi-threaded environment /that there are may be several writing

13、 or reading threads at the same /time, move the guard object to the beginning of this function. / m_count += length; return (length); /* *Read some data with the specified length from this ring buffer. *You must insure the output buffer has enough space to store the returned data. *param buf : The o

14、utput buffer used to store the poped data temporarily. *param length: The length of the data requested to read. *return : The real length had been read. If there are no enough valid *data in the ring buffer to pop or the specified length is 0, *return 0. */ size_type pop(BYTE *buf, size_type length) assert(buf != 0); if (length = 0 | length m_count) /* no enough data to pop */ return 0;/* OK! length (rear data length) */ :memmove(buf, :memmove(buf + rearLen, m_pRingBuffer, length - rearLen); m_popPos = length - rea

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号