3 ms·
A memory mapped file can be used to store complex object oriented structures if you make use of 'relative' pointers, where you have to add the address of the po
by fjfaase 2y ago
A memory mapped file can be used to store complex object oriented structures if you make use of 'relative' pointers, where you have to add the address of the pointer to the pointer to get to the object you are pointing to. I once used this method, to persist a complex data structure without having to write serialization code.
A long time ago, I implemented some C++ classes that would hide most of the additional work and also took care of allocating new objects inside the memory mapped file. See: https://www.iwriteiam.nl/D0205.html#13MMF https://www.iwriteiam.nl/D0205.html#13MMF (Note that this implementation actually makes use of a slightly different approach where pointers are relative with respect to the first position of the file. This implementation has the limitation that you can only open one such store as a memory mapped file. It was only later I realized that it was possible to do without the offset. I never came to rewriting all the code.)
- dmitrygr 2y ago> complex object oriented structures if you make use of 'relative' pointers You just described the original MsOffice file formats (and given many people who ever tried to parse them a PTSD shock)
- anon291 2y agoMost stl classes will work just fine with smart pointers. For example, std::vector takes a custom allocator. You can have the allocators pointer type be a custom pointer class. The pointer class is a wrapper over an offset that is the same size as the native word. Then the operator-> is just (this + offset). I've used this approach successfully in many projects. If you're worried about differing stls(probably a reasonable worry here), then use any of the boost containers.
- quietbritishjim 2y agoBoost.Container [1] has reimplementations of the STL containers that use offsets to be compatible with memory-mapped files. Boost.Interprocess [2] has some other useful types, such as smart pointers that are compatible with memory-mapped files, along with platform-independent APIs for handling them. [1] https://www.boost.org/doc/libs/1_86_0/doc/html/container.html#container.intro.introduction_building_container https://www.boost.org/doc/libs/1_86_0/doc/html/container.htm... [2] https://www.boost.org/doc/libs/1_86_0/doc/html/interprocess.html https://www.boost.org/doc/libs/1_86_0/doc/html/interprocess....