PageRenderTime 59ms CodeModel.GetById 20ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/boost/interprocess/allocators/allocator.hpp

http://hadesmem.googlecode.com/
C++ Header | 305 lines | 151 code | 55 blank | 99 comment | 5 complexity | f2d1a2518f1fdb93c8f52c45bba141eb 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_ALLOCATOR_HPP
 12#define BOOST_INTERPROCESS_ALLOCATOR_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/interprocess/interprocess_fwd.hpp>
 24#include <boost/interprocess/containers/allocation_type.hpp>
 25#include <boost/interprocess/containers/container/detail/multiallocation_chain.hpp>
 26#include <boost/interprocess/allocators/detail/allocator_common.hpp>
 27#include <boost/interprocess/detail/utilities.hpp>
 28#include <boost/interprocess/containers/version_type.hpp>
 29#include <boost/interprocess/exceptions.hpp>
 30#include <boost/assert.hpp>
 31#include <boost/utility/addressof.hpp>
 32#include <boost/interprocess/detail/type_traits.hpp>
 33
 34#include <memory>
 35#include <algorithm>
 36#include <cstddef>
 37#include <stdexcept>
 38
 39//!\file
 40//!Describes an allocator that allocates portions of fixed size
 41//!memory buffer (shared memory, mapped file...)
 42
 43namespace boost {
 44namespace interprocess {
 45
 46
 47//!An STL compatible allocator that uses a segment manager as 
 48//!memory source. The internal pointer type will of the same type (raw, smart) as
 49//!"typename SegmentManager::void_pointer" type. This allows
 50//!placing the allocator in shared memory, memory mapped-files, etc...
 51template<class T, class SegmentManager>
 52class allocator 
 53{
 54   public:
 55   //Segment manager
 56   typedef SegmentManager                                segment_manager;
 57   typedef typename SegmentManager::void_pointer         void_pointer;
 58
 59   /// @cond
 60   private:
 61
 62   //Self type
 63   typedef allocator<T, SegmentManager>   self_t;
 64
 65   //Pointer to void
 66   typedef typename segment_manager::void_pointer  aux_pointer_t;
 67
 68   //Typedef to const void pointer
 69   typedef typename 
 70      boost::pointer_to_other
 71         <aux_pointer_t, const void>::type   cvoid_ptr;
 72
 73   //Pointer to the allocator
 74   typedef typename boost::pointer_to_other
 75      <cvoid_ptr, segment_manager>::type     alloc_ptr_t;
 76
 77   //Not assignable from related allocator
 78   template<class T2, class SegmentManager2>
 79   allocator& operator=(const allocator<T2, SegmentManager2>&);
 80
 81   //Not assignable from other allocator
 82   allocator& operator=(const allocator&);
 83
 84   //Pointer to the allocator
 85   alloc_ptr_t mp_mngr;
 86   /// @endcond
 87
 88   public:
 89   typedef T                                    value_type;
 90   typedef typename boost::pointer_to_other
 91      <cvoid_ptr, T>::type                      pointer;
 92   typedef typename boost::
 93      pointer_to_other<pointer, const T>::type  const_pointer;
 94   typedef typename detail::add_reference
 95                     <value_type>::type         reference;
 96   typedef typename detail::add_reference
 97                     <const value_type>::type   const_reference;
 98   typedef std::size_t                          size_type;
 99   typedef std::ptrdiff_t                       difference_type;
100
101   typedef boost::interprocess::version_type<allocator, 2>   version;
102
103   /// @cond
104
105   //Experimental. Don't use.
106   typedef boost::container::containers_detail::transform_multiallocation_chain
107      <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
108   /// @endcond
109
110   //!Obtains an allocator that allocates
111   //!objects of type T2
112   template<class T2>
113   struct rebind
114   {   
115      typedef allocator<T2, SegmentManager>     other;
116   };
117
118   //!Returns the segment manager.
119   //!Never throws
120   segment_manager* get_segment_manager()const
121   {  return detail::get_pointer(mp_mngr);   }
122
123   //!Constructor from the segment manager.
124   //!Never throws
125   allocator(segment_manager *segment_mngr) 
126      : mp_mngr(segment_mngr) { }
127
128   //!Constructor from other allocator.
129   //!Never throws
130   allocator(const allocator &other) 
131      : mp_mngr(other.get_segment_manager()){ }
132
133   //!Constructor from related allocator.
134   //!Never throws
135   template<class T2>
136   allocator(const allocator<T2, SegmentManager> &other) 
137      : mp_mngr(other.get_segment_manager()){}
138
139   //!Allocates memory for an array of count elements. 
140   //!Throws boost::interprocess::bad_alloc if there is no enough memory
141   pointer allocate(size_type count, cvoid_ptr hint = 0)
142   {
143      (void)hint;
144      if(count > this->max_size())
145         throw bad_alloc();
146      return pointer(static_cast<value_type*>(mp_mngr->allocate(count*sizeof(T))));
147   }
148
149   //!Deallocates memory previously allocated.
150   //!Never throws
151   void deallocate(const pointer &ptr, size_type)
152   {  mp_mngr->deallocate((void*)detail::get_pointer(ptr));  }
153
154   //!Returns the number of elements that could be allocated.
155   //!Never throws
156   size_type max_size() const
157   {  return mp_mngr->get_size()/sizeof(T);   }
158
159   //!Swap segment manager. Does not throw. If each allocator is placed in
160   //!different memory segments, the result is undefined.
161   friend void swap(self_t &alloc1, self_t &alloc2)
162   {  detail::do_swap(alloc1.mp_mngr, alloc2.mp_mngr);   }
163
164   //!Returns maximum the number of objects the previously allocated memory
165   //!pointed by p can hold. This size only works for memory allocated with
166   //!allocate, allocation_command and allocate_many.
167   size_type size(const pointer &p) const
168   {  
169      return (size_type)mp_mngr->size(detail::get_pointer(p))/sizeof(T);
170   }
171
172   std::pair<pointer, bool>
173      allocation_command(boost::interprocess::allocation_type command,
174                         size_type limit_size, 
175                         size_type preferred_size,
176                         size_type &received_size, const pointer &reuse = 0)
177   {
178      return mp_mngr->allocation_command
179         (command, limit_size, preferred_size, received_size, detail::get_pointer(reuse));
180   }
181
182   //!Allocates many elements of size elem_size in a contiguous block
183   //!of memory. The minimum number to be allocated is min_elements,
184   //!the preferred and maximum number is
185   //!preferred_elements. The number of actually allocated elements is
186   //!will be assigned to received_size. The elements must be deallocated
187   //!with deallocate(...)
188   multiallocation_chain allocate_many
189      (size_type elem_size, std::size_t num_elements)
190   {
191      return multiallocation_chain(mp_mngr->allocate_many(sizeof(T)*elem_size, num_elements));
192   }
193
194   //!Allocates n_elements elements, each one of size elem_sizes[i]in a
195   //!contiguous block
196   //!of memory. The elements must be deallocated
197   multiallocation_chain allocate_many
198      (const size_type *elem_sizes, size_type n_elements)
199   {
200      multiallocation_chain(mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T)));
201   }
202
203   //!Allocates many elements of size elem_size in a contiguous block
204   //!of memory. The minimum number to be allocated is min_elements,
205   //!the preferred and maximum number is
206   //!preferred_elements. The number of actually allocated elements is
207   //!will be assigned to received_size. The elements must be deallocated
208   //!with deallocate(...)
209   void deallocate_many(multiallocation_chain chain)
210   {
211      return mp_mngr->deallocate_many(chain.extract_multiallocation_chain());
212   }
213
214   //!Allocates just one object. Memory allocated with this function
215   //!must be deallocated only with deallocate_one().
216   //!Throws boost::interprocess::bad_alloc if there is no enough memory
217   pointer allocate_one()
218   {  return this->allocate(1);  }
219
220   //!Allocates many elements of size == 1 in a contiguous block
221   //!of memory. The minimum number to be allocated is min_elements,
222   //!the preferred and maximum number is
223   //!preferred_elements. The number of actually allocated elements is
224   //!will be assigned to received_size. Memory allocated with this function
225   //!must be deallocated only with deallocate_one().
226   multiallocation_chain allocate_individual
227      (std::size_t num_elements)
228   {  return this->allocate_many(1, num_elements); }
229
230   //!Deallocates memory previously allocated with allocate_one().
231   //!You should never use deallocate_one to deallocate memory allocated
232   //!with other functions different from allocate_one(). Never throws
233   void deallocate_one(const pointer &p)
234   {  return this->deallocate(p, 1);  }
235
236   //!Allocates many elements of size == 1 in a contiguous block
237   //!of memory. The minimum number to be allocated is min_elements,
238   //!the preferred and maximum number is
239   //!preferred_elements. The number of actually allocated elements is
240   //!will be assigned to received_size. Memory allocated with this function
241   //!must be deallocated only with deallocate_one().
242   void deallocate_individual(multiallocation_chain chain)
243   {  return this->deallocate_many(boost::interprocess::move(chain)); }
244
245   //!Returns address of mutable object.
246   //!Never throws
247   pointer address(reference value) const
248   {  return pointer(boost::addressof(value));  }
249
250   //!Returns address of non mutable object.
251   //!Never throws
252   const_pointer address(const_reference value) const
253   {  return const_pointer(boost::addressof(value));  }
254
255   //!Copy construct an object
256   //!Throws if T's copy constructor throws
257   void construct(const pointer &ptr, const_reference v)
258   {  new((void*)detail::get_pointer(ptr)) value_type(v);  }
259
260   //!Default construct an object. 
261   //!Throws if T's default constructor throws
262   void construct(const pointer &ptr)
263   {  new((void*)detail::get_pointer(ptr)) value_type;  }
264
265   //!Destroys object. Throws if object's
266   //!destructor throws
267   void destroy(const pointer &ptr)
268   {  BOOST_ASSERT(ptr != 0); (*ptr).~value_type();  }
269};
270
271//!Equality test for same type
272//!of allocator
273template<class T, class SegmentManager> inline
274bool operator==(const allocator<T , SegmentManager>  &alloc1, 
275                const allocator<T, SegmentManager>  &alloc2)
276   {  return alloc1.get_segment_manager() == alloc2.get_segment_manager(); }
277
278//!Inequality test for same type
279//!of allocator
280template<class T, class SegmentManager> inline
281bool operator!=(const allocator<T, SegmentManager>  &alloc1, 
282                const allocator<T, SegmentManager>  &alloc2)
283   {  return alloc1.get_segment_manager() != alloc2.get_segment_manager(); }
284
285}  //namespace interprocess {
286
287/// @cond
288
289template<class T>
290struct has_trivial_destructor;
291
292template<class T, class SegmentManager>
293struct has_trivial_destructor
294   <boost::interprocess::allocator <T, SegmentManager> >
295{
296   enum { value = true };
297};
298/// @endcond
299
300}  //namespace boost {
301
302#include <boost/interprocess/detail/config_end.hpp>
303
304#endif   //BOOST_INTERPROCESS_ALLOCATOR_HPP
305