Can std::vector avoid copying if allocator provides realloc semantics?
Asked Answered
L

1

9

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.

Leontine answered 6/5, 2014 at 21:38 Comment(1)
Maybe related.Contain
S
0

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.

Swoon answered 7/5, 2014 at 16:17 Comment(1)
I had more or less come to that conclusion on my own but was hoping that I might have missed something. After looking at how many vector member functions I would have to disable and at the simplicity of those remaining I have concluded that I do indeed need to write my own container. Thanks for the extra shove.Leontine

© 2022 - 2024 — McMap. All rights reserved.