allocator_common.hpp

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

HPP
818
字号
////////////////////////////////////////////////////////////////////////////////// (C) Copyright Ion Gaztanaga 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_DETAIL_NODE_ALLOCATOR_COMMON_HPP#define BOOST_INTERPROCESS_DETAIL_NODE_ALLOCATOR_COMMON_HPP#include <boost/interprocess/detail/config_begin.hpp>#include <boost/interprocess/detail/workaround.hpp>#include <boost/interprocess/segment_manager.hpp>#include <boost/interprocess/interprocess_fwd.hpp>#include <boost/interprocess/detail/utilities.hpp> //pointer_to_other, get_pointer#include <utility>   //std::pair#include <boost/utility/addressof.hpp> //boost::addressof#include <boost/assert.hpp>   //BOOST_ASSERT#include <boost/interprocess/exceptions.hpp> //bad_alloc#include <boost/interprocess/sync/scoped_lock.hpp> //scoped_lock#include <boost/interprocess/allocators/allocation_type.hpp> //allocation_type#include <algorithm> //std::swapnamespace boost {namespace interprocess {namespace detail {//!Object function that creates the node allocator if it is not created and//!increments reference count if it is already createdtemplate<class NodePool>struct get_or_create_node_pool_func{      //!This connects or constructs the unique instance of node_pool_t   //!Can throw boost::interprocess::bad_alloc   void operator()()   {      //Find or create the node_pool_t      mp_node_pool =    mp_segment_manager->template find_or_construct                        <NodePool>(unique_instance)(mp_segment_manager);      //If valid, increment link count      if(mp_node_pool != 0)         mp_node_pool->inc_ref_count();   }   //!Constructor. Initializes function   //!object parameters   get_or_create_node_pool_func(typename NodePool::segment_manager *mngr)      : mp_segment_manager(mngr){}      NodePool                            *mp_node_pool;   typename NodePool::segment_manager  *mp_segment_manager;};template<class NodePool>inline NodePool *get_or_create_node_pool(typename NodePool::segment_manager *mgnr){   detail::get_or_create_node_pool_func<NodePool> func(mgnr);   mgnr->atomic_func(func);   return func.mp_node_pool;}//!Object function that decrements the reference count. If the count //!reaches to zero destroys the node allocator from memory. //!Never throwstemplate<class NodePool>struct destroy_if_last_link_func{   //!Decrements reference count and destroys the object if there is no    //!more attached allocators. Never throws   void operator()()   {      //If not the last link return      if(mp_node_pool->dec_ref_count() != 0) return;      //Last link, let's destroy the segment_manager      mp_node_pool->get_segment_manager()->template destroy<NodePool>(unique_instance);    }     //!Constructor. Initializes function   //!object parameters   destroy_if_last_link_func(NodePool *pool)       : mp_node_pool(pool)   {}   NodePool                           *mp_node_pool;};//!Destruction function, initializes and executes destruction function //!object. Never throwstemplate<class NodePool>inline void destroy_node_pool_if_last_link(NodePool *pool){   //Get segment manager   typename NodePool::segment_manager *mngr = pool->get_segment_manager();   //Execute destruction functor atomically   destroy_if_last_link_func<NodePool>func(pool);   mngr->atomic_func(func);}template<class NodePool>class cache_impl{   typedef typename NodePool::segment_manager::      void_pointer                                    void_pointer;   typedef typename pointer_to_other      <void_pointer, NodePool>::type                  node_pool_ptr;   typedef typename NodePool::multiallocation_chain   multiallocation_chain;   node_pool_ptr           mp_node_pool;   multiallocation_chain   m_cached_nodes;   std::size_t             m_max_cached_nodes;   public:   typedef typename NodePool::multiallocation_iterator   multiallocation_iterator;   typedef typename NodePool::segment_manager            segment_manager;   cache_impl(segment_manager *segment_mngr, std::size_t max_cached_nodes)      : mp_node_pool(get_or_create_node_pool<NodePool>(segment_mngr))      , m_max_cached_nodes(max_cached_nodes)   {}   cache_impl(const cache_impl &other)      : mp_node_pool(other.get_node_pool())      , m_max_cached_nodes(other.get_max_cached_nodes())   {      mp_node_pool->inc_ref_count();   }   ~cache_impl()   {      this->deallocate_all_cached_nodes();      detail::destroy_node_pool_if_last_link(detail::get_pointer(mp_node_pool));      }   NodePool *get_node_pool() const   {  return detail::get_pointer(mp_node_pool); }   segment_manager *get_segment_manager() const   {  return mp_node_pool->get_segment_manager(); }   std::size_t get_max_cached_nodes() const   {  return m_max_cached_nodes; }   void *cached_allocation()   {      //If don't have any cached node, we have to get a new list of free nodes from the pool      if(m_cached_nodes.empty()){         mp_node_pool->allocate_nodes(m_cached_nodes, m_max_cached_nodes/2);      }      return m_cached_nodes.pop_front();   }   multiallocation_iterator cached_allocation(std::size_t n)   {      multiallocation_chain chain;      std::size_t count = n;      BOOST_TRY{         //If don't have any cached node, we have to get a new list of free nodes from the pool         while(!m_cached_nodes.empty() && count--){            void *ret = m_cached_nodes.pop_front();            chain.push_back(ret);         }         if(chain.size() != n){            mp_node_pool->allocate_nodes(chain, n - chain.size());         }         assert(chain.size() == n);         chain.splice_back(m_cached_nodes);         return multiallocation_iterator(chain.get_it());      }      BOOST_CATCH(...){         this->cached_deallocation(multiallocation_iterator(chain.get_it()));         BOOST_RETHROW      }      BOOST_CATCH_END   }   void cached_deallocation(void *ptr)   {      //Check if cache is full      if(m_cached_nodes.size() >= m_max_cached_nodes){         //This only occurs if this allocator deallocate memory allocated         //with other equal allocator. Since the cache is full, and more          //deallocations are probably coming, we'll make some room in cache         //in a single, efficient multi node deallocation.         this->priv_deallocate_n_nodes(m_cached_nodes.size() - m_max_cached_nodes/2);      }      m_cached_nodes.push_front(ptr);   }   void cached_deallocation(multiallocation_iterator it)   {      multiallocation_iterator itend;      while(it != itend){         void *addr = &*it;         ++it;         m_cached_nodes.push_front(addr);      }      //Check if cache is full      if(m_cached_nodes.size() >= m_max_cached_nodes){         //This only occurs if this allocator deallocate memory allocated         //with other equal allocator. Since the cache is full, and more          //deallocations are probably coming, we'll make some room in cache         //in a single, efficient multi node deallocation.         this->priv_deallocate_n_nodes(m_cached_nodes.size() - m_max_cached_nodes/2);      }   }   //!Sets the new max cached nodes value. This can provoke deallocations   //!if "newmax" is less than current cached nodes. Never throws   void set_max_cached_nodes(std::size_t newmax)   {      m_max_cached_nodes = newmax;      this->priv_deallocate_remaining_nodes();   }   //!Frees all cached nodes.   //!Never throws   void deallocate_all_cached_nodes()   {      if(m_cached_nodes.empty()) return;      mp_node_pool->deallocate_nodes(m_cached_nodes);   }   private:   //!Frees all cached nodes at once.   //!Never throws   void priv_deallocate_remaining_nodes()   {      if(m_cached_nodes.size() > m_max_cached_nodes){         priv_deallocate_n_nodes(m_cached_nodes.size()-m_max_cached_nodes);      }   }   //!Frees n cached nodes at once. Never throws   void priv_deallocate_n_nodes(std::size_t n)   {      //Deallocate all new linked list at once      mp_node_pool->deallocate_nodes(m_cached_nodes, n);   }};template<class Derived, class T, class SegmentManager>class array_allocation_impl{   const Derived *derived() const   {  return static_cast<const Derived*>(this); }   Derived *derived()   {  return static_cast<Derived*>(this); }   typedef typename SegmentManager::void_pointer         void_pointer;   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 transform_iterator      < typename SegmentManager::         multiallocation_iterator      , detail::cast_functor <T> >          multiallocation_iterator;   typedef typename SegmentManager::      multiallocation_chain                 multiallocation_chain;   public:   //!Returns maximum the number of objects the previously allocated memory   //!pointed by p can hold. This size only works for memory allocated with   //!allocate, allocation_command and allocate_many.   size_type size(const pointer &p) const   {        return (size_type)this->derived()->get_segment_manager()->size(detail::get_pointer(p))/sizeof(T);   }   std::pair<pointer, bool>      allocation_command(allocation_type command,                         size_type limit_size,                          size_type preferred_size,                         size_type &received_size, const pointer &reuse = 0)   {      return this->derived()->get_segment_manager()->allocation_command         (command, limit_size, preferred_size, received_size, detail::get_pointer(reuse));   }   //!Allocates many elements of size elem_size in a contiguous block   //!of memory. The minimum number to be allocated is min_elements,   //!the preferred and maximum number is   //!preferred_elements. The number of actually allocated elements is   //!will be assigned to received_size. The elements must be deallocated   //!with deallocate(...)   multiallocation_iterator allocate_many(size_type elem_size, std::size_t num_elements)   {      return multiallocation_iterator         (this->derived()->get_segment_manager()->allocate_many(sizeof(T)*elem_size, num_elements));   }   //!Allocates n_elements elements, each one of size elem_sizes[i]in a   //!contiguous block   //!of memory. The elements must be deallocated   multiallocation_iterator allocate_many(const size_type *elem_sizes, size_type n_elements)   {      return multiallocation_iterator         (this->derived()->get_segment_manager()->allocate_many(elem_sizes, n_elements, sizeof(T)));   }   //!Allocates many elements of size elem_size in a contiguous block   //!of memory. The minimum number to be allocated is min_elements,   //!the preferred and maximum number is   //!preferred_elements. The number of actually allocated elements is   //!will be assigned to received_size. The elements must be deallocated   //!with deallocate(...)   void deallocate_many(multiallocation_iterator it)   {  return this->derived()->get_segment_manager()->deallocate_many(it.base()); }   //!Returns the number of elements that could be   //!allocated. Never throws   size_type max_size() const   {  return this->derived()->get_segment_manager()->get_size()/sizeof(T);  }   //!Returns address of mutable object.   //!Never throws   pointer address(reference value) const   {  return pointer(boost::addressof(value));  }   //!Returns address of non mutable object.   //!Never throws   const_pointer address(const_reference value) const   {  return const_pointer(boost::addressof(value));  }   //!Default construct an object.    //!Throws if T's default constructor throws   void construct(const pointer &ptr)   {  new((void*)detail::get_pointer(ptr)) value_type;  }   //!Copy construct an object   //!Throws if T's copy constructor throws   void construct(const pointer &ptr, const_reference v)   {  new((void*)detail::get_pointer(ptr)) value_type(v);  }   //!Destroys object. Throws if object's   //!destructor throws   void destroy(const pointer &ptr)   {  BOOST_ASSERT(ptr != 0); (*ptr).~value_type();  }};template<class Derived, unsigned int Version, class T, class SegmentManager>class node_pool_allocation_impl   :  public array_allocation_impl      < Derived      , T      , SegmentManager>{   const Derived *derived() const   {  return static_cast<const Derived*>(this); }   Derived *derived()   {  return static_cast<Derived*>(this); }   typedef typename SegmentManager::void_pointer         void_pointer;   typedef typename detail::      pointer_to_other<void_pointer, const void>::type   cvoid_pointer;   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 transform_iterator      < typename SegmentManager::         multiallocation_iterator      , detail::cast_functor <T> >          multiallocation_iterator;   typedef typename SegmentManager::      multiallocation_chain                 multiallocation_chain;   template <int Dummy>   struct node_pool   {      typedef typename Derived::template node_pool<0>::type type;      static type *get(void *p)      {  return static_cast<type*>(p); }   };   public:   //!Allocate memory for an array of count elements.    //!Throws boost::interprocess::bad_alloc if there is no enough memory   pointer allocate(size_type count, cvoid_pointer hint = 0)   {      (void)hint;      typedef typename node_pool<0>::type node_pool_t;      node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool());

⌨️ 快捷键说明

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