Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:12

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/container for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
0012 #define BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/container/detail/config_begin.hpp>
0023 #include <boost/container/detail/workaround.hpp>
0024 
0025 #include <boost/container/detail/mutex.hpp>
0026 #include <boost/container/detail/pool_common_alloc.hpp>
0027 #include <boost/container/detail/node_pool_impl.hpp>
0028 #include <boost/container/detail/mutex.hpp>
0029 #include <boost/move/utility_core.hpp>
0030 #include <cstddef>
0031 #include <cassert>
0032 
0033 namespace boost {
0034 namespace container {
0035 namespace dtl {
0036 
0037 //!Pooled memory allocator using single segregated storage. Includes
0038 //!a reference count but the class does not delete itself, this is
0039 //!responsibility of user classes. Node size (NodeSize) and the number of
0040 //!nodes allocated per block (NodesPerBlock) are known at compile time
0041 template< std::size_t NodeSize, std::size_t NodesPerBlock >
0042 class private_node_pool
0043    //Inherit from the implementation to avoid template bloat
0044    :  public boost::container::dtl::
0045          private_node_pool_impl<fake_segment_manager>
0046 {
0047    typedef boost::container::dtl::
0048       private_node_pool_impl<fake_segment_manager>   base_t;
0049    //Non-copyable
0050    private_node_pool(const private_node_pool &);
0051    private_node_pool &operator=(const private_node_pool &);
0052 
0053    public:
0054    typedef typename base_t::multiallocation_chain multiallocation_chain;
0055    static const std::size_t nodes_per_block = NodesPerBlock;
0056 
0057    //!Constructor from a segment manager. Never throws
0058    private_node_pool()
0059       :  base_t(0, NodeSize, NodesPerBlock)
0060    {}
0061 
0062 };
0063 
0064 template< std::size_t NodeSize
0065         , std::size_t NodesPerBlock
0066         >
0067 class shared_node_pool
0068    : public private_node_pool<NodeSize, NodesPerBlock>
0069 {
0070    private:
0071    typedef private_node_pool<NodeSize, NodesPerBlock> private_node_allocator_t;
0072 
0073    public:
0074    typedef typename private_node_allocator_t::free_nodes_t  free_nodes_t;
0075    typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain;
0076 
0077    //!Constructor from a segment manager. Never throws
0078    shared_node_pool()
0079    : private_node_allocator_t(){}
0080 
0081    //!Destructor. Deallocates all allocated blocks. Never throws
0082    ~shared_node_pool()
0083    {}
0084 
0085    //!Allocates array of count elements. Can throw bad_alloc
0086    void *allocate_node()
0087    {
0088       //-----------------------
0089       scoped_lock<default_mutex> guard(mutex_);
0090       //-----------------------
0091       return private_node_allocator_t::allocate_node();
0092    }
0093 
0094    //!Deallocates an array pointed by ptr. Never throws
0095    void deallocate_node(void *ptr)
0096    {
0097       //-----------------------
0098       scoped_lock<default_mutex> guard(mutex_);
0099       //-----------------------
0100       private_node_allocator_t::deallocate_node(ptr);
0101    }
0102 
0103    //!Allocates a singly linked list of n nodes ending in null pointer.
0104    //!can throw bad_alloc
0105    void allocate_nodes(const std::size_t n, multiallocation_chain &chain)
0106    {
0107       //-----------------------
0108       scoped_lock<default_mutex> guard(mutex_);
0109       //-----------------------
0110       return private_node_allocator_t::allocate_nodes(n, chain);
0111    }
0112 
0113    void deallocate_nodes(multiallocation_chain &chain)
0114    {
0115       //-----------------------
0116       scoped_lock<default_mutex> guard(mutex_);
0117       //-----------------------
0118       private_node_allocator_t::deallocate_nodes(chain);
0119    }
0120 
0121    //!Deallocates all the free blocks of memory. Never throws
0122    void deallocate_free_blocks()
0123    {
0124       //-----------------------
0125       scoped_lock<default_mutex> guard(mutex_);
0126       //-----------------------
0127       private_node_allocator_t::deallocate_free_blocks();
0128    }
0129 
0130    //!Deallocates all blocks. Never throws
0131    void purge_blocks()
0132    {
0133       //-----------------------
0134       scoped_lock<default_mutex> guard(mutex_);
0135       //-----------------------
0136       private_node_allocator_t::purge_blocks();
0137    }
0138 
0139    std::size_t num_free_nodes()
0140    {
0141       //-----------------------
0142       scoped_lock<default_mutex> guard(mutex_);
0143       //-----------------------
0144       return private_node_allocator_t::num_free_nodes();
0145    }
0146 
0147    private:
0148    default_mutex mutex_;
0149 };
0150 
0151 }  //namespace dtl {
0152 }  //namespace container {
0153 }  //namespace boost {
0154 
0155 #include <boost/container/detail/config_end.hpp>
0156 
0157 #endif   //#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP