private_node_allocator.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 444 行 · 第 1/2 页

HPP
444
字号
////////////////////////////////////////////////////////////////////////////////// (C) Copyright Ion Gaztanaga 2005-2008. Distributed under the Boost// Software License, Version 1.0. (See accompanying file// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//// See http://www.boost.org/libs/interprocess for documentation.////////////////////////////////////////////////////////////////////////////////#ifndef BOOST_INTERPROCESS_PRIVATE_NODE_ALLOCATOR_HPP#define BOOST_INTERPROCESS_PRIVATE_NODE_ALLOCATOR_HPP#if (defined _MSC_VER) && (_MSC_VER >= 1200)#  pragma once#endif#include <boost/interprocess/detail/config_begin.hpp>#include <boost/interprocess/detail/workaround.hpp>#include <boost/interprocess/interprocess_fwd.hpp>#include <boost/assert.hpp>#include <boost/utility/addressof.hpp>#include <boost/interprocess/allocators/detail/node_pool.hpp>#include <boost/interprocess/exceptions.hpp>#include <boost/interprocess/detail/utilities.hpp>#include <boost/interprocess/detail/workaround.hpp>#include <memory>#include <algorithm>#include <cstddef>//!\file//!Describes private_node_allocator_base pooled shared memory STL compatible allocator namespace boost {namespace interprocess {/// @condnamespace detail {template < unsigned int Version         , class T         , class SegmentManager         , std::size_t NodesPerBlock         >class private_node_allocator_base   : public node_pool_allocation_impl   < private_node_allocator_base < Version, T, SegmentManager, NodesPerBlock>   , Version   , T   , SegmentManager   >{   public:   //Segment manager   typedef SegmentManager                                segment_manager;   typedef typename SegmentManager::void_pointer         void_pointer;   /// @cond   private:   typedef private_node_allocator_base      < Version, T, SegmentManager, NodesPerBlock>       self_t;   typedef detail::private_node_pool      <SegmentManager      , sizeof_value<T>::value      , NodesPerBlock      > node_pool_t;   BOOST_STATIC_ASSERT((Version <=2));   /// @endcond   public:   typedef typename detail::      pointer_to_other<void_pointer, T>::type            pointer;   typedef typename detail::      pointer_to_other<void_pointer, const T>::type      const_pointer;   typedef T                                             value_type;   typedef typename detail::add_reference                     <value_type>::type                  reference;   typedef typename detail::add_reference                     <const value_type>::type            const_reference;   typedef std::size_t                                   size_type;   typedef std::ptrdiff_t                                difference_type;   typedef detail::version_type      <private_node_allocator_base, Version>              version;   typedef transform_iterator      < typename SegmentManager::         multiallocation_iterator      , detail::cast_functor <T> >              multiallocation_iterator;   typedef typename SegmentManager::      multiallocation_chain                     multiallocation_chain;   //!Obtains node_allocator from other node_allocator   template<class T2>   struct rebind   {        typedef private_node_allocator_base         <Version, T2, SegmentManager, NodesPerBlock>   other;   };   /// @cond   template <int dummy>   struct node_pool   {      typedef detail::private_node_pool      <SegmentManager      , sizeof_value<T>::value      , NodesPerBlock      > type;      static type *get(void *p)      {  return static_cast<type*>(p);  }   };   private:   //!Not assignable from related private_node_allocator_base   template<unsigned int Version2, class T2, class MemoryAlgorithm2, std::size_t N2>   private_node_allocator_base& operator=      (const private_node_allocator_base<Version2, T2, MemoryAlgorithm2, N2>&);   //!Not assignable from other private_node_allocator_base   private_node_allocator_base& operator=(const private_node_allocator_base&);   /// @endcond   public:   //!Constructor from a segment manager   private_node_allocator_base(segment_manager *segment_mngr)      : m_node_pool(segment_mngr)   {}   //!Copy constructor from other private_node_allocator_base. Never throws   private_node_allocator_base(const private_node_allocator_base &other)      : m_node_pool(other.get_segment_manager())   {}   //!Copy constructor from related private_node_allocator_base. Never throws.   template<class T2>   private_node_allocator_base      (const private_node_allocator_base         <Version, T2, SegmentManager, NodesPerBlock> &other)      : m_node_pool(other.get_segment_manager())   {}   //!Destructor, frees all used memory. Never throws   ~private_node_allocator_base()    {}   //!Returns the segment manager. Never throws   segment_manager* get_segment_manager()const   {  return m_node_pool.get_segment_manager(); }   //!Returns the internal node pool. Never throws   node_pool_t* get_node_pool() const   {  return const_cast<node_pool_t*>(&m_node_pool); }   //!Swaps allocators. Does not throw. If each allocator is placed in a   //!different shared memory segments, the result is undefined.   friend void swap(self_t &alloc1,self_t &alloc2)   {  alloc1.m_node_pool.swap(alloc2.m_node_pool);  }   /// @cond   private:   node_pool_t m_node_pool;   /// @endcond};//!Equality test for same type of private_node_allocator_basetemplate<unsigned int V, class T, class S, std::size_t NPC> inlinebool operator==(const private_node_allocator_base<V, T, S, NPC> &alloc1,                 const private_node_allocator_base<V, T, S, NPC> &alloc2){  return &alloc1 == &alloc2; }//!Inequality test for same type of private_node_allocator_basetemplate<unsigned int V, class T, class S, std::size_t NPC> inlinebool operator!=(const private_node_allocator_base<V, T, S, NPC> &alloc1,                 const private_node_allocator_base<V, T, S, NPC> &alloc2){  return &alloc1 != &alloc2; }template < class T         , class SegmentManager         , std::size_t NodesPerBlock = 64         >class private_node_allocator_v1   :  public private_node_allocator_base         < 1         , T         , SegmentManager         , NodesPerBlock         >{   public:   typedef detail::private_node_allocator_base         < 1, T, SegmentManager, NodesPerBlock> base_t;   template<class T2>   struct rebind   {        typedef private_node_allocator_v1<T2, SegmentManager, NodesPerBlock>  other;   };   private_node_allocator_v1(SegmentManager *segment_mngr)       : base_t(segment_mngr)   {}   template<class T2>   private_node_allocator_v1      (const private_node_allocator_v1<T2, SegmentManager, NodesPerBlock> &other)      : base_t(other)   {}};}  //namespace detail {/// @endcond//!An STL node allocator that uses a segment manager as memory //!source. The internal pointer type will of the same type (raw, smart) as//!"typename SegmentManager::void_pointer" type. This allows//!placing the allocator in shared memory, memory mapped-files, etc...//!This allocator has its own node pool. NodesPerBlock is the number of nodes allocated 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?