views:

1835

answers:

5

I've long had a desire for an STLish container that I could place into a shared memory segment or a memory mapped file.

I've considered the use of a custom allocator and placement new to place a regular STL container into a shared memory segment. (like this ddj article). The problem is that STL containers will internally have pointers to the memory they own. Therefore, if the shared memory segment or memory mapped file loads at a different base address (perhaps on a subsequent run, or in a second process), then the internal pointers are suddenly invalid. As far as I can figure out, the custom allocator approach only works if you can always map the memory segment into your process at the same address. At least with memory mapped files, I have lots of experience of that NOT being the case if you just let the system map it where ever it feels like.

I've had some thoughts on how to do this, but I'd like to avoid it if someone else has already done the work (that's me, being lazy).

I'm currently leaving locking out of the discussion, as the best locking strategy is highly application dependent.

+8  A: 

The best starting point for this is probably the boost Interprocess libraries. They have a good example of a map in shared memory here: interprocess map

You will probably also want to read the section on offset smart pointers, which solves the internal pointer problem you were referring to. Offser Pointer

grieve
I support this answer using personal experience. The online guide made using IPC a breeze! www.boost.org/doc/libs/1_36_0/doc/html/interprocess/quick_guide.html www.boost.org/doc/libs/1_36_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file
que que
A: 

You may also want to checkout the Intel Threading Building Blocks (TBB) Containers.

Pat Notz
that assumes he's using threads, which is likely not the case.
Leon Timmermans
A: 

I always had good experiences (years ago) with ACE. Its a networking/communication framework, but has a section on shared memory.

gbjbaanb
A: 

I only know of proprietary versions. Bloomberg and EA have both published about their STL versions, but havent released ( to my knowledge ) the fruits of their labor.

Sanjaya R
A: 

Try using Qt's QSharedMemory Implementation.

Ankur Gupta
I just looked at QSharedMemory - it just gives shared memory access, it doesn't give containers of any type.
Michael Kohne
agreed. My bad. I hv implemented a fixed size message queue over it.
Ankur Gupta