Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:09:04

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. 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/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP
0012 #define BOOST_INTERPROCESS_CACHED_ADAPTIVE_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/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 
0025 #include <boost/interprocess/interprocess_fwd.hpp>
0026 #include <boost/interprocess/allocators/detail/adaptive_node_pool.hpp>
0027 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
0028 #include <boost/interprocess/detail/utilities.hpp>
0029 #include <boost/interprocess/detail/workaround.hpp>
0030 #include <boost/interprocess/containers/version_type.hpp>
0031 #include <boost/interprocess/allocators/detail/node_tools.hpp>
0032 #include <cstddef>
0033 
0034 //!\file
0035 //!Describes cached_adaptive_pool pooled shared memory STL compatible allocator
0036 
0037 namespace boost {
0038 namespace interprocess {
0039 
0040 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0041 
0042 namespace ipcdetail {
0043 
0044 template < class T
0045          , class SegmentManager
0046          , std::size_t NodesPerBlock = 64
0047          , std::size_t MaxFreeBlocks = 2
0048          , unsigned char OverheadPercent = 5
0049          >
0050 class cached_adaptive_pool_v1
0051    :  public ipcdetail::cached_allocator_impl
0052          < T
0053          , ipcdetail::shared_adaptive_node_pool
0054             < SegmentManager
0055             , sizeof_value<T>::value
0056             , NodesPerBlock
0057             , MaxFreeBlocks
0058             , OverheadPercent
0059             >
0060          , 1>
0061 {
0062    public:
0063    typedef ipcdetail::cached_allocator_impl
0064          < T
0065          , ipcdetail::shared_adaptive_node_pool
0066             < SegmentManager
0067             , sizeof_value<T>::value
0068             , NodesPerBlock
0069             , MaxFreeBlocks
0070             , OverheadPercent
0071             >
0072          , 1> base_t;
0073 
0074    template<class T2>
0075    struct rebind
0076    {
0077       typedef cached_adaptive_pool_v1
0078          <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>  other;
0079    };
0080 
0081    typedef typename base_t::size_type size_type;
0082 
0083    cached_adaptive_pool_v1(SegmentManager *segment_mngr,
0084                            size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
0085       : base_t(segment_mngr, max_cached_nodes)
0086    {}
0087 
0088    template<class T2>
0089    cached_adaptive_pool_v1
0090       (const cached_adaptive_pool_v1
0091          <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
0092       : base_t(other)
0093    {}
0094 };
0095 
0096 }  //namespace ipcdetail{
0097 
0098 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0099 
0100 //!An STL node allocator that uses a segment manager as memory
0101 //!source. The internal pointer type will of the same type (raw, smart) as
0102 //!"typename SegmentManager::void_pointer" type. This allows
0103 //!placing the allocator in shared memory, memory mapped-files, etc...
0104 //!
0105 //!This node allocator shares a segregated storage between all instances of
0106 //!cached_adaptive_pool with equal sizeof(T) placed in the same
0107 //!memory segment. But also caches some nodes privately to
0108 //!avoid some synchronization overhead.
0109 //!
0110 //!NodesPerBlock is the minimum number of nodes of nodes allocated at once when
0111 //!the allocator needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
0112 //!that the adaptive node pool will hold. The rest of the totally free blocks will be
0113 //!deallocated with the segment manager.
0114 //!
0115 //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
0116 //!(memory usable for nodes / total memory allocated from the segment manager)
0117 template < class T
0118          , class SegmentManager
0119          , std::size_t NodesPerBlock
0120          , std::size_t MaxFreeBlocks
0121          , unsigned char OverheadPercent
0122          >
0123 class cached_adaptive_pool
0124    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0125    :  public ipcdetail::cached_allocator_impl
0126          < T
0127          , ipcdetail::shared_adaptive_node_pool
0128             < SegmentManager
0129             , sizeof_value<T>::value
0130             , NodesPerBlock
0131             , MaxFreeBlocks
0132             , OverheadPercent
0133             >
0134          , 2>
0135    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0136 {
0137 
0138    #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0139    public:
0140    typedef ipcdetail::cached_allocator_impl
0141          < T
0142          , ipcdetail::shared_adaptive_node_pool
0143             < SegmentManager
0144             , sizeof_value<T>::value
0145             , NodesPerBlock
0146             , MaxFreeBlocks
0147             , OverheadPercent
0148             >
0149          , 2> base_t;
0150 
0151    public:
0152    typedef boost::interprocess::version_type<cached_adaptive_pool, 2>   version;
0153 
0154    template<class T2>
0155    struct rebind
0156    {
0157       typedef cached_adaptive_pool
0158          <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent>  other;
0159    };
0160 
0161    cached_adaptive_pool(SegmentManager *segment_mngr,
0162                          std::size_t max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
0163       : base_t(segment_mngr, max_cached_nodes)
0164    {}
0165 
0166    template<class T2>
0167    cached_adaptive_pool
0168       (const cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
0169       : base_t(other)
0170    {}
0171 
0172    #else
0173    public:
0174    typedef implementation_defined::segment_manager       segment_manager;
0175    typedef segment_manager::void_pointer                 void_pointer;
0176    typedef implementation_defined::pointer               pointer;
0177    typedef implementation_defined::const_pointer         const_pointer;
0178    typedef T                                             value_type;
0179    typedef typename ipcdetail::add_reference
0180                      <value_type>::type                  reference;
0181    typedef typename ipcdetail::add_reference
0182                      <const value_type>::type            const_reference;
0183    typedef typename segment_manager::size_type           size_type;
0184    typedef typename segment_manager::difference_type     difference_type;
0185 
0186    //!Obtains cached_adaptive_pool from
0187    //!cached_adaptive_pool
0188    template<class T2>
0189    struct rebind
0190    {
0191       typedef cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
0192    };
0193 
0194    private:
0195    //!Not assignable from
0196    //!related cached_adaptive_pool
0197    template<class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char OP2>
0198    cached_adaptive_pool& operator=
0199       (const cached_adaptive_pool<T2, SegmentManager2, N2, F2, OP2>&);
0200 
0201    //!Not assignable from
0202    //!other cached_adaptive_pool
0203    cached_adaptive_pool& operator=(const cached_adaptive_pool&);
0204 
0205    public:
0206    //!Constructor from a segment manager. If not present, constructs a node
0207    //!pool. Increments the reference count of the associated node pool.
0208    //!Can throw boost::interprocess::bad_alloc
0209    cached_adaptive_pool(segment_manager *segment_mngr);
0210 
0211    //!Copy constructor from other cached_adaptive_pool. Increments the reference
0212    //!count of the associated node pool. Never throws
0213    cached_adaptive_pool(const cached_adaptive_pool &other);
0214 
0215    //!Copy constructor from related cached_adaptive_pool. If not present, constructs
0216    //!a node pool. Increments the reference count of the associated node pool.
0217    //!Can throw boost::interprocess::bad_alloc
0218    template<class T2>
0219    cached_adaptive_pool
0220       (const cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other);
0221 
0222    //!Destructor, removes node_pool_t from memory
0223    //!if its reference count reaches to zero. Never throws
0224    ~cached_adaptive_pool();
0225 
0226    //!Returns a pointer to the node pool.
0227    //!Never throws
0228    node_pool_t* get_node_pool() const;
0229 
0230    //!Returns the segment manager.
0231    //!Never throws
0232    segment_manager* get_segment_manager()const;
0233 
0234    //!Returns the number of elements that could be allocated.
0235    //!Never throws
0236    size_type max_size() const;
0237 
0238    //!Allocate memory for an array of count elements.
0239    //!Throws boost::interprocess::bad_alloc if there is no enough memory
0240    pointer allocate(size_type count, cvoid_pointer hint = 0);
0241 
0242    //!Deallocate allocated memory.
0243    //!Never throws
0244    void deallocate(const pointer &ptr, size_type count);
0245 
0246    //!Deallocates all free blocks
0247    //!of the pool
0248    void deallocate_free_blocks();
0249 
0250    //!Swaps allocators. Does not throw. If each allocator is placed in a
0251    //!different memory segment, the result is undefined.
0252    friend void swap(self_t &alloc1, self_t &alloc2);
0253 
0254    //!Returns address of mutable object.
0255    //!Never throws
0256    pointer address(reference value) const;
0257 
0258    //!Returns address of non mutable object.
0259    //!Never throws
0260    const_pointer address(const_reference value) const;
0261 
0262    //!Copy construct an object.
0263    //!Throws if T's copy constructor throws
0264    void construct(const pointer &ptr, const_reference v);
0265 
0266    //!Destroys object. Throws if object's
0267    //!destructor throws
0268    void destroy(const pointer &ptr);
0269 
0270    //!Returns maximum the number of objects the previously allocated memory
0271    //!pointed by p can hold. This size only works for memory allocated with
0272    //!allocate, allocation_command and allocate_many.
0273    size_type size(const pointer &p) const;
0274 
0275    pointer allocation_command(boost::interprocess::allocation_type command,
0276                          size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
0277 
0278    //!Allocates many elements of size elem_size in a contiguous block
0279    //!of memory. The minimum number to be allocated is min_elements,
0280    //!the preferred and maximum number is
0281    //!preferred_elements. The number of actually allocated elements is
0282    //!will be assigned to received_size. The elements must be deallocated
0283    //!with deallocate(...)
0284    void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
0285 
0286    //!Allocates n_elements elements, each one of size elem_sizes[i]in a
0287    //!contiguous block
0288    //!of memory. The elements must be deallocated
0289    void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
0290 
0291    //!Allocates many elements of size elem_size in a contiguous block
0292    //!of memory. The minimum number to be allocated is min_elements,
0293    //!the preferred and maximum number is
0294    //!preferred_elements. The number of actually allocated elements is
0295    //!will be assigned to received_size. The elements must be deallocated
0296    //!with deallocate(...)
0297    void deallocate_many(multiallocation_chain &chain);
0298 
0299    //!Allocates just one object. Memory allocated with this function
0300    //!must be deallocated only with deallocate_one().
0301    //!Throws boost::interprocess::bad_alloc if there is no enough memory
0302    pointer allocate_one();
0303 
0304    //!Allocates many elements of size == 1 in a contiguous block
0305    //!of memory. The minimum number to be allocated is min_elements,
0306    //!the preferred and maximum number is
0307    //!preferred_elements. The number of actually allocated elements is
0308    //!will be assigned to received_size. Memory allocated with this function
0309    //!must be deallocated only with deallocate_one().
0310    multiallocation_chain allocate_individual(size_type num_elements);
0311 
0312    //!Deallocates memory previously allocated with allocate_one().
0313    //!You should never use deallocate_one to deallocate memory allocated
0314    //!with other functions different from allocate_one(). Never throws
0315    void deallocate_one(const pointer &p);
0316 
0317    //!Allocates many elements of size == 1 in a contiguous block
0318    //!of memory. The minimum number to be allocated is min_elements,
0319    //!the preferred and maximum number is
0320    //!preferred_elements. The number of actually allocated elements is
0321    //!will be assigned to received_size. Memory allocated with this function
0322    //!must be deallocated only with deallocate_one().
0323    void deallocate_individual(multiallocation_chain &chain);
0324    //!Sets the new max cached nodes value. This can provoke deallocations
0325    //!if "newmax" is less than current cached nodes. Never throws
0326    void set_max_cached_nodes(size_type newmax);
0327 
0328    //!Returns the max cached nodes parameter.
0329    //!Never throws
0330    size_type get_max_cached_nodes() const;
0331    #endif
0332 };
0333 
0334 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0335 
0336 //!Equality test for same type
0337 //!of cached_adaptive_pool
0338 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, std::size_t OP> inline
0339 bool operator==(const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
0340                 const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
0341 
0342 //!Inequality test for same type
0343 //!of cached_adaptive_pool
0344 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, std::size_t OP> inline
0345 bool operator!=(const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
0346                 const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
0347 
0348 #endif
0349 
0350 }  //namespace interprocess {
0351 }  //namespace boost {
0352 
0353 
0354 #include <boost/interprocess/detail/config_end.hpp>
0355 
0356 #endif   //#ifndef BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP
0357