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
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
21 #include <boost/intrusive/pointer_traits.hpp>
23 #include <boost/detail/no_exceptions_support.hpp>
24 #include <boost/interprocess/detail/type_traits.hpp>
25 #include <boost/interprocess/detail/utilities.hpp>
26 #include <boost/interprocess/detail/in_place_interface.hpp>
27 #include <boost/interprocess/exceptions.hpp>
28 #include <boost/type_traits/make_unsigned.hpp>
29 #include <boost/type_traits/alignment_of.hpp>
30 #include <boost/intrusive/pointer_traits.hpp>
31 #include <cstddef> //std::size_t
32 #include <string> //char_traits
33 #include <new> //std::nothrow
34 #include <utility> //std::pair
35 #include <iterator> //std::iterator_traits
36 #include <boost/assert.hpp> //BOOST_ASSERT
37 #include <functional> //unary_function
38 #ifndef BOOST_NO_EXCEPTIONS
43 //!Describes the object placed in a memory segment that provides
44 //!named object allocation capabilities.
47 namespace interprocess{
49 template<class MemoryManager>
50 class segment_manager_base;
52 //!An integer that describes the type of the
53 //!instance constructed in memory
54 enum instance_type { anonymous_type, named_type, unique_type, max_allocation_type };
58 template<class MemoryAlgorithm>
59 class mem_algo_deallocator
62 MemoryAlgorithm & m_algo;
65 mem_algo_deallocator(void *ptr, MemoryAlgorithm &algo)
66 : m_ptr(ptr), m_algo(algo)
72 ~mem_algo_deallocator()
73 { if(m_ptr) m_algo.deallocate(m_ptr); }
76 template<class size_type>
79 size_type m_value_bytes;
80 unsigned short m_num_char;
81 unsigned char m_value_alignment;
82 unsigned char m_alloc_type_sizeof_char;
84 block_header(size_type val_bytes
85 ,size_type val_alignment
86 ,unsigned char al_type
87 ,std::size_t szof_char
90 : m_value_bytes(val_bytes)
91 , m_num_char((unsigned short)num_char)
92 , m_value_alignment((unsigned char)val_alignment)
93 , m_alloc_type_sizeof_char( (al_type << 5u) | ((unsigned char)szof_char & 0x1F) )
97 block_header &operator= (const T& )
100 size_type total_size() const
102 if(alloc_type() != anonymous_type){
103 return name_offset() + (m_num_char+1)*sizeof_char();
106 return this->value_offset() + m_value_bytes;
110 size_type value_bytes() const
111 { return m_value_bytes; }
113 template<class Header>
114 size_type total_size_with_header() const
116 return get_rounded_size
117 ( size_type(sizeof(Header))
118 , size_type(::boost::alignment_of<block_header<size_type> >::value))
122 unsigned char alloc_type() const
123 { return (m_alloc_type_sizeof_char >> 5u)&(unsigned char)0x7; }
125 unsigned char sizeof_char() const
126 { return m_alloc_type_sizeof_char & (unsigned char)0x1F; }
128 template<class CharType>
129 CharType *name() const
131 return const_cast<CharType*>(reinterpret_cast<const CharType*>
132 (reinterpret_cast<const char*>(this) + name_offset()));
135 unsigned short name_length() const
136 { return m_num_char; }
138 size_type name_offset() const
140 return this->value_offset() + get_rounded_size(size_type(m_value_bytes), size_type(sizeof_char()));
145 return const_cast<char*>((reinterpret_cast<const char*>(this) + this->value_offset()));
148 size_type value_offset() const
150 return get_rounded_size(size_type(sizeof(block_header<size_type>)), size_type(m_value_alignment));
153 template<class CharType>
154 bool less_comp(const block_header<size_type> &b) const
156 return m_num_char < b.m_num_char ||
157 (m_num_char < b.m_num_char &&
158 std::char_traits<CharType>::compare
159 (name<CharType>(), b.name<CharType>(), m_num_char) < 0);
162 template<class CharType>
163 bool equal_comp(const block_header<size_type> &b) const
165 return m_num_char == b.m_num_char &&
166 std::char_traits<CharType>::compare
167 (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::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)), size_type(::boost::alignment_of<block_header<size_type> >::value)));
197 template<class Header>
198 static Header *to_first_header(block_header<size_type> *bheader)
201 reinterpret_cast<Header*>(reinterpret_cast<char*>(bheader) -
202 get_rounded_size(size_type(sizeof(Header)), size_type(::boost::alignment_of<block_header<size_type> >::value)));
208 inline void array_construct(void *mem, std::size_t num, in_place_interface &table)
211 std::size_t constructed = 0;
213 table.construct_n(mem, num, constructed);
215 //If there is an exception call destructors and erase index node
217 std::size_t destroyed = 0;
218 table.destroy_n(mem, constructed, destroyed);
224 template<class CharT>
225 struct intrusive_compare_key
227 typedef CharT char_type;
229 intrusive_compare_key(const CharT *str, std::size_t len)
230 : mp_str(str), m_len(len)
233 const CharT * mp_str;
237 //!This struct indicates an anonymous object creation
239 template<instance_type type>
252 struct char_if_void<void>
257 typedef instance_t<anonymous_type> anonymous_instance_t;
258 typedef instance_t<unique_type> unique_instance_t;
261 template<class Hook, class CharType, class SizeType>
262 struct intrusive_value_type_impl
267 intrusive_value_type_impl(const intrusive_value_type_impl &);
268 intrusive_value_type_impl& operator=(const intrusive_value_type_impl &);
271 typedef CharType char_type;
272 typedef SizeType size_type;
274 intrusive_value_type_impl(){}
276 enum { BlockHdrAlignment = ::boost::alignment_of<block_header<size_type> >::value };
278 block_header<size_type> *get_block_header() const
280 return const_cast<block_header<size_type>*>
281 (reinterpret_cast<const block_header<size_type> *>(reinterpret_cast<const char*>(this) +
282 get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment))));
285 bool operator <(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
286 { return (this->get_block_header())->template less_comp<CharType>(*other.get_block_header()); }
288 bool operator ==(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
289 { return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header()); }
291 static intrusive_value_type_impl *get_intrusive_value_type(block_header<size_type> *hdr)
293 return reinterpret_cast<intrusive_value_type_impl *>(reinterpret_cast<char*>(hdr) -
294 get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment)));
297 CharType *name() const
298 { return get_block_header()->template name<CharType>(); }
300 unsigned short name_length() const
301 { return get_block_header()->name_length(); }
304 { return get_block_header()->value(); }
307 template<class CharType>
308 class char_ptr_holder
311 char_ptr_holder(const CharType *name)
315 char_ptr_holder(const anonymous_instance_t *)
316 : m_name(static_cast<CharType*>(0))
319 char_ptr_holder(const unique_instance_t *)
320 : m_name(reinterpret_cast<CharType*>(-1))
323 operator const CharType *()
326 const CharType *get() const
329 bool is_unique() const
330 { return m_name == reinterpret_cast<CharType*>(-1); }
332 bool is_anonymous() const
333 { return m_name == static_cast<CharType*>(0); }
336 const CharType *m_name;
339 //!The key of the the named allocation information index. Stores an offset pointer
340 //!to a null terminated string and the length of the string to speed up sorting
341 template<class CharT, class VoidPointer>
344 typedef typename boost::intrusive::
345 pointer_traits<VoidPointer>::template
346 rebind_pointer<const CharT>::type const_char_ptr_t;
347 typedef CharT char_type;
348 typedef typename boost::intrusive::pointer_traits<const_char_ptr_t>::difference_type difference_type;
349 typedef typename boost::make_unsigned<difference_type>::type size_type;
352 //Offset pointer to the object's name
353 const_char_ptr_t mp_str;
354 //Length of the name buffer (null NOT included)
358 //!Constructor of the key
359 index_key (const char_type *nm, size_type length)
360 : mp_str(nm), m_len(length)
363 //!Less than function for index ordering
364 bool operator < (const index_key & right) const
366 return (m_len < right.m_len) ||
367 (m_len == right.m_len &&
368 std::char_traits<char_type>::compare
369 (to_raw_pointer(mp_str)
370 ,to_raw_pointer(right.mp_str), m_len) < 0);
373 //!Equal to function for index ordering
374 bool operator == (const index_key & right) const
376 return m_len == right.m_len &&
377 std::char_traits<char_type>::compare
378 (to_raw_pointer(mp_str),
379 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 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
484 : std::unary_function< typename std::iterator_traits<Iterator>::value_type
485 , segment_manager_iterator_value_adaptor<Iterator, intrusive> >
487 typedef segment_manager_iterator_value_adaptor<Iterator, intrusive> result_type;
489 result_type operator()(const typename std::iterator_traits<Iterator>::value_type &arg) const
490 { return result_type(arg); }
493 } //namespace ipcdetail {
495 //These pointers are the ones the user will use to
496 //indicate previous allocation types
497 static const ipcdetail::anonymous_instance_t * anonymous_instance = 0;
498 static const ipcdetail::unique_instance_t * unique_instance = 0;
500 namespace ipcdetail_really_deep_namespace {
502 //Otherwise, gcc issues a warning of previously defined
503 //anonymous_instance and unique_instance
508 (void)anonymous_instance;
509 (void)unique_instance;
513 } //detail_really_deep_namespace
515 }} //namespace boost { namespace interprocess
517 #include <boost/interprocess/detail/config_end.hpp>
519 #endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP