Is there a good way to use C++ STL data structures contained within an mmap'ed region of memory?
-
I would like to be able to pass the data structure from one process to another and use mmap as the mechanism to do it. Persisting to a file would also be nice. This is a follow-up question to .
-
Answer:
http://www.boost.org/doc/libs/1_42_0/doc/html/interprocess.html, especially http://www.boost.org/doc/libs/1_42_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.containers_explained Be careful if you use a file though. If you mmap a file and the machine dies, there are no consistency guarantees -- the kernel can output different sections of the file at different times. You should take a snapshot of the file if you want to be safe in the case of power failures. Another thing to be careful of is memory fragmentation. If you go this route Boost will be doing its own memory allocation. There are a number of allocators they offer, and I'm not sure how well tuned they are. If you are using a lot of memory, you should have monitoring for fragmentation. If possible, you should avoid allocating short-lived, per request data in the shared space.
Ben Maurer at Quora Visit the source
Other answers
Every STL object takes an "alloc" template parameter. The default functionality is to use 'new'. You can pass in a custom allocation object that you create. This document appears to discuss exactly your subject, STL types going through threads. http://www.drdobbs.com/184406243 Another interesting approach is to override the new operator. This is dangerous though so maybe make a custom new operator (this can also be overridden!). Ah the joys of C++, where everything is customizable...
Dustin Dettmer
Even if you go down the custom allocator route, i.e. create a memory mapped file and then create a STL map structure with a custom allocator, the problem is pointers. You have to make sure the memory mapped file always loads from the same exact memory address. You can do this on some operating systems, i.e. on Windows, you can choose the address with MapViewOfFileEx. You could replace pointers by a template pointer class that keeps the load address of mmap() as a static member, and the offset relative to the mmap area as a member.
Hussein Kanji
you might want to check out STXXL ...
Dennis DeCoste
Related Q & A:
- What is a good way to advertise enexpensively for a small business? It is handyman work?Best solution by Yahoo! Answers
- What's a good way to let a girl know you are interested in her?Best solution by Yahoo! Answers
- What is a good way to break in a snowboard?Best solution by answers.yahoo.com
- What is a good way to promote a movie?Best solution by Yahoo! Answers
- What is a good camcorder to use as a webcam?Best solution by tomshardware.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.