Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it possible to use custom allocator for boost::any in sharedmemory?

Thread Tools

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

Posts: n/a
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)))

if (content)

virtual placeholder * clone() const
return new (manager->allocate(sizeof(ValueType)))

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
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>
typedef char_string options_key_type;
typedef any options_mapped_type; ///<-- the
mapped value
typedef std:air<options_key_type, options_mapped_type>
typedef allocator<options_value_type, segment_manager_t>
typedef map<options_key_type, options_mapped_type,
std::less<options_key_type>, options_value_type_allocator>

options_type* options;

options_value_type_allocator options_allocator_instance(segment-

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();
string_allocator_instance), any(10.0)));
string_allocator_instance), any(true)));
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to use custom allocator for boost::any in sharedmemory? James C++ 0 01-01-2009 10:58 AM
custom allocator using templates Robert Frunzke C++ 4 01-29-2006 05:30 PM
Custom memory allocator - alignment Romeo Colacitti C Programming 4 03-07-2005 01:02 AM
Custom allocator sample code for vector Alex Vinokur C++ 16 08-16-2004 11:25 AM
Idea for custom thread-safe STL allocator? Brian Genisio C++ 12 01-15-2004 03:41 PM