
STL, std::allocator and memory mapped files
Quote:
>I want to use memory mapped files to store my application specific
>data. I will be needing lists and maybe some other STL classes. Since
>memory mapped files are no more than a range of ordinary addresses in
>user space, I thought I might use STL lists instead of inventing my
>own but one big problem arises. Memory mapped file can be mapped to
>different start address each time it's created so list's pointers
>should actually be offsets to the beginning of the file and not actual
>pointers. Offsets are easily translated into actual pointers by adding
>the start address of a memory mapped file.
>Has anybody ever done anything like this? I am not an expert in STL so
>I'm seeking help. I thought that changing std::allocator class will do
>the trick. I have looked at the source of std::allocator and std::list
>and haven't found a way to do what I want. Am I just missing something
>or is it really impossible?
std::list, std::map, etc. use pointers internally. Unless you can guarantee
the shared memory is mapped to the same logical address in all apps using a
shared STL container, it won't work. If you can guarantee this, don't forget
that the list, map, or whatever must itself reside in the shared memory, and
any memory dynamically allocated by the container's values must reside in
the shared memory as well. For example, suppose you arrange for a std::list
object to reside in a shared memory section named X, and you give it an
allocator that allocates from X. If the list contains strings, the strings
must also obtain their dynamic storage from X; the list's allocator
parameter is concerned only with allocations made by the list, not
allocations made by objects stored in the list.
--
Doug Harrison
Visual C++ MVP