Reputation: 7891
I need a data structure which accepts several nonadjacent buffers and, I, as the user of this class, can see just one big contiguous buffer. Suppose I can add any number of buffers to it, using some API like this:
void add_buffer(uint8_t* buffer, size_t buffer_length);
and then I can get n bytes
of it using this function:
uint8_t* get_buffer(size_t buffer_size, bool& allocated);
Each buffer is nonadjacent with other buffers, but address returned by above function is for a contiguous memory block. If requested buffer size from get_buffer
, is less than or equals to the first buffer inside the class, just the beginning address of the first buffer will be returned, but if the requested buffer is distributed across several inserted buffers, new memory will be allocated and data from these buffers will be copied to it and the contiguous memory address will be returned.
Now I want to know, Is there any implementation of this data structure or not, furthermore, What is the name of this structure :) Maybe if I knew the name of it, I could find it in my searches.
Upvotes: 0
Views: 248
Reputation: 2075
Perhaps you could use boost::asio::buffer. buffer is a single buffer. You can combine several of them into a "single contiguous" buffer with the std containers, e.g. vector<>. You can add a buffer to the collection with push_back.
You can copy content from the buffer collection with
vector<const_buffer> buffers = ...;
vector<unsigned char> data(boost::asio::buffer_size(buffers));
boost::asio::buffer_copy(boost::asio::buffer(data), buffers);
Btw, if you post a little more info about your problem and what you trying to do, you will probably get better answers faster. Your question sounds a lot like a scatter/gather scenario from networking or disk i/o.
Upvotes: 1