简体   繁体   中英

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; 
}

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;
}

You can make it more short by using boost circular buffer. ( http://www.boost.org/doc/libs/1_55_0/doc/html/circular_buffer.html ). All boost libraries are usually very good polished for performance and for implicit specialties on all platforms, so usually I have preference to use them instead of writing my custom codes doing same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM