Boost logo

Boost :

Subject: [boost] [Boost.Lockfree] Interest in a ringbuffer class
From: Raphaël Londeix (raphael.londeix_at_[hidden])
Date: 2017-01-25 11:54:51


Hi everyone,

Is there any interest in having a lower level ringbuffer type in
Boost.Lockfree ?

The containers provided today in the library can only store homogeneous
types of fixed size.
At work, we have refined many implementations of ringbuffer classes that
allow one to push/pop
raw buffers. Before investing some time into a pull request, I wanted to
know if there's some
interest in such a type. The interface looks like this:

template<typename Self, typename... Policies>
class basic_ringbuffer
{
public:
    typedef ... buffer;

public:
    buffer* start_write(size_t);
    void commit_write(size_t);
    void commit_write(buffer const*);

    buffer const* start_read();
    void commit_read(size_t);
    void commit_read(buffer const*);

public:
    void reader_reset();
    void writer_reset();

private:
     buffer* _ring() { return static_cast<Self>(*this).ring(); }
};

Of course, these are only the lowest level methods, one could implement
push()/pop() or other higher level methods.
Beyond the fact that this API provides by default zero-copy semantics, this
interface is also
agnostic to the actual owner of the memory, which allow users to implement
custom ringbuffers
easily (shared memory, mmap trick to map the beginning at the end, etc.),
for example:

    class custom_ringbuffer : basic_ringbuffer<custom_ringbuffer,
SomePolicies...>
    {
        buffer* ring() { return some_memory; }
    };

The usage would look like:

    custom_ringbuffer rb;

    // the write side
    if (auto buf = rb.start_write(100)) {
        memcpy(buf.data, "some data", 9);
        rb.commit_write(9);
    }

    // the read side
    if (auto buf = rb.start_read()) {
        std::cout << std::string_view(buf.data, buf.size) << std::endl;
        rb.commit_read(buf);
    }

What do you think ? Did I miss something in boost itself ? Should I give a
more complete overview of the API ?

Cheers,

-- 
Raphaël Londeix
http://hotgloupi.fr

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk