**Non-Boost** STL allocator for shared memory

后端 未结 2 1659
一生所求
一生所求 2021-02-14 05:02

Due to policy where I work, I am unable to use a version of Boost newer than 1.33.1 and unable to use a version of GCC newer than 4.1.2. Yes, it\'s garbage, but there is nothing

相关标签:
2条回答
  • 2021-02-14 06:00

    Pointers do not work in shared memory unless you cannot pin down the shared memory at a fixed address (consistent in all processes). As such, you need specific classes that will either be contiguous (no pointer), or have an offset (and not a pointer) into the memory area in which the shared memory is mapped.

    We are using shared memory at work in a pretty similar situation: one process computes a set of data, places it in shared memory, and then signal the other processes that they may map the memory into their own address space; the memory is never changed afterwards.

    The way we go about it is having POD structures (*) (some including char xxx[N]; attributes for string storage). If you can actually limit your strings, you are golden. And as far as map goes: it's inefficient for read-only storage => a sorted array performs better (hurray for memory locality). So I would advise going at it so:

    struct Key {
        enum { Size = 318 };
        char value[Size];
    };
    
    struct Value {
        enum { Size = 412 };
        enum K { Int, Long, String };
        K kind;
        union { int i; long l; char string[Size]; } value;
    };
    

    And then simply have an array of std::pair<Key, Value> that you sort (std::sort) and over which you use std::lower_bound for searches. You'll need to write a comparison operator for key, obviously:

    bool operator<(Key const& left, Key const& right) {
        return memcmp(left.value, right.value, Key::Size) < 0;
    }
    

    And I agree that the enum + union trick is less appealing (interface wise) than a boost variant... it's up to you to make the interface better.

    (*) Actually, a pure POD is not necessary. It's perfectly okay to have private attributes, constructors and copy constructors for example. All that is needed is to avoid indirection (pointers).

    0 讨论(0)
  • 2021-02-14 06:03

    Simple workaround. Create your own "libNotBoost v1.0` from Boost 1.51. The Boost library allows this. Since it's no longer Boost, you're fine.

    0 讨论(0)
提交回复
热议问题