Search code examples
c++boostmmapboost-interprocessboost-multi-index

Persistence of boost multi_index_container stored in a file


Can I rely on boost multi_index_container allocated within memory mapped file? Will be this kind of "database" portable between computers with the same endianess?


Solution

  • No, this is not safe. The address of the first byte in the memory mapped file cannot be guaranteed to be the same between invocations.

    Boost's memory mapped file takes a hint, but it is only a hint.

    So in the first invocation, the memory might be located at 0xBAADF00D. On the second invocation, it might be located at 0xF00DBAAD. Pointers within the structure would no longer be valid, as they would point to memory around 0xBAADF00D rather than around 0xF00DBAAD.

    As a serious problem, it might usually work in testing, as the hint might usually be obeyed -- but sometimes there will already be stuff at that memory address, and the hint will have to be ignored.

    (There are other parts that make this really hard -- but the above makes it pretty infeasible).

    Now, strategies like this can work, but require very intrusive introspection of the data structures in question. You'd have to do a pass like DLL readdressing in order to get the data structures to have the correct offsets, and by that point you might as well just serialize it flat.