1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP
12 #define BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
26 #include <boost/interprocess/exceptions.hpp>
27 // interprocess/detail
28 #include <boost/interprocess/detail/type_traits.hpp>
29 #include <boost/interprocess/detail/utilities.hpp>
30 #include <boost/interprocess/detail/in_place_interface.hpp>
32 #include <boost/container/detail/type_traits.hpp> //alignment_of
33 #include <boost/container/detail/minimal_char_traits_header.hpp>
35 #include <boost/intrusive/pointer_traits.hpp>
37 #include <boost/move/detail/type_traits.hpp> //make_unsigned
39 #include <boost/assert.hpp> //BOOST_ASSERT
40 #include <boost/core/no_exceptions_support.hpp>
42 #include <cstddef> //std::size_t
45 //!Describes the object placed in a memory segment that provides
46 //!named object allocation capabilities.
49 namespace interprocess{
51 template<class MemoryManager>
52 class segment_manager_base;
54 //!An integer that describes the type of the
55 //!instance constructed in memory
56 enum instance_type { anonymous_type, named_type, unique_type, max_allocation_type };
60 template<class MemoryAlgorithm>
61 class mem_algo_deallocator
64 MemoryAlgorithm & m_algo;
67 mem_algo_deallocator(void *ptr, MemoryAlgorithm &algo)
68 : m_ptr(ptr), m_algo(algo)
74 ~mem_algo_deallocator()
75 { if(m_ptr) m_algo.deallocate(m_ptr); }
78 template<class size_type>
81 size_type m_value_bytes;
82 unsigned short m_num_char;
83 unsigned char m_value_alignment;
84 unsigned char m_alloc_type_sizeof_char;
86 block_header(size_type val_bytes
87 ,size_type val_alignment
88 ,unsigned char al_type
89 ,std::size_t szof_char
92 : m_value_bytes(val_bytes)
93 , m_num_char((unsigned short)num_char)
94 , m_value_alignment((unsigned char)val_alignment)
95 , m_alloc_type_sizeof_char( (al_type << 5u) | ((unsigned char)szof_char & 0x1F) )
99 block_header &operator= (const T& )
102 size_type total_size() const
104 if(alloc_type() != anonymous_type){
105 return name_offset() + (m_num_char+1)*sizeof_char();
108 return this->value_offset() + m_value_bytes;
112 size_type value_bytes() const
113 { return m_value_bytes; }
115 template<class Header>
116 size_type total_size_with_header() const
118 return get_rounded_size
119 ( size_type(sizeof(Header))
120 , size_type(::boost::container::container_detail::alignment_of<block_header<size_type> >::value))
124 unsigned char alloc_type() const
125 { return (m_alloc_type_sizeof_char >> 5u)&(unsigned char)0x7; }
127 unsigned char sizeof_char() const
128 { return m_alloc_type_sizeof_char & (unsigned char)0x1F; }
130 template<class CharType>
131 CharType *name() const
133 return const_cast<CharType*>(reinterpret_cast<const CharType*>
134 (reinterpret_cast<const char*>(this) + name_offset()));
137 unsigned short name_length() const
138 { return m_num_char; }
140 size_type name_offset() const
142 return this->value_offset() + get_rounded_size(size_type(m_value_bytes), size_type(sizeof_char()));
147 return const_cast<char*>((reinterpret_cast<const char*>(this) + this->value_offset()));
150 size_type value_offset() const
152 return get_rounded_size(size_type(sizeof(block_header<size_type>)), size_type(m_value_alignment));
155 template<class CharType>
156 bool less_comp(const block_header<size_type> &b) const
158 return m_num_char < b.m_num_char ||
159 (m_num_char < b.m_num_char &&
160 std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) < 0);
163 template<class CharType>
164 bool equal_comp(const block_header<size_type> &b) const
166 return m_num_char == b.m_num_char &&
167 std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) == 0;
171 static block_header<size_type> *block_header_from_value(T *value)
172 { return block_header_from_value(value, sizeof(T), ::boost::container::container_detail::alignment_of<T>::value); }
174 static block_header<size_type> *block_header_from_value(const void *value, std::size_t sz, std::size_t algn)
177 const_cast<block_header*>
178 (reinterpret_cast<const block_header*>(reinterpret_cast<const char*>(value) -
179 get_rounded_size(sizeof(block_header), algn)));
182 BOOST_ASSERT(hdr->m_value_alignment == algn);
183 BOOST_ASSERT(hdr->m_value_bytes % sz == 0);
187 template<class Header>
188 static block_header<size_type> *from_first_header(Header *header)
190 block_header<size_type> * hdr =
191 reinterpret_cast<block_header<size_type>*>(reinterpret_cast<char*>(header) +
192 get_rounded_size( size_type(sizeof(Header))
193 , size_type(::boost::container::container_detail::alignment_of<block_header<size_type> >::value)));
198 template<class Header>
199 static Header *to_first_header(block_header<size_type> *bheader)
202 reinterpret_cast<Header*>(reinterpret_cast<char*>(bheader) -
203 get_rounded_size( size_type(sizeof(Header))
204 , size_type(::boost::container::container_detail::alignment_of<block_header<size_type> >::value)));
210 inline void array_construct(void *mem, std::size_t num, in_place_interface &table)
213 std::size_t constructed = 0;
215 table.construct_n(mem, num, constructed);
217 //If there is an exception call destructors and erase index node
219 std::size_t destroyed = 0;
220 table.destroy_n(mem, constructed, destroyed);
226 template<class CharT>
227 struct intrusive_compare_key
229 typedef CharT char_type;
231 intrusive_compare_key(const CharT *str, std::size_t len)
232 : mp_str(str), m_len(len)
235 const CharT * mp_str;
239 //!This struct indicates an anonymous object creation
241 template<instance_type type>
254 struct char_if_void<void>
259 typedef instance_t<anonymous_type> anonymous_instance_t;
260 typedef instance_t<unique_type> unique_instance_t;
263 template<class Hook, class CharType, class SizeType>
264 struct intrusive_value_type_impl
269 intrusive_value_type_impl(const intrusive_value_type_impl &);
270 intrusive_value_type_impl& operator=(const intrusive_value_type_impl &);
273 typedef CharType char_type;
274 typedef SizeType size_type;
276 intrusive_value_type_impl(){}
278 enum { BlockHdrAlignment = ::boost::container::container_detail::alignment_of<block_header<size_type> >::value };
280 block_header<size_type> *get_block_header() const
282 return const_cast<block_header<size_type>*>
283 (reinterpret_cast<const block_header<size_type> *>(reinterpret_cast<const char*>(this) +
284 get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment))));
287 bool operator <(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
288 { return (this->get_block_header())->template less_comp<CharType>(*other.get_block_header()); }
290 bool operator ==(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
291 { return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header()); }
293 static intrusive_value_type_impl *get_intrusive_value_type(block_header<size_type> *hdr)
295 return reinterpret_cast<intrusive_value_type_impl *>(reinterpret_cast<char*>(hdr) -
296 get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment)));
299 CharType *name() const
300 { return get_block_header()->template name<CharType>(); }
302 unsigned short name_length() const
303 { return get_block_header()->name_length(); }
306 { return get_block_header()->value(); }
309 template<class CharType>
310 class char_ptr_holder
313 char_ptr_holder(const CharType *name)
317 char_ptr_holder(const anonymous_instance_t *)
318 : m_name(static_cast<CharType*>(0))
321 char_ptr_holder(const unique_instance_t *)
322 : m_name(reinterpret_cast<CharType*>(-1))
325 operator const CharType *()
328 const CharType *get() const
331 bool is_unique() const
332 { return m_name == reinterpret_cast<CharType*>(-1); }
334 bool is_anonymous() const
335 { return m_name == static_cast<CharType*>(0); }
338 const CharType *m_name;
341 //!The key of the the named allocation information index. Stores an offset pointer
342 //!to a null terminated string and the length of the string to speed up sorting
343 template<class CharT, class VoidPointer>
346 typedef typename boost::intrusive::
347 pointer_traits<VoidPointer>::template
348 rebind_pointer<const CharT>::type const_char_ptr_t;
349 typedef CharT char_type;
350 typedef typename boost::intrusive::pointer_traits<const_char_ptr_t>::difference_type difference_type;
351 typedef typename boost::move_detail::make_unsigned<difference_type>::type size_type;
354 //Offset pointer to the object's name
355 const_char_ptr_t mp_str;
356 //Length of the name buffer (null NOT included)
360 //!Constructor of the key
361 index_key (const char_type *nm, size_type length)
362 : mp_str(nm), m_len(length)
365 //!Less than function for index ordering
366 bool operator < (const index_key & right) const
368 return (m_len < right.m_len) ||
369 (m_len == right.m_len &&
370 std::char_traits<char_type>::compare
371 (to_raw_pointer(mp_str),to_raw_pointer(right.mp_str), m_len) < 0);
374 //!Equal to function for index ordering
375 bool operator == (const index_key & right) const
377 return m_len == right.m_len &&
378 std::char_traits<char_type>::compare
379 (to_raw_pointer(mp_str), to_raw_pointer(right.mp_str), m_len) == 0;
382 void name(const CharT *nm)
385 void name_length(size_type len)
388 const CharT *name() const
389 { return to_raw_pointer(mp_str); }
391 size_type name_length() const
395 //!The index_data stores a pointer to a buffer and the element count needed
396 //!to know how many destructors must be called when calling destroy
397 template<class VoidPointer>
400 typedef VoidPointer void_pointer;
402 explicit index_data(void *ptr) : m_ptr(ptr){}
405 { return static_cast<void*>(to_raw_pointer(m_ptr)); }
408 template<class MemoryAlgorithm>
409 struct segment_manager_base_type
410 { typedef segment_manager_base<MemoryAlgorithm> type; };
412 template<class CharT, class MemoryAlgorithm>
415 typedef typename MemoryAlgorithm::void_pointer void_pointer;
416 typedef CharT char_type;
417 typedef index_key<CharT, void_pointer> key_type;
418 typedef index_data<void_pointer> mapped_type;
419 typedef typename segment_manager_base_type
420 <MemoryAlgorithm>::type segment_manager_base;
422 template<class HeaderBase>
423 struct intrusive_value_type
424 { typedef intrusive_value_type_impl<HeaderBase, CharT, typename segment_manager_base::size_type> type; };
426 typedef intrusive_compare_key<CharT> intrusive_compare_key_type;
429 template<class Iterator, bool intrusive>
430 class segment_manager_iterator_value_adaptor
432 typedef typename Iterator::value_type iterator_val_t;
433 typedef typename iterator_val_t::char_type char_type;
436 segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
440 const char_type *name() const
441 { return m_val->name(); }
443 unsigned short name_length() const
444 { return m_val->name_length(); }
446 const void *value() const
447 { return m_val->value(); }
449 const typename Iterator::value_type *m_val;
453 template<class Iterator>
454 class segment_manager_iterator_value_adaptor<Iterator, false>
456 typedef typename Iterator::value_type iterator_val_t;
457 typedef typename iterator_val_t::first_type first_type;
458 typedef typename iterator_val_t::second_type second_type;
459 typedef typename first_type::char_type char_type;
460 typedef typename first_type::size_type size_type;
463 segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
467 const char_type *name() const
468 { return m_val->first.name(); }
470 size_type name_length() const
471 { return m_val->first.name_length(); }
473 const void *value() const
475 return reinterpret_cast<block_header<size_type>*>
476 (to_raw_pointer(m_val->second.m_ptr))->value();
479 const typename Iterator::value_type *m_val;
482 template<class Iterator, bool intrusive>
483 struct segment_manager_iterator_transform
485 typedef segment_manager_iterator_value_adaptor<Iterator, intrusive> result_type;
487 template <class T> result_type operator()(const T &arg) const
488 { return result_type(arg); }
491 } //namespace ipcdetail {
493 //These pointers are the ones the user will use to
494 //indicate previous allocation types
495 static const ipcdetail::anonymous_instance_t * anonymous_instance = 0;
496 static const ipcdetail::unique_instance_t * unique_instance = 0;
498 namespace ipcdetail_really_deep_namespace {
500 //Otherwise, gcc issues a warning of previously defined
501 //anonymous_instance and unique_instance
506 (void)anonymous_instance;
507 (void)unique_instance;
511 } //detail_really_deep_namespace
513 }} //namespace boost { namespace interprocess
515 #include <boost/interprocess/detail/config_end.hpp>
517 #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP