PageRenderTime 90ms CodeModel.GetById 10ms app.highlight 70ms RepoModel.GetById 3ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/detail/segment_manager_helper.hpp

http://hadesmem.googlecode.com/
C++ Header | 504 lines | 370 code | 98 blank | 36 comment | 16 complexity | 6b39c568f7235027ce63fa4bb92eee6a MD5 | raw file
  1//////////////////////////////////////////////////////////////////////////////
  2//
  3// (C) Copyright Ion Gaztanaga 2005-2009. 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)
  6//
  7// See http://www.boost.org/libs/interprocess for documentation.
  8//
  9//////////////////////////////////////////////////////////////////////////////
 10
 11#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP
 12#define BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP
 13
 14#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 15#  pragma once
 16#endif
 17
 18#include <boost/interprocess/detail/config_begin.hpp>
 19#include <boost/interprocess/detail/workaround.hpp>
 20
 21#include <boost/pointer_to_other.hpp>
 22
 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 <cstddef>   //std::size_t
 29#include <string>    //char_traits
 30#include <new>       //std::nothrow
 31#include <utility>   //std::pair
 32#include <boost/assert.hpp>   //BOOST_ASSERT
 33#include <functional>   //unary_function
 34#ifndef BOOST_NO_EXCEPTIONS
 35#include <exception>
 36#endif
 37
 38//!\file
 39//!Describes the object placed in a memory segment that provides
 40//!named object allocation capabilities.
 41
 42namespace boost{
 43namespace interprocess{
 44
 45template<class MemoryManager>
 46class segment_manager_base;
 47
 48//!An integer that describes the type of the
 49//!instance constructed in memory
 50enum instance_type {   anonymous_type, named_type, unique_type, max_allocation_type };
 51
 52namespace detail{
 53
 54template<class MemoryAlgorithm>
 55class mem_algo_deallocator
 56{
 57   void *            m_ptr;
 58   MemoryAlgorithm & m_algo;
 59
 60   public:
 61   mem_algo_deallocator(void *ptr, MemoryAlgorithm &algo)
 62      :  m_ptr(ptr), m_algo(algo)
 63   {}
 64
 65   void release()
 66   {  m_ptr = 0;  }
 67
 68   ~mem_algo_deallocator()
 69   {  if(m_ptr) m_algo.deallocate(m_ptr);  }
 70};
 71
 72/// @cond
 73struct block_header
 74{
 75   std::size_t    m_value_bytes;
 76   unsigned short m_num_char;
 77   unsigned char  m_value_alignment;
 78   unsigned char  m_alloc_type_sizeof_char;
 79
 80   block_header(std::size_t value_bytes
 81               ,std::size_t value_alignment
 82               ,std::size_t alloc_type
 83               ,std::size_t sizeof_char
 84               ,std::size_t num_char
 85               )
 86      :  m_value_bytes(value_bytes)
 87      ,  m_num_char(num_char)
 88      ,  m_value_alignment(value_alignment)
 89      ,  m_alloc_type_sizeof_char
 90         ( ((unsigned char)alloc_type << 5u) | 
 91           ((unsigned char)sizeof_char & 0x1F)   )
 92   {};
 93
 94
 95   template<class T>
 96   block_header &operator= (const T& )
 97   {  return *this;  }
 98
 99   std::size_t total_size() const
100   {
101      if(alloc_type() != anonymous_type){
102         return name_offset() + (m_num_char+1)*sizeof_char();
103      }
104      else{
105         return value_offset() + m_value_bytes;
106      }
107   }
108
109   std::size_t value_bytes() const
110   {  return m_value_bytes;   }
111
112   template<class Header>
113   std::size_t total_size_with_header() const
114   {
115      return get_rounded_size
116               (  sizeof(Header)
117               ,  detail::alignment_of<block_header>::value)
118           + total_size();
119   }
120
121   std::size_t alloc_type() const
122   {  return (m_alloc_type_sizeof_char >> 5u)&(unsigned char)0x7;  }
123
124   std::size_t sizeof_char() const
125   {  return m_alloc_type_sizeof_char & (unsigned char)0x1F;  }
126
127   template<class CharType>
128   CharType *name() const
129   {  
130      return const_cast<CharType*>(reinterpret_cast<const CharType*>
131         (reinterpret_cast<const char*>(this) + name_offset()));
132   }
133
134   std::size_t name_length() const
135   {  return m_num_char;   }
136
137   std::size_t name_offset() const
138   { 
139      return value_offset() + get_rounded_size(m_value_bytes, sizeof_char());
140   }
141
142   void *value() const
143   {
144      return const_cast<char*>((reinterpret_cast<const char*>(this) + value_offset()));
145   }
146
147   std::size_t value_offset() const
148   {
149      return get_rounded_size(sizeof(block_header), m_value_alignment);
150   }
151
152   template<class CharType>
153   bool less_comp(const block_header &b) const
154   {
155      return m_num_char < b.m_num_char ||
156             (m_num_char < b.m_num_char && 
157               std::char_traits<CharType>::compare
158                  (name<CharType>(), b.name<CharType>(), m_num_char) < 0);
159   }
160
161   template<class CharType>
162   bool equal_comp(const block_header &b) const
163   {
164      return m_num_char == b.m_num_char &&
165             std::char_traits<CharType>::compare
166               (name<CharType>(), b.name<CharType>(), m_num_char) == 0;
167   }
168
169   template<class T>
170   static block_header *block_header_from_value(T *value)
171   {  return block_header_from_value(value, sizeof(T), detail::alignment_of<T>::value);  }
172
173   static block_header *block_header_from_value(const void *value, std::size_t sz, std::size_t algn)
174   {  
175      block_header * hdr = 
176         const_cast<block_header*>
177            (reinterpret_cast<const block_header*>(reinterpret_cast<const char*>(value) - 
178               get_rounded_size(sizeof(block_header), algn)));
179      (void)sz;
180      //Some sanity checks
181      BOOST_ASSERT(hdr->m_value_alignment == algn);
182      BOOST_ASSERT(hdr->m_value_bytes % sz == 0);
183      return hdr;
184   }
185
186   template<class Header>
187   static block_header *from_first_header(Header *header)
188   {  
189      block_header * hdr = 
190         reinterpret_cast<block_header*>(reinterpret_cast<char*>(header) + 
191            get_rounded_size(sizeof(Header), detail::alignment_of<block_header>::value));
192      //Some sanity checks
193      return hdr;
194   }
195
196   template<class Header>
197   static Header *to_first_header(block_header *bheader)
198   {  
199      Header * hdr = 
200         reinterpret_cast<Header*>(reinterpret_cast<char*>(bheader) - 
201         get_rounded_size(sizeof(Header), detail::alignment_of<block_header>::value));
202      //Some sanity checks
203      return hdr;
204   }
205};
206
207inline void array_construct(void *mem, std::size_t num, detail::in_place_interface &table)
208{
209   //Try constructors
210   std::size_t constructed = 0;
211   BOOST_TRY{
212      table.construct_n(mem, num, constructed);
213   }
214   //If there is an exception call destructors and erase index node
215   BOOST_CATCH(...){
216      std::size_t destroyed = 0;
217      table.destroy_n(mem, constructed, destroyed);
218      BOOST_RETHROW
219   }
220   BOOST_CATCH_END
221}
222
223template<class CharT>
224struct intrusive_compare_key
225{
226   typedef CharT char_type;
227
228   intrusive_compare_key(const CharT *str, std::size_t len)
229      :  mp_str(str), m_len(len)
230   {}
231
232   const CharT *  mp_str;
233   std::size_t    m_len;
234};
235
236//!This struct indicates an anonymous object creation
237//!allocation
238template<instance_type type>
239class instance_t
240{
241   instance_t(){}
242};
243
244template<class T>
245struct char_if_void
246{
247   typedef T type;
248};
249
250template<>
251struct char_if_void<void>
252{
253   typedef char type;
254};
255
256typedef instance_t<anonymous_type>  anonymous_instance_t;
257typedef instance_t<unique_type>     unique_instance_t;
258
259
260template<class Hook, class CharType>
261struct intrusive_value_type_impl
262   :  public Hook
263{
264   private:
265   //Non-copyable
266   intrusive_value_type_impl(const intrusive_value_type_impl &);
267   intrusive_value_type_impl& operator=(const intrusive_value_type_impl &);
268
269   public:
270   typedef CharType char_type;
271
272   intrusive_value_type_impl(){}
273
274   enum  {  BlockHdrAlignment = detail::alignment_of<block_header>::value  };
275
276   block_header *get_block_header() const
277   {
278      return const_cast<block_header*>
279         (reinterpret_cast<const block_header *>(reinterpret_cast<const char*>(this) +
280            get_rounded_size(sizeof(*this), BlockHdrAlignment)));
281   }
282
283   bool operator <(const intrusive_value_type_impl<Hook, CharType> & other) const
284   {  return (this->get_block_header())->template less_comp<CharType>(*other.get_block_header());  }
285
286   bool operator ==(const intrusive_value_type_impl<Hook, CharType> & other) const
287   {  return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header());  }
288
289   static intrusive_value_type_impl *get_intrusive_value_type(block_header *hdr)
290   {
291      return reinterpret_cast<intrusive_value_type_impl *>(reinterpret_cast<char*>(hdr) -
292         get_rounded_size(sizeof(intrusive_value_type_impl), BlockHdrAlignment));
293   }
294
295   CharType *name() const
296   {  return get_block_header()->template name<CharType>(); }
297
298   std::size_t name_length() const
299   {  return get_block_header()->name_length(); }
300
301   void *value() const
302   {  return get_block_header()->value(); }
303};
304
305template<class CharType>
306class char_ptr_holder
307{
308   public:
309   char_ptr_holder(const CharType *name) 
310      : m_name(name)
311   {}
312
313   char_ptr_holder(const detail::anonymous_instance_t *) 
314      : m_name(static_cast<CharType*>(0))
315   {}
316
317   char_ptr_holder(const detail::unique_instance_t *) 
318      : m_name(reinterpret_cast<CharType*>(-1))
319   {}
320
321   operator const CharType *()
322   {  return m_name;  }
323
324   private:
325   const CharType *m_name;
326};
327
328//!The key of the the named allocation information index. Stores an offset pointer 
329//!to a null terminated string and the length of the string to speed up sorting
330template<class CharT, class VoidPointer>
331struct index_key
332{
333   typedef typename boost::
334      pointer_to_other<VoidPointer, const CharT>::type   const_char_ptr_t;
335   typedef CharT                                         char_type;
336
337   private:
338   //Offset pointer to the object's name
339   const_char_ptr_t  mp_str;
340   //Length of the name buffer (null NOT included)
341   std::size_t       m_len;
342   public:
343
344   //!Constructor of the key
345   index_key (const char_type *name, std::size_t length)
346      : mp_str(name), m_len(length) {}
347
348   //!Less than function for index ordering
349   bool operator < (const index_key & right) const
350   {
351      return (m_len < right.m_len) || 
352               (m_len == right.m_len && 
353               std::char_traits<char_type>::compare 
354                  (detail::get_pointer(mp_str)
355                  ,detail::get_pointer(right.mp_str), m_len) < 0);
356   }
357
358   //!Equal to function for index ordering
359   bool operator == (const index_key & right) const
360   {
361      return   m_len == right.m_len && 
362               std::char_traits<char_type>::compare 
363                  (detail::get_pointer(mp_str),
364                   detail::get_pointer(right.mp_str), m_len) == 0;
365   }
366
367   void name(const CharT *name)
368   {  mp_str = name; }
369
370   void name_length(std::size_t len)
371   {  m_len = len; }
372
373   const CharT *name() const
374   {  return detail::get_pointer(mp_str); }
375
376   std::size_t name_length() const
377   {  return m_len; }
378};
379
380//!The index_data stores a pointer to a buffer and the element count needed
381//!to know how many destructors must be called when calling destroy
382template<class VoidPointer>
383struct index_data
384{
385   typedef VoidPointer void_pointer;
386   void_pointer    m_ptr;
387   index_data(void *ptr) : m_ptr(ptr){}
388
389   void *value() const
390   {  return static_cast<void*>(detail::get_pointer(m_ptr));  }
391};
392
393template<class MemoryAlgorithm>
394struct segment_manager_base_type
395{  typedef segment_manager_base<MemoryAlgorithm> type;   };
396
397template<class CharT, class MemoryAlgorithm>
398struct index_config
399{
400   typedef typename MemoryAlgorithm::void_pointer        void_pointer;
401   typedef CharT                                         char_type;
402   typedef detail::index_key<CharT, void_pointer>        key_type;
403   typedef detail::index_data<void_pointer>              mapped_type;
404   typedef typename segment_manager_base_type
405      <MemoryAlgorithm>::type                            segment_manager_base;
406
407   template<class HeaderBase>
408   struct intrusive_value_type
409   {  typedef detail::intrusive_value_type_impl<HeaderBase, CharT>  type; };
410
411   typedef intrusive_compare_key<CharT>            intrusive_compare_key_type;
412};
413
414template<class Iterator, bool intrusive>
415class segment_manager_iterator_value_adaptor
416{
417   typedef typename Iterator::value_type        iterator_val_t;
418   typedef typename iterator_val_t::char_type   char_type;
419
420   public:
421   segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
422      :  m_val(&val)
423   {}
424
425   const char_type *name() const
426   {  return m_val->name(); }
427
428   std::size_t name_length() const
429   {  return m_val->name_length(); }
430
431   const void *value() const
432   {  return m_val->value(); }
433
434   const typename Iterator::value_type *m_val;
435};
436
437
438template<class Iterator>
439class segment_manager_iterator_value_adaptor<Iterator, false>
440{
441   typedef typename Iterator::value_type        iterator_val_t;
442   typedef typename iterator_val_t::first_type  first_type;
443   typedef typename iterator_val_t::second_type second_type;
444   typedef typename first_type::char_type       char_type;
445
446   public:
447   segment_manager_iterator_value_adaptor(const typename Iterator::value_type &val)
448      :  m_val(&val)
449   {}
450
451   const char_type *name() const
452   {  return m_val->first.name(); }
453
454   std::size_t name_length() const
455   {  return m_val->first.name_length(); }
456
457   const void *value() const
458   {
459      return reinterpret_cast<block_header*>
460         (detail::get_pointer(m_val->second.m_ptr))->value();
461   }
462
463   const typename Iterator::value_type *m_val;
464};
465
466template<class Iterator, bool intrusive>
467struct segment_manager_iterator_transform
468   :  std::unary_function< typename Iterator::value_type
469                         , segment_manager_iterator_value_adaptor<Iterator, intrusive> >
470{
471   typedef segment_manager_iterator_value_adaptor<Iterator, intrusive> result_type;
472   
473   result_type operator()(const typename Iterator::value_type &arg) const
474   {  return result_type(arg); }
475};
476
477}  //namespace detail {
478
479//These pointers are the ones the user will use to 
480//indicate previous allocation types
481static const detail::anonymous_instance_t   * anonymous_instance = 0;
482static const detail::unique_instance_t      * unique_instance = 0;
483
484namespace detail_really_deep_namespace {
485
486//Otherwise, gcc issues a warning of previously defined
487//anonymous_instance and unique_instance
488struct dummy
489{
490   dummy()
491   {
492      (void)anonymous_instance;
493      (void)unique_instance;
494   }
495};
496
497}  //detail_really_deep_namespace
498
499}} //namespace boost { namespace interprocess
500
501#include <boost/interprocess/detail/config_end.hpp>
502
503#endif //#ifndef BOOST_INTERPROCESS_SEGMENT_MANAGER_BASE_HPP
504