Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_PRIVATE_ADAPTIVE_POOL_HPP Chris@16: #define BOOST_INTERPROCESS_PRIVATE_ADAPTIVE_POOL_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes private_adaptive_pool_base pooled shared memory STL compatible allocator Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: namespace ipcdetail { Chris@16: Chris@16: template < unsigned int Version Chris@16: , class T Chris@16: , class SegmentManager Chris@16: , std::size_t NodesPerBlock Chris@16: , std::size_t MaxFreeBlocks Chris@16: , unsigned char OverheadPercent Chris@16: > Chris@16: class private_adaptive_pool_base Chris@16: : public node_pool_allocation_impl Chris@16: < private_adaptive_pool_base < Version, T, SegmentManager, NodesPerBlock Chris@16: , MaxFreeBlocks, OverheadPercent> Chris@16: , Version Chris@16: , T Chris@16: , SegmentManager Chris@16: > Chris@16: { Chris@16: public: Chris@16: //Segment manager Chris@16: typedef SegmentManager segment_manager; Chris@16: typedef typename SegmentManager::void_pointer void_pointer; Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: typedef private_adaptive_pool_base Chris@16: < Version, T, SegmentManager, NodesPerBlock Chris@16: , MaxFreeBlocks, OverheadPercent> self_t; Chris@16: typedef ipcdetail::private_adaptive_node_pool Chris@16: ::value Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > node_pool_t; Chris@16: Chris@16: BOOST_STATIC_ASSERT((Version <=2)); Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: typedef typename boost::intrusive:: Chris@16: pointer_traits::template Chris@16: rebind_pointer::type pointer; Chris@16: typedef typename boost::intrusive:: Chris@16: pointer_traits::template Chris@16: rebind_pointer::type const_pointer; Chris@16: typedef T value_type; Chris@16: typedef typename ipcdetail::add_reference Chris@16: ::type reference; Chris@16: typedef typename ipcdetail::add_reference Chris@16: ::type const_reference; Chris@16: typedef typename segment_manager::size_type size_type; Chris@16: typedef typename segment_manager::size_type difference_type; Chris@16: typedef boost::interprocess::version_type Chris@16: version; Chris@16: typedef boost::container::container_detail::transform_multiallocation_chain Chris@16: multiallocation_chain; Chris@16: Chris@16: //!Obtains node_allocator from other node_allocator Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef private_adaptive_pool_base Chris@16: other; Chris@16: }; Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: template Chris@16: struct node_pool Chris@16: { Chris@16: typedef ipcdetail::private_adaptive_node_pool Chris@16: ::value Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > type; Chris@16: Chris@16: static type *get(void *p) Chris@16: { return static_cast(p); } Chris@16: }; Chris@16: Chris@16: private: Chris@16: //!Not assignable from related private_adaptive_pool_base Chris@16: template Chris@16: private_adaptive_pool_base& operator= Chris@16: (const private_adaptive_pool_base&); Chris@16: Chris@16: //!Not assignable from other private_adaptive_pool_base Chris@16: private_adaptive_pool_base& operator=(const private_adaptive_pool_base&); Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: //!Constructor from a segment manager Chris@16: private_adaptive_pool_base(segment_manager *segment_mngr) Chris@16: : m_node_pool(segment_mngr) Chris@16: {} Chris@16: Chris@16: //!Copy constructor from other private_adaptive_pool_base. Never throws Chris@16: private_adaptive_pool_base(const private_adaptive_pool_base &other) Chris@16: : m_node_pool(other.get_segment_manager()) Chris@16: {} Chris@16: Chris@16: //!Copy constructor from related private_adaptive_pool_base. Never throws. Chris@16: template Chris@16: private_adaptive_pool_base Chris@16: (const private_adaptive_pool_base Chris@16: &other) Chris@16: : m_node_pool(other.get_segment_manager()) Chris@16: {} Chris@16: Chris@16: //!Destructor, frees all used memory. Never throws Chris@16: ~private_adaptive_pool_base() Chris@16: {} Chris@16: Chris@16: //!Returns the segment manager. Never throws Chris@16: segment_manager* get_segment_manager()const Chris@16: { return m_node_pool.get_segment_manager(); } Chris@16: Chris@16: //!Returns the internal node pool. Never throws Chris@16: node_pool_t* get_node_pool() const Chris@16: { return const_cast(&m_node_pool); } Chris@16: Chris@16: //!Swaps allocators. Does not throw. If each allocator is placed in a Chris@16: //!different shared memory segments, the result is undefined. Chris@16: friend void swap(self_t &alloc1,self_t &alloc2) Chris@101: { boost::adl_move_swap(alloc1.m_node_pool, alloc2.m_node_pool); } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: node_pool_t m_node_pool; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: //!Equality test for same type of private_adaptive_pool_base Chris@16: template inline Chris@16: bool operator==(const private_adaptive_pool_base &alloc1, Chris@16: const private_adaptive_pool_base &alloc2) Chris@16: { return &alloc1 == &alloc2; } Chris@16: Chris@16: //!Inequality test for same type of private_adaptive_pool_base Chris@16: template inline Chris@16: bool operator!=(const private_adaptive_pool_base &alloc1, Chris@16: const private_adaptive_pool_base &alloc2) Chris@16: { return &alloc1 != &alloc2; } Chris@16: Chris@16: template < class T Chris@16: , class SegmentManager Chris@16: , std::size_t NodesPerBlock = 64 Chris@16: , std::size_t MaxFreeBlocks = 2 Chris@16: , unsigned char OverheadPercent = 5 Chris@16: > Chris@16: class private_adaptive_pool_v1 Chris@16: : public private_adaptive_pool_base Chris@16: < 1 Chris@16: , T Chris@16: , SegmentManager Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > Chris@16: { Chris@16: public: Chris@16: typedef ipcdetail::private_adaptive_pool_base Chris@16: < 1, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t; Chris@16: Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef private_adaptive_pool_v1 other; Chris@16: }; Chris@16: Chris@16: private_adaptive_pool_v1(SegmentManager *segment_mngr) Chris@16: : base_t(segment_mngr) Chris@16: {} Chris@16: Chris@16: template Chris@16: private_adaptive_pool_v1 Chris@16: (const private_adaptive_pool_v1 &other) Chris@16: : base_t(other) Chris@16: {} Chris@16: }; Chris@16: Chris@16: } //namespace ipcdetail { Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!An STL node allocator that uses a segment manager as memory Chris@16: //!source. The internal pointer type will of the same type (raw, smart) as Chris@16: //!"typename SegmentManager::void_pointer" type. This allows Chris@16: //!placing the allocator in shared memory, memory mapped-files, etc... Chris@16: //!This allocator has its own node pool. Chris@16: //! Chris@16: //!NodesPerBlock is the minimum number of nodes of nodes allocated at once when Chris@16: //!the allocator needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks Chris@16: //!that the adaptive node pool will hold. The rest of the totally free blocks will be Chris@16: //!deallocated with the segment manager. Chris@16: //! Chris@16: //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator: Chris@16: //!(memory usable for nodes / total memory allocated from the segment manager) Chris@16: template < class T Chris@16: , class SegmentManager Chris@16: , std::size_t NodesPerBlock Chris@16: , std::size_t MaxFreeBlocks Chris@16: , unsigned char OverheadPercent Chris@16: > Chris@16: class private_adaptive_pool Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: : public ipcdetail::private_adaptive_pool_base Chris@16: < 2 Chris@16: , T Chris@16: , SegmentManager Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: { Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: typedef ipcdetail::private_adaptive_pool_base Chris@16: < 2, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t; Chris@16: public: Chris@16: typedef boost::interprocess::version_type version; Chris@16: Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef private_adaptive_pool Chris@16: other; Chris@16: }; Chris@16: Chris@16: private_adaptive_pool(SegmentManager *segment_mngr) Chris@16: : base_t(segment_mngr) Chris@16: {} Chris@16: Chris@16: template Chris@16: private_adaptive_pool Chris@16: (const private_adaptive_pool &other) Chris@16: : base_t(other) Chris@16: {} Chris@16: Chris@16: #else Chris@16: public: Chris@16: typedef implementation_defined::segment_manager segment_manager; Chris@16: typedef segment_manager::void_pointer void_pointer; Chris@16: typedef implementation_defined::pointer pointer; Chris@16: typedef implementation_defined::const_pointer const_pointer; Chris@16: typedef T value_type; Chris@16: typedef typename ipcdetail::add_reference Chris@16: ::type reference; Chris@16: typedef typename ipcdetail::add_reference Chris@16: ::type const_reference; Chris@16: typedef typename segment_manager::size_type size_type; Chris@16: typedef typename segment_manager::difference_type difference_type; Chris@16: Chris@16: //!Obtains private_adaptive_pool from Chris@16: //!private_adaptive_pool Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef private_adaptive_pool Chris@16: other; Chris@16: }; Chris@16: Chris@16: private: Chris@16: //!Not assignable from Chris@16: //!related private_adaptive_pool Chris@16: template Chris@16: private_adaptive_pool& operator= Chris@16: (const private_adaptive_pool&); Chris@16: Chris@16: //!Not assignable from Chris@16: //!other private_adaptive_pool Chris@16: private_adaptive_pool& operator=(const private_adaptive_pool&); Chris@16: Chris@16: public: Chris@16: //!Constructor from a segment manager. If not present, constructs a node Chris@16: //!pool. Increments the reference count of the associated node pool. Chris@16: //!Can throw boost::interprocess::bad_alloc Chris@16: private_adaptive_pool(segment_manager *segment_mngr); Chris@16: Chris@16: //!Copy constructor from other private_adaptive_pool. Increments the reference Chris@16: //!count of the associated node pool. Never throws Chris@16: private_adaptive_pool(const private_adaptive_pool &other); Chris@16: Chris@16: //!Copy constructor from related private_adaptive_pool. If not present, constructs Chris@16: //!a node pool. Increments the reference count of the associated node pool. Chris@16: //!Can throw boost::interprocess::bad_alloc Chris@16: template Chris@16: private_adaptive_pool Chris@16: (const private_adaptive_pool &other); Chris@16: Chris@16: //!Destructor, removes node_pool_t from memory Chris@16: //!if its reference count reaches to zero. Never throws Chris@16: ~private_adaptive_pool(); Chris@16: Chris@16: //!Returns a pointer to the node pool. Chris@16: //!Never throws Chris@16: node_pool_t* get_node_pool() const; Chris@16: Chris@16: //!Returns the segment manager. Chris@16: //!Never throws Chris@16: segment_manager* get_segment_manager()const; Chris@16: Chris@16: //!Returns the number of elements that could be allocated. Chris@16: //!Never throws Chris@16: size_type max_size() const; Chris@16: Chris@16: //!Allocate memory for an array of count elements. Chris@16: //!Throws boost::interprocess::bad_alloc if there is no enough memory Chris@16: pointer allocate(size_type count, cvoid_pointer hint = 0); Chris@16: Chris@16: //!Deallocate allocated memory. Chris@16: //!Never throws Chris@16: void deallocate(const pointer &ptr, size_type count); Chris@16: Chris@16: //!Deallocates all free blocks Chris@16: //!of the pool Chris@16: void deallocate_free_blocks(); Chris@16: Chris@16: //!Swaps allocators. Does not throw. If each allocator is placed in a Chris@16: //!different memory segment, the result is undefined. Chris@16: friend void swap(self_t &alloc1, self_t &alloc2); Chris@16: Chris@16: //!Returns address of mutable object. Chris@16: //!Never throws Chris@16: pointer address(reference value) const; Chris@16: Chris@16: //!Returns address of non mutable object. Chris@16: //!Never throws Chris@16: const_pointer address(const_reference value) const; Chris@16: Chris@16: //!Copy construct an object. Chris@16: //!Throws if T's copy constructor throws Chris@16: void construct(const pointer &ptr, const_reference v); Chris@16: Chris@16: //!Destroys object. Throws if object's Chris@16: //!destructor throws Chris@16: void destroy(const pointer &ptr); Chris@16: Chris@16: //!Returns maximum the number of objects the previously allocated memory Chris@16: //!pointed by p can hold. This size only works for memory allocated with Chris@16: //!allocate, allocation_command and allocate_many. Chris@16: size_type size(const pointer &p) const; Chris@16: Chris@101: pointer allocation_command(boost::interprocess::allocation_type command, Chris@101: size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse); Chris@16: Chris@16: //!Allocates many elements of size elem_size in a contiguous block Chris@16: //!of memory. The minimum number to be allocated is min_elements, Chris@16: //!the preferred and maximum number is Chris@16: //!preferred_elements. The number of actually allocated elements is Chris@16: //!will be assigned to received_size. The elements must be deallocated Chris@16: //!with deallocate(...) Chris@16: void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain); Chris@16: Chris@16: //!Allocates n_elements elements, each one of size elem_sizes[i]in a Chris@16: //!contiguous block Chris@16: //!of memory. The elements must be deallocated Chris@16: void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain); Chris@16: Chris@16: //!Allocates many elements of size elem_size in a contiguous block Chris@16: //!of memory. The minimum number to be allocated is min_elements, Chris@16: //!the preferred and maximum number is Chris@16: //!preferred_elements. The number of actually allocated elements is Chris@16: //!will be assigned to received_size. The elements must be deallocated Chris@16: //!with deallocate(...) Chris@16: void deallocate_many(multiallocation_chain &chain); Chris@16: Chris@16: //!Allocates just one object. Memory allocated with this function Chris@16: //!must be deallocated only with deallocate_one(). Chris@16: //!Throws boost::interprocess::bad_alloc if there is no enough memory Chris@16: pointer allocate_one(); Chris@16: Chris@16: //!Allocates many elements of size == 1 in a contiguous block Chris@16: //!of memory. The minimum number to be allocated is min_elements, Chris@16: //!the preferred and maximum number is Chris@16: //!preferred_elements. The number of actually allocated elements is Chris@16: //!will be assigned to received_size. Memory allocated with this function Chris@16: //!must be deallocated only with deallocate_one(). Chris@16: void allocate_individual(size_type num_elements, multiallocation_chain &chain); Chris@16: Chris@16: //!Deallocates memory previously allocated with allocate_one(). Chris@16: //!You should never use deallocate_one to deallocate memory allocated Chris@16: //!with other functions different from allocate_one(). Never throws Chris@16: void deallocate_one(const pointer &p); Chris@16: Chris@16: //!Allocates many elements of size == 1 in a contiguous block Chris@16: //!of memory. The minimum number to be allocated is min_elements, Chris@16: //!the preferred and maximum number is Chris@16: //!preferred_elements. The number of actually allocated elements is Chris@16: //!will be assigned to received_size. Memory allocated with this function Chris@16: //!must be deallocated only with deallocate_one(). Chris@16: void deallocate_individual(multiallocation_chain &chain); Chris@16: #endif Chris@16: }; Chris@16: Chris@16: #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!Equality test for same type Chris@16: //!of private_adaptive_pool Chris@16: template inline Chris@16: bool operator==(const private_adaptive_pool &alloc1, Chris@16: const private_adaptive_pool &alloc2); Chris@16: Chris@16: //!Inequality test for same type Chris@16: //!of private_adaptive_pool Chris@16: template inline Chris@16: bool operator!=(const private_adaptive_pool &alloc1, Chris@16: const private_adaptive_pool &alloc2); Chris@16: Chris@16: #endif Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //#ifndef BOOST_INTERPROCESS_PRIVATE_ADAPTIVE_POOL_HPP Chris@16: