Question

I have a memory interface that separates out acquiring address space from attaching backing store. (Under Linux the pool of address space managed by the interface is mmap'ed MAP_ANONYMOUS and MAP_NORESERVE, madvise'ed MADV_DONTNEED and mprotect'ed PROT_NONE. Backing is then attached by madvise MADV_WILLNEED and mprotect PROT_READ, PROT_WRITE and PROT_EXEC.)

This interface allows me to allocate a large amount of address space while lazily acquiring the actual physical memory. I would like to use this to create a "lazy vector" which makes requests for backing store at appropriate points but never copies the current contents of the vector as it grows.

Given the semantics of the standard library's allocators is such an scheme possible? Pointers, hints or other guidance gratefully accepted.

Was it helpful?

Solution

There's no provision in the standard library allocator interface for resizing an existing block of memory. There's just "allocate a block of size N" and "deallocate a block" semantics.

So, the answer to your question is "No, you can't do this with std::vector"

You can, of course, write your own vector-like class that does this - and if this is something that is important to you, you should do so. Writing a container class is not really that hard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top