2

I have created icl map in shared memory as described in below link

c++ boost icl containers in shared memory

below is the code

#include <boost/icl/interval_map.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

#include <iostream>
#include <vector>
#include <set>

namespace bip = boost::interprocess;
namespace icl = boost::icl;

namespace shared {
    using segment = bip::managed_shared_memory;
    using smgr    = segment::segment_manager;
}

namespace {

static bip::managed_shared_memory global_mm(boost::interprocess::open_or_create,
        "MySharedMemory",65536 //segment name
        );
    static bip::allocator<void, shared::smgr> global_alloc(global_mm.get_segment_manager());

/*
    static bip::managed_mapped_file global_mm(bip::open_or_create, "./demo.bin", 1ul<<20);
    static bip::allocator<void, shared::smgr> global_alloc(global_mm.get_segment_manager());
*/

    template <class T> struct SimpleAllocator : std::allocator<T> { // inheriting the nested typedefs only
        typedef T value_type;

        SimpleAllocator() : _alloc(global_alloc) {}
        template <class U>
            SimpleAllocator(const SimpleAllocator<U> &other) : _alloc(other._alloc) {}

        T* allocate(std::size_t n)           { return std::addressof(*_alloc.allocate(n)); }
        void deallocate(T *p, std::size_t n) { _alloc.deallocate(p, n); }

        // optionals
        template <typename Other> struct rebind { typedef SimpleAllocator<Other> other; };
        bip::allocator<T, shared::smgr> _alloc;
    };

    template <class T, class U> bool operator==(const SimpleAllocator<T> &, const SimpleAllocator<U> &) { return true;  }
    template <class T, class U> bool operator!=(const SimpleAllocator<T> &, const SimpleAllocator<U> &) { return false; }
}

namespace shared {

    template <typename T> using allocator = SimpleAllocator<T>;
    template<typename T>  using set       = std::set<T, std::less<T>, allocator<T> >;

    template <typename Domain, typename Codomain>
    using basic_map = icl::interval_map<Domain, Codomain,
            icl::partial_absorber, std::less, icl::inplace_plus, icl::inter_section, icl::discrete_interval<int, std::less>,
            allocator
        >;

    using map      = basic_map<int, set<int> >;
    using interval = map::interval_type;
}

#include <iostream>

int main() {

    shared::map* demo =  global_mm.find_or_construct<shared::map>("Store")();;
    for (auto&& element : {
            shared::map::value_type { shared::interval::right_open(4, 5), { 1, 7, } },
            shared::map::value_type { shared::interval::right_open(2, 6), { 1, 2, 3, } },
        })
    {
        demo->add(element);
        std::cout << "adding: " << element.first << ", result: " << *demo << "\n";
    }
}

Output is : adding: [4,5), result: {([4,5)->{1 7 })} adding: [2,6), result: {([2,4)->{1 2 3 })([4,5)->{1 2 3 7 })([5,6)->{1 2 3 })}

Next I created another program just to access this map

Below is the code

#include <boost/icl/interval_map.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

#include <iostream>
#include <vector>
#include <set>

namespace bip = boost::interprocess;
namespace icl = boost::icl;

namespace shared {
    using segment = bip::managed_shared_memory;
    using smgr    = segment::segment_manager;
}

namespace {

static bip::managed_shared_memory global_mm(boost::interprocess::open_only,
        "MySharedMemory");
    static bip::allocator<void, shared::smgr> global_alloc(global_mm.get_segment_manager());

    template <class T> struct SimpleAllocator : std::allocator<T> { // inheriting the nested typedefs only
        typedef T value_type;

        SimpleAllocator() : _alloc(global_alloc) {}
        template <class U>
            SimpleAllocator(const SimpleAllocator<U> &other) : _alloc(other._alloc) {}

        T* allocate(std::size_t n)           { return std::addressof(*_alloc.allocate(n)); }
        void deallocate(T *p, std::size_t n) { _alloc.deallocate(p, n); }

        // optionals
        template <typename Other> struct rebind { typedef SimpleAllocator<Other> other; };
        bip::allocator<T, shared::smgr> _alloc;
    };

    template <class T, class U> bool operator==(const SimpleAllocator<T> &, const SimpleAllocator<U> &) { return true;  }
    template <class T, class U> bool operator!=(const SimpleAllocator<T> &, const SimpleAllocator<U> &) { return false; }
}

namespace shared {

    template <typename T> using allocator = SimpleAllocator<T>;
    template<typename T>  using set       = std::set<T, std::less<T>, allocator<T> >;

    template <typename Domain, typename Codomain>
    using basic_map = icl::interval_map<Domain, Codomain,
            icl::partial_absorber, std::less, icl::inplace_plus, icl::inter_section, icl::discrete_interval<int, std::less>,
            allocator
        >;

    using map      = basic_map<int, set<int> >;
    using interval = map::interval_type;
}

#include <iostream>

int main() {

    shared::map* demo =  global_mm.find_or_construct<shared::map>("Store")();
    std::cout << demo->size();
    auto it = demo->find(4);
    if (it != demo->end()) {
        std::cout << "key :: " << it->first << std::endl;

    }

}

But this program crashes at this line std::cout << demo->size(). My OS is Redhat & boost version is 1.54, C++11.

Community
  • 1
  • 1
Asna
  • 108
  • 8
  • well, there's one painful mistake where I "store" the global alloc in `SimpleAllocator::_alloc`. That obviously can't work because the allocators will be part of the datastructure in shared memory, and hence the segment manager address would be stored there. Oops. But I haven't been able to make it work by directly calling `global_alloc` either. See here for my WIP: http://paste.ubuntu.com/12132874/ – sehe Aug 20 '15 at 01:56
  • i think boost interval containers only support std::allocators! only work around wud be to make this allocator point to the shared memory segment and allocate object accordingly – Asna Oct 14 '15 at 13:42

0 Answers0