Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2015-2015. 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_POOL_RESOURCE_HPP
0012 #define BOOST_CONTAINER_POOL_RESOURCE_HPP
0013 
0014 #if defined (_MSC_VER)
0015 #  pragma once 
0016 #endif
0017 
0018 #include <boost/container/detail/config_begin.hpp>
0019 #include <boost/container/detail/workaround.hpp>
0020 #include <boost/container/pmr/memory_resource.hpp>
0021 #include <boost/container/detail/block_list.hpp>
0022 #include <boost/container/pmr/pool_options.hpp>
0023 
0024 #include <cstddef>
0025 
0026 namespace boost {
0027 namespace container {
0028 namespace pmr {
0029 
0030 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
0031 
0032 class pool_data_t;
0033 
0034 static const std::size_t pool_options_minimum_max_blocks_per_chunk = 1u;
0035 static const std::size_t pool_options_default_max_blocks_per_chunk = 32u;
0036 static const std::size_t pool_options_minimum_largest_required_pool_block =
0037    memory_resource::max_align > 2*sizeof(void*) ? memory_resource::max_align : 2*sizeof(void*);
0038 static const std::size_t pool_options_default_largest_required_pool_block =
0039    pool_options_minimum_largest_required_pool_block > 4096u
0040       ? pool_options_minimum_largest_required_pool_block : 4096u;
0041 
0042 #endif   //BOOST_CONTAINER_DOXYGEN_INVOKED
0043 
0044 class pool_resource
0045 {
0046    typedef block_list_base<> block_list_base_t;
0047 
0048    pool_options m_options;
0049    memory_resource&   m_upstream;
0050    block_list_base_t  m_oversized_list;
0051    pool_data_t *m_pool_data;
0052    std::size_t  m_pool_count;
0053 
0054    static void priv_limit_option(std::size_t &val, std::size_t min, std::size_t max);
0055    static std::size_t priv_pool_index(std::size_t block_size);
0056    static std::size_t priv_pool_block(std::size_t index);
0057 
0058    void priv_fix_options();
0059    void priv_init_pools();
0060    void priv_constructor_body();
0061 
0062    public:
0063 
0064    //! <b>Requires</b>: `upstream` is the address of a valid memory resource.
0065    //!
0066    //! <b>Effects</b>: Constructs a pool resource object that will obtain memory
0067    //!   from upstream whenever the pool resource is unable to satisfy a memory
0068    //!   request from its own internal data structures. The resulting object will hold
0069    //!   a copy of upstream, but will not own the resource to which upstream points.
0070    //!   [ Note: The intention is that calls to upstream->allocate() will be
0071    //!   substantially fewer than calls to this->allocate() in most cases. - end note 
0072    //!   The behavior of the pooling mechanism is tuned according to the value of
0073    //!   the opts argument.
0074    //!
0075    //! <b>Throws</b>: Nothing unless upstream->allocate() throws. It is unspecified if
0076    //!   or under what conditions this constructor calls upstream->allocate().
0077    pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT;
0078 
0079    //! <b>Effects</b>: Same as
0080    //!   `pool_resource(pool_options(), get_default_resource())`.
0081    pool_resource() BOOST_NOEXCEPT;
0082 
0083    //! <b>Effects</b>: Same as
0084    //!   `pool_resource(pool_options(), upstream)`.
0085    explicit pool_resource(memory_resource* upstream) BOOST_NOEXCEPT;
0086 
0087    //! <b>Effects</b>: Same as
0088    //!   `pool_resource(opts, get_default_resource())`.
0089    explicit pool_resource(const pool_options& opts) BOOST_NOEXCEPT;
0090 
0091    #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
0092    pool_resource(const pool_resource&) = delete;
0093    pool_resource operator=(const pool_resource&) = delete;
0094    #else
0095    private:
0096    pool_resource          (const pool_resource&);
0097    pool_resource operator=(const pool_resource&);
0098    public:
0099    #endif
0100 
0101    //! <b>Effects</b>: Calls
0102    //!   `this->release()`.
0103    ~pool_resource();
0104 
0105    //! <b>Effects</b>: Calls Calls `upstream_resource()->deallocate()` as necessary
0106    //!   to release all allocated memory. [ Note: memory is released back to
0107    //!   `upstream_resource()` even if deallocate has not been called for some
0108    //!   of the allocated blocks. - end note ]
0109    void release();
0110 
0111    //! <b>Returns</b>: The value of the upstream argument provided to the
0112    //!   constructor of this object.
0113    memory_resource* upstream_resource() const;
0114 
0115    //! <b>Returns</b>: The options that control the pooling behavior of this resource.
0116    //!   The values in the returned struct may differ from those supplied to the pool
0117    //!   resource constructor in that values of zero will be replaced with
0118    //!   implementation-defined defaults and sizes may be rounded to unspecified granularity.
0119    pool_options options() const;
0120 
0121    public:  //public so that [un]synchronized_pool_resource can use them
0122 
0123    //! <b>Returns</b>: A pointer to allocated storage with a size of at least `bytes`.
0124    //!   The size and alignment of the allocated memory shall meet the requirements for
0125    //!   a class derived from `memory_resource`.
0126    //!
0127    //! <b>Effects</b>: If the pool selected for a block of size bytes is unable to
0128    //!   satisfy the memory request from its own internal data structures, it will call
0129    //!   `upstream_resource()->allocate()` to obtain more memory. If `bytes` is larger
0130    //!   than that which the largest pool can handle, then memory will be allocated
0131    //!   using `upstream_resource()->allocate()`.
0132    //!
0133    //! <b>Throws</b>: Nothing unless `upstream_resource()->allocate()` throws.
0134    void* do_allocate(std::size_t bytes, std::size_t alignment);
0135 
0136    //! <b>Effects</b>: Return the memory at p to the pool. It is unspecified if or under
0137    //!   what circumstances this operation will result in a call to
0138    //!   `upstream_resource()->deallocate()`.
0139    //!
0140    //! <b>Throws</b>: Nothing.
0141    void do_deallocate(void* p, std::size_t bytes, std::size_t alignment);
0142 
0143    //Non-standard observers
0144    public:
0145    //! <b>Returns</b>: The number of pools that will be used in the pool resource.
0146    //!
0147    //! <b>Note</b>: Non-standard extension.
0148    std::size_t pool_count() const;
0149 
0150    //! <b>Returns</b>: The index of the pool that will be used to serve the allocation of `bytes`.
0151    //!   from the pool specified by `pool_index`. Returns `pool_count()` if `bytes` is bigger
0152    //!   than `options().largest_required_pool_block` (no pool will be used to serve this).
0153    //!
0154    //! <b>Note</b>: Non-standard extension.
0155    std::size_t pool_index(std::size_t bytes) const;
0156 
0157    //! <b>Requires</b>: `pool_idx < pool_index()`
0158    //!
0159    //! <b>Returns</b>: The number blocks that will be allocated in the next chunk
0160    //!   from the pool specified by `pool_idx`.
0161    //!
0162    //! <b>Note</b>: Non-standard extension.
0163    std::size_t pool_next_blocks_per_chunk(std::size_t pool_idx) const;
0164 
0165    //! <b>Requires</b>: `pool_idx < pool_index()`
0166    //!
0167    //! <b>Returns</b>: The number of bytes of the block that the specified `pool_idx` pool manages.
0168    //!
0169    //! <b>Note</b>: Non-standard extension.
0170    std::size_t pool_block(std::size_t pool_idx) const;
0171 
0172    //! <b>Requires</b>: `pool_idx < pool_index()`
0173    //!
0174    //! <b>Returns</b>: The number of blocks that the specified `pool_idx` pool has cached
0175    //!   and will be served without calling the upstream_allocator.
0176    //!
0177    //! <b>Note</b>: Non-standard extension.
0178    std::size_t pool_cached_blocks(std::size_t pool_idx) const;
0179 };
0180 
0181 }  //namespace pmr {
0182 }  //namespace container {
0183 }  //namespace boost {
0184 
0185 #include <boost/container/detail/config_end.hpp>
0186 
0187 #endif   //BOOST_CONTAINER_POOL_RESOURCE_HPP