Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement circular buffer to write/read arbitrary amount of data in a single call

Most of circular buffer assumes to only read/write ONE object each time, the only link I found to operates on binary data in form of (const char *bytes, size_t byte_count) is http://www.asawicki.info/news_1468_circular_buffer_of_raw_binary_data_in_c.html, which I feel is not incorrect and a little long. What is the right implementation for that?

I created one myself. but it is still long. Can anybody share a more elegant version? or can you point out is there any thing I can improve in my code to make it short?

class Pipe{
    Pipe(size_t capacity): _capacity(capacity){ init();  }
    ~Pipe(){delete [] _buf; }
    size_t read(char* data, size_t bytes);
    size_t write(const char* data, size_t bytes);
   private: 
    //only _capacity-1 is used, one is to identify full or empty.
    void init(){_buf = new char[_capacity]; 
     _wptr = 0; _rptr = 0; _used_size = 0; 
    }
    char* _buf;
    size_t _capacity, _wptr, _rptr, _used_size;
    bool isFull(){return (_wptr + 1 ) % (_capacity) == _rptr;} 
    bool isEmpty(){return _wptr == _rptr;} 
}; 
size_t Pipe::read(char* data, size_t bytes){
    if (isEmpty() || bytes == 0) return 0;
    size_t bytes_read1 = 0, bytes_read2 = 0;
    if (_rptr>=_wptr+1) { //two piece can be read
        bytes_read1 = min(bytes, _capacity - _rptr);
        memcpy(data, _buf + _rptr, bytes_read1);
        _rptr += bytes_read1;
        bytes -= bytes_read1;
        if (_rptr == _capacity) _rptr = 0;
        if (bytes > 0){
        bytes_read2 = min(bytes, _wptr);
            memcpy(_buf + _rptr, data, bytes_read2);
            _rptr += bytes_read2;
            bytes -= bytes_read2;
        }
    }
    else{//one piece can be read
    bytes_read1 = min(bytes, _wptr - _rptr); 
    memcpy(_buf + _wptr, data, bytes_read1);
    _rptr += bytes_read1;
    bytes -= bytes_read1;
    }
    return bytes_read1 + bytes_read2; 
}

size_t Pipe::write(const char* data, size_t bytes){
    if (isFull() || bytes == 0) return 0;
    size_t bytes_write1 = 0, bytes_write2 = 0;
    if (_wptr>=_rptr) { //two piece can be written
        bytes_write1 = min(bytes, _capacity - _wptr); 
        memcpy(_buf + _wptr, data, bytes_write1);
        _wptr += bytes_write1;
        bytes -= bytes_write1;
        if (_wptr == _capacity) _wptr = 0;
        if (bytes > 0){ //_wptr must be 0 here.
            bytes_write2 = min(bytes, _rptr-1);//-1 bcz there is one     
    slot to check empty/full
            memcpy(_buf + _wptr, data+ bytes_write1, bytes_write2);
            _wptr += bytes_write2;
            bytes -= bytes_write2;
        }
    }
    else{ //one piece can be written
        bytes_write1 = min(bytes, _rptr - _wptr -1); 
        memcpy(_buf + _wptr, data, bytes_write1);
        _wptr += bytes_write1;
        bytes -= bytes_write1;
    }
    return bytes_write1 + bytes_write2; 
}
like image 625
user389955 Avatar asked Jan 20 '14 22:01

user389955


People also ask

How is a circular buffer implemented?

Circular Buffers can be implemented in two ways, using an array or a linked list. An empty object array along with its capacity is initialized inside the constructor as the type of elements added is unknown.

What is a circular buffer and how is it used in real time systems?

A circular buffer is a utility used to transfer successive data values from a producer thread to a consumer thread, who retrieves the data in FIFO (first in first out) order. This kind of data structure will be used when pipelining threads, a subject discussed in detail in Chapter 15.

What is ring buffer implement a ring buffer using array?

A ring buffer is an efficient FIFO buffer. It uses a fixed-size array that can be pre-allocated upfront and allows an efficient memory access pattern. All the buffer operations are constant time O(1), including consuming an element, as it doesn't require a shifting of elements.

How do you know if a circular buffer is full?

Determining if a Buffer is Full Both the “full” and “empty” cases of the circular buffer look the same: head and tail pointer are equal. There are two approaches to differentiating between full and empty: “Waste” a slot in the buffer: Full state is head + 1 == tail.


1 Answers

The code in OP may be simplified by excluding all conditionals. Original interface and memcpy for implementation is retained (only constructor/destructor/read/write made public and unused _used_size may be dropped).

size_t Pipe::read(char* data, size_t bytes)
{
    bytes = min(bytes, getUsed());
    const size_t bytes_read1 = min(bytes, _capacity - _rptr);
    memcpy(data, _buf + _rptr, bytes_read1);
    memcpy(data + bytes_read1, _buf, bytes - bytes_read1);
    updateIndex(_rptr, bytes);
    return bytes;
}

size_t Pipe::write(const char* data, size_t bytes)
{
    bytes = min(bytes, getFree());
    const size_t bytes_write1 = min(bytes, _capacity - _wptr); 
    memcpy(_buf + _wptr, data, bytes_write1);
    memcpy(_buf, data + bytes_write1, bytes - bytes_write1);
    updateIndex(_wptr, bytes);
    return bytes;
}

Several private methods used here may have this simple implementation:

size_t Pipe::getUsed()
{ return (_capacity - _rptr + _wptr) % _capacity; }

size_t Pipe::getFree()
{ return (_capacity - 1 - _wptr + _rptr) % _capacity; }

void Pipe::updateIndex(size_t& index, size_t bytes)
{ index = (index + bytes) % _capacity; }

This implementation has one disadvantage: it is broken when _capacity is close to maximum size_t value (because of overflows). This may be fixed by substituting modulo with conditionals in free/used calculations and index updates. Here are modifications for methods used in read:

size_t Pipe::getUsed()
{
    if (_wptr >= _rptr)
        return _wptr - _rptr;
    else
        return _capacity - _rptr + _wptr;
}

void Pipe::updateIndex(size_t& index, size_t bytes)
{
    if (bytes >= _capacity - index)
        index = index + bytes - _capacity;
    else
        index = index + bytes;
}
like image 166
Evgeny Kluev Avatar answered Oct 04 '22 21:10

Evgeny Kluev