PageRenderTime 83ms CodeModel.GetById 53ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 466 lines | 250 code | 77 blank | 139 comment | 3 complexity | 83b3807714c1118d806fe707279cdb23 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_ADAPTIVE_POOL_HPP
 12#define BOOST_INTERPROCESS_ADAPTIVE_POOL_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/assert.hpp>
 25#include <boost/utility/addressof.hpp>
 26#include <boost/interprocess/detail/utilities.hpp>
 27#include <boost/interprocess/detail/type_traits.hpp>
 28#include <boost/interprocess/allocators/detail/adaptive_node_pool.hpp>
 29#include <boost/interprocess/exceptions.hpp>
 30#include <boost/interprocess/allocators/detail/allocator_common.hpp>
 31#include <boost/interprocess/containers/container/detail/multiallocation_chain.hpp>
 32#include <boost/interprocess/detail/mpl.hpp>
 33#include <memory>
 34#include <algorithm>
 35#include <cstddef>
 36
 37//!\file
 38//!Describes adaptive_pool pooled shared memory STL compatible allocator 
 39
 40namespace boost {
 41namespace interprocess {
 42
 43/// @cond
 44
 45namespace detail{
 46
 47template < unsigned int Version
 48         , class T
 49         , class SegmentManager
 50         , std::size_t NodesPerBlock
 51         , std::size_t MaxFreeBlocks
 52         , unsigned char OverheadPercent
 53         >
 54class adaptive_pool_base
 55   : public node_pool_allocation_impl
 56   < adaptive_pool_base
 57      < Version, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>
 58   , Version
 59   , T
 60   , SegmentManager
 61   >
 62{
 63   public:
 64   typedef typename SegmentManager::void_pointer         void_pointer;
 65   typedef SegmentManager                                segment_manager;
 66   typedef adaptive_pool_base
 67      <Version, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>   self_t;
 68
 69   /// @cond
 70
 71   template <int dummy>
 72   struct node_pool
 73   {
 74      typedef detail::shared_adaptive_node_pool
 75      < SegmentManager, sizeof_value<T>::value, NodesPerBlock, MaxFreeBlocks, OverheadPercent> type;
 76
 77      static type *get(void *p)
 78      {  return static_cast<type*>(p);  }
 79   };
 80   /// @endcond
 81
 82   BOOST_STATIC_ASSERT((Version <=2));
 83
 84   public:
 85   //-------
 86   typedef typename boost::
 87      pointer_to_other<void_pointer, T>::type            pointer;
 88   typedef typename boost::
 89      pointer_to_other<void_pointer, const T>::type      const_pointer;
 90   typedef T                                             value_type;
 91   typedef typename detail::add_reference
 92                     <value_type>::type                  reference;
 93   typedef typename detail::add_reference
 94                     <const value_type>::type            const_reference;
 95   typedef std::size_t                                   size_type;
 96   typedef std::ptrdiff_t                                difference_type;
 97
 98   typedef boost::interprocess::version_type<adaptive_pool_base, Version>   version;
 99   typedef boost::container::containers_detail::transform_multiallocation_chain
100      <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
101
102   //!Obtains adaptive_pool_base from 
103   //!adaptive_pool_base
104   template<class T2>
105   struct rebind
106   {  
107      typedef adaptive_pool_base<Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>       other;
108   };
109
110   /// @cond
111   private:
112   //!Not assignable from related adaptive_pool_base
113   template<unsigned int Version2, class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char O2>
114   adaptive_pool_base& operator=
115      (const adaptive_pool_base<Version2, T2, SegmentManager2, N2, F2, O2>&);
116
117   /// @endcond
118
119   public:
120   //!Constructor from a segment manager. If not present, constructs a node
121   //!pool. Increments the reference count of the associated node pool.
122   //!Can throw boost::interprocess::bad_alloc
123   adaptive_pool_base(segment_manager *segment_mngr) 
124      : mp_node_pool(detail::get_or_create_node_pool<typename node_pool<0>::type>(segment_mngr)) { }
125
126   //!Copy constructor from other adaptive_pool_base. Increments the reference 
127   //!count of the associated node pool. Never throws
128   adaptive_pool_base(const adaptive_pool_base &other) 
129      : mp_node_pool(other.get_node_pool()) 
130   {  
131      node_pool<0>::get(detail::get_pointer(mp_node_pool))->inc_ref_count();   
132   }
133
134   //!Assignment from other adaptive_pool_base
135   adaptive_pool_base& operator=(const adaptive_pool_base &other)
136   {
137      adaptive_pool_base c(other);
138      swap(*this, c);
139      return *this;
140   }
141
142   //!Copy constructor from related adaptive_pool_base. If not present, constructs
143   //!a node pool. Increments the reference count of the associated node pool.
144   //!Can throw boost::interprocess::bad_alloc
145   template<class T2>
146   adaptive_pool_base
147      (const adaptive_pool_base<Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
148      : mp_node_pool(detail::get_or_create_node_pool<typename node_pool<0>::type>(other.get_segment_manager())) { }
149
150   //!Destructor, removes node_pool_t from memory
151   //!if its reference count reaches to zero. Never throws
152   ~adaptive_pool_base() 
153   {  detail::destroy_node_pool_if_last_link(node_pool<0>::get(detail::get_pointer(mp_node_pool)));   }
154
155   //!Returns a pointer to the node pool.
156   //!Never throws
157   void* get_node_pool() const
158   {  return detail::get_pointer(mp_node_pool);   }
159
160   //!Returns the segment manager.
161   //!Never throws
162   segment_manager* get_segment_manager()const
163   {  return node_pool<0>::get(detail::get_pointer(mp_node_pool))->get_segment_manager();  }
164
165   //!Swaps allocators. Does not throw. If each allocator is placed in a
166   //!different memory segment, the result is undefined.
167   friend void swap(self_t &alloc1, self_t &alloc2)
168   {  detail::do_swap(alloc1.mp_node_pool, alloc2.mp_node_pool);  }
169
170   /// @cond
171   private:
172   void_pointer   mp_node_pool;
173   /// @endcond
174};
175
176//!Equality test for same type
177//!of adaptive_pool_base
178template<unsigned int V, class T, class S, std::size_t NPC, std::size_t F, unsigned char OP> inline
179bool operator==(const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc1, 
180                const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc2)
181   {  return alloc1.get_node_pool() == alloc2.get_node_pool(); }
182
183//!Inequality test for same type
184//!of adaptive_pool_base
185template<unsigned int V, class T, class S, std::size_t NPC, std::size_t F, unsigned char OP> inline
186bool operator!=(const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc1, 
187                const adaptive_pool_base<V, T, S, NPC, F, OP> &alloc2)
188   {  return alloc1.get_node_pool() != alloc2.get_node_pool(); }
189
190template < class T
191         , class SegmentManager
192         , std::size_t NodesPerBlock = 64
193         , std::size_t MaxFreeBlocks = 2
194         , unsigned char OverheadPercent = 5
195         >
196class adaptive_pool_v1
197   :  public adaptive_pool_base
198         < 1
199         , T
200         , SegmentManager
201         , NodesPerBlock
202         , MaxFreeBlocks
203         , OverheadPercent
204         >
205{
206   public:
207   typedef detail::adaptive_pool_base
208         < 1, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
209
210   template<class T2>
211   struct rebind
212   {  
213      typedef adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>  other;
214   };
215
216   adaptive_pool_v1(SegmentManager *segment_mngr) 
217      : base_t(segment_mngr)
218   {}
219
220   template<class T2>
221   adaptive_pool_v1
222      (const adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
223      : base_t(other)
224   {}
225};
226
227}  //namespace detail{
228
229/// @endcond
230
231//!An STL node allocator that uses a segment manager as memory 
232//!source. The internal pointer type will of the same type (raw, smart) as
233//!"typename SegmentManager::void_pointer" type. This allows
234//!placing the allocator in shared memory, memory mapped-files, etc...
235//!
236//!This node allocator shares a segregated storage between all instances 
237//!of adaptive_pool with equal sizeof(T) placed in the same segment 
238//!group. NodesPerBlock is the number of nodes allocated at once when the allocator
239//!needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
240//!that the adaptive node pool will hold. The rest of the totally free blocks will be
241//!deallocated with the segment manager.
242//!
243//!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
244//!(memory usable for nodes / total memory allocated from the segment manager)
245template < class T
246         , class SegmentManager
247         , std::size_t NodesPerBlock
248         , std::size_t MaxFreeBlocks
249         , unsigned char OverheadPercent
250         >
251class adaptive_pool
252   /// @cond
253   :  public detail::adaptive_pool_base
254         < 2
255         , T
256         , SegmentManager
257         , NodesPerBlock
258         , MaxFreeBlocks
259         , OverheadPercent
260         >
261   /// @endcond
262{
263
264   #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
265   typedef detail::adaptive_pool_base
266         < 2, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
267   public:
268   typedef boost::interprocess::version_type<adaptive_pool, 2>   version;
269
270   template<class T2>
271   struct rebind
272   {  
273      typedef adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>  other;
274   };
275
276   adaptive_pool(SegmentManager *segment_mngr) 
277      : base_t(segment_mngr)
278   {}
279
280   template<class T2>
281   adaptive_pool
282      (const adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
283      : base_t(other)
284   {}
285
286   #else //BOOST_INTERPROCESS_DOXYGEN_INVOKED
287   public:
288   typedef implementation_defined::segment_manager       segment_manager;
289   typedef segment_manager::void_pointer                 void_pointer;
290   typedef implementation_defined::pointer               pointer;
291   typedef implementation_defined::const_pointer         const_pointer;
292   typedef T                                             value_type;
293   typedef typename detail::add_reference
294                     <value_type>::type                  reference;
295   typedef typename detail::add_reference
296                     <const value_type>::type            const_reference;
297   typedef std::size_t                                   size_type;
298   typedef std::ptrdiff_t                                difference_type;
299
300   //!Obtains adaptive_pool from 
301   //!adaptive_pool
302   template<class T2>
303   struct rebind
304   {  
305      typedef adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
306   };
307
308   private:
309   //!Not assignable from
310   //!related adaptive_pool
311   template<class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char OP2>
312   adaptive_pool& operator=
313      (const adaptive_pool<T2, SegmentManager2, N2, F2, OP2>&);
314
315   //!Not assignable from 
316   //!other adaptive_pool
317   //adaptive_pool& operator=(const adaptive_pool&);
318
319   public:
320   //!Constructor from a segment manager. If not present, constructs a node
321   //!pool. Increments the reference count of the associated node pool.
322   //!Can throw boost::interprocess::bad_alloc
323   adaptive_pool(segment_manager *segment_mngr);
324
325   //!Copy constructor from other adaptive_pool. Increments the reference 
326   //!count of the associated node pool. Never throws
327   adaptive_pool(const adaptive_pool &other);
328
329   //!Copy constructor from related adaptive_pool. If not present, constructs
330   //!a node pool. Increments the reference count of the associated node pool.
331   //!Can throw boost::interprocess::bad_alloc
332   template<class T2>
333   adaptive_pool
334      (const adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other);
335
336   //!Destructor, removes node_pool_t from memory
337   //!if its reference count reaches to zero. Never throws
338   ~adaptive_pool();
339
340   //!Returns a pointer to the node pool.
341   //!Never throws
342   void* get_node_pool() const;
343
344   //!Returns the segment manager.
345   //!Never throws
346   segment_manager* get_segment_manager()const;
347
348   //!Returns the number of elements that could be allocated.
349   //!Never throws
350   size_type max_size() const;
351
352   //!Allocate memory for an array of count elements. 
353   //!Throws boost::interprocess::bad_alloc if there is no enough memory
354   pointer allocate(size_type count, cvoid_pointer hint = 0);
355
356   //!Deallocate allocated memory.
357   //!Never throws
358   void deallocate(const pointer &ptr, size_type count);
359
360   //!Deallocates all free blocks
361   //!of the pool
362   void deallocate_free_blocks();
363
364   //!Swaps allocators. Does not throw. If each allocator is placed in a
365   //!different memory segment, the result is undefined.
366   friend void swap(self_t &alloc1, self_t &alloc2);
367
368   //!Returns address of mutable object.
369   //!Never throws
370   pointer address(reference value) const;
371
372   //!Returns address of non mutable object.
373   //!Never throws
374   const_pointer address(const_reference value) const;
375
376   //!Copy construct an object. 
377   //!Throws if T's copy constructor throws
378   void construct(const pointer &ptr, const_reference v);
379
380   //!Destroys object. Throws if object's
381   //!destructor throws
382   void destroy(const pointer &ptr);
383
384   //!Returns maximum the number of objects the previously allocated memory
385   //!pointed by p can hold. This size only works for memory allocated with
386   //!allocate, allocation_command and allocate_many.
387   size_type size(const pointer &p) const;
388
389   std::pair<pointer, bool>
390      allocation_command(boost::interprocess::allocation_type command,
391                         size_type limit_size, 
392                         size_type preferred_size,
393                         size_type &received_size, const pointer &reuse = 0);
394
395   //!Allocates many elements of size elem_size in a contiguous block
396   //!of memory. The minimum number to be allocated is min_elements,
397   //!the preferred and maximum number is
398   //!preferred_elements. The number of actually allocated elements is
399   //!will be assigned to received_size. The elements must be deallocated
400   //!with deallocate(...)
401   multiallocation_chain allocate_many(size_type elem_size, std::size_t num_elements);
402
403   //!Allocates n_elements elements, each one of size elem_sizes[i]in a
404   //!contiguous block
405   //!of memory. The elements must be deallocated
406   multiallocation_chain allocate_many(const size_type *elem_sizes, size_type n_elements);
407
408   //!Allocates many elements of size elem_size in a contiguous block
409   //!of memory. The minimum number to be allocated is min_elements,
410   //!the preferred and maximum number is
411   //!preferred_elements. The number of actually allocated elements is
412   //!will be assigned to received_size. The elements must be deallocated
413   //!with deallocate(...)
414   void deallocate_many(multiallocation_chain chain);
415
416   //!Allocates just one object. Memory allocated with this function
417   //!must be deallocated only with deallocate_one().
418   //!Throws boost::interprocess::bad_alloc if there is no enough memory
419   pointer allocate_one();
420
421   //!Allocates many elements of size == 1 in a contiguous block
422   //!of memory. The minimum number to be allocated is min_elements,
423   //!the preferred and maximum number is
424   //!preferred_elements. The number of actually allocated elements is
425   //!will be assigned to received_size. Memory allocated with this function
426   //!must be deallocated only with deallocate_one().
427   multiallocation_chain allocate_individual(std::size_t num_elements);
428
429   //!Deallocates memory previously allocated with allocate_one().
430   //!You should never use deallocate_one to deallocate memory allocated
431   //!with other functions different from allocate_one(). Never throws
432   void deallocate_one(const pointer &p);
433
434   //!Allocates many elements of size == 1 in a contiguous block
435   //!of memory. The minimum number to be allocated is min_elements,
436   //!the preferred and maximum number is
437   //!preferred_elements. The number of actually allocated elements is
438   //!will be assigned to received_size. Memory allocated with this function
439   //!must be deallocated only with deallocate_one().
440   void deallocate_individual(multiallocation_chain it);
441   #endif
442};
443
444#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
445
446//!Equality test for same type
447//!of adaptive_pool
448template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
449bool operator==(const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1, 
450                const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
451
452//!Inequality test for same type
453//!of adaptive_pool
454template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
455bool operator!=(const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1, 
456                const adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
457
458#endif
459
460}  //namespace interprocess {
461}  //namespace boost {
462
463#include <boost/interprocess/detail/config_end.hpp>
464
465#endif   //#ifndef BOOST_INTERPROCESS_ADAPTIVE_POOL_HPP
466