boost::circular_buffer equivalent for files?

前端 未结 1 844
有刺的猬
有刺的猬 2021-01-03 17:18

I am looking for a library which allows to get a circular buffer on disk.
In Boost there is something similar, but it is an in memory based container: circular_buffer.

相关标签:
1条回答
  • 2021-01-03 17:43

    You can call it whatever you think is natural.

    You're looking for memory mapped files.

    Using the right allocator, you can make containers be allocating in this memory mapped region. That would make the container "on disk".

    I'll see whether Boost Circularbuffer supports this directly.

    Update Yes.

    The best thing is, this gives you full possibility to even use IPC synchronization and thread synchronization. Using a "private" memory map you could map the buffer read-writable without writing changes back to disk in some of the processes.

    Proof of concept:

    Live On Coliru ¹

    #include <boost/circular_buffer.hpp>
    #include <boost/interprocess/allocators/allocator.hpp>
    #include <boost/interprocess/managed_mapped_file.hpp>
    
    namespace bip = boost::interprocess;
    
    struct message {
        int data[32];
    };
    
    int main()
    {
        bip::managed_mapped_file mmf(bip::open_or_create, "/tmp/circ_buffer.bin", 4ul << 20);
        typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
    
        boost::circular_buffer<message, allocator> instance(100, mmf.get_segment_manager());
    }
    

    ¹ On Coliru the filesize is - understandably constrained.

    0 讨论(0)
提交回复
热议问题