Is it possible to use custom allocator for boost::any in sharedmemory?

Discussion in 'C++' started by James, Jan 1, 2009.

  1. James

    James Guest

    I am trying to develop 'generic' property map in shared memory based
    on boost::any and had some problem.

    Boost::interprocess and related containers in shared memory are used.
    The property map will look like as below (part code):
    My question is: because boost::any use "new" directly in the
    constructor to construct the object

    template<typename ValueType>
    any(const ValueType & value)
    : content(new holder<ValueType>(value))
    {
    }

    it fails to co-work with shared memory because the object is not in
    the shared memory and will not be seen by other processes.
    My quick-and-dirty modification to boost::any is to replace all "new"
    with "placement new" like:

    template<typename ValueType>
    any(const ValueType & value) /// manager is global
    variable of the shared memory manager
    : content(new (manager->allocate(sizeof(ValueType)))
    holder<ValueType>(value))
    {
    }

    ~any()
    {
    if (content)
    content->~placeholder();
    any_allocator::shared_allocator->deallocate(content);
    }

    virtual placeholder * clone() const
    {
    return new (manager->allocate(sizeof(ValueType)))
    holder(held);
    }

    But when trying to get the content from another process, is still
    cause segfault in
    any(const any & other)
    : content(other.content ? other.content->clone() : 0)
    {
    }
    any idea about that?
    Thanks a lot in advance.

    ------------------------------------------------------------------------------------------
    below my testing code where "any" is the modified version

    #include "boost/interprocess/managed_shared_memory.hpp"
    #include "boost/interprocess/containers/map.hpp"
    #include "boost/interprocess/allocators/allocator.hpp"
    #include "boost/interprocess/containers/string.hpp"

    using namespace boost::interprocess;
    typedef managed_shared_memory::segment_manager
    segment_manager_t;
    typedef allocator<char, segment_manager_t> char_allocator;
    typedef allocator<void, segment_manager_t> void_allocator;
    typedef basic_string<char, std::char_traits<char>, char_allocator>
    char_string;
    typedef char_string options_key_type;
    typedef any options_mapped_type; ///<-- the
    mapped value
    typedef std::pair<options_key_type, options_mapped_type>
    options_value_type;
    typedef allocator<options_value_type, segment_manager_t>
    options_value_type_allocator;
    typedef map<options_key_type, options_mapped_type,
    std::less<options_key_type>, options_value_type_allocator>
    options_type;

    options_type* options;

    options_value_type_allocator options_allocator_instance(segment-
    >get_segment_manager());

    options = segment->construct<options_type>("options")
    (std::less<options_key_type>(), options_allocator_instance);
    void_allocator string_allocator_instance(segment->get_segment_manager
    ());
    any_allocator::shared_allocator = segment->get_segment_manager();
    options->insert(options_value_type(char_string("key1",
    string_allocator_instance), any(10.0)));
    options->insert(options_value_type(char_string("key1",
    string_allocator_instance), any(true)));
    ....
     
    James, Jan 1, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Latter
    Replies:
    2
    Views:
    2,934
    Julie
    May 17, 2004
  2. Misiu
    Replies:
    3
    Views:
    2,459
    Misiu
    Jan 31, 2007
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Implemetaction a straw by next of memory at any allocator.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 19, 2007, in forum: C++
    Replies:
    0
    Views:
    338
    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki
    Jul 19, 2007
  4. Juha Nieminen
    Replies:
    8
    Views:
    3,249
    Juha Nieminen
    Jul 11, 2008
  5. James
    Replies:
    0
    Views:
    864
    James
    Jan 1, 2009
Loading...

Share This Page