Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Container to present multiple memory chunks as single continuous one

Is there some "standard" container (STL, boost) which can present multiple memory chunks as single continuous one? I need to work with some data with following conditions:

  • Total size of data is not known in advance (web response)
  • Memory is allocated in chunks (with some external allocation function, which I have no control of)
  • Memory freeing is not controlled by me, so reallocations are relatively expensive

So, after getting all the data, I have a list of memory chunks. And I need to apply some STL algorithms (search, copy, etc.) to data as a whole. There is a solution to write container to hold info about these chunks + forward iterator which is able to "jump" from one chunk to another.

But problem seems rather general, so I hope that there is some well-known answer which I'm missing. Thanks in advance.

like image 555
iw.kuchin Avatar asked Mar 19 '12 08:03

iw.kuchin


1 Answers

The memory is provided to you, you say. That sounds like you don't want to copy it. No problem, the STL philosophy is quite flexible. You don't actually need a container; they're just there for memory management and that's already taken care of.

What you do need is an iterator. There's no standard one; you'll have to write one yourself. There are just too many slight variations to provide a standard solution for this. But don't worry, it's fairly easy. You get the necessary typedefs if you inherit from std::iterator<value_type>, so you need to only write operator* (straightforward) and operator++/operator--/operator+/operator- (which understand the chunks).

like image 116
MSalters Avatar answered Oct 05 '22 09:10

MSalters