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_CACHED_ADAPTIVE_POOL_HPP Chris@16: #define BOOST_INTERPROCESS_CACHED_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: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes cached_adaptive_pool 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 < 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 cached_adaptive_pool_v1 Chris@16: : public ipcdetail::cached_allocator_impl Chris@16: < T Chris@16: , ipcdetail::shared_adaptive_node_pool Chris@16: < SegmentManager Chris@16: , sizeof_value::value Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > Chris@16: , 1> Chris@16: { Chris@16: public: Chris@16: typedef ipcdetail::cached_allocator_impl Chris@16: < T Chris@16: , ipcdetail::shared_adaptive_node_pool Chris@16: < SegmentManager Chris@16: , sizeof_value::value Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > Chris@16: , 1> base_t; Chris@16: Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef cached_adaptive_pool_v1 Chris@16: other; Chris@16: }; Chris@16: Chris@16: typedef typename base_t::size_type size_type; Chris@16: Chris@16: cached_adaptive_pool_v1(SegmentManager *segment_mngr, Chris@16: size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES) Chris@16: : base_t(segment_mngr, max_cached_nodes) Chris@16: {} Chris@16: Chris@16: template Chris@16: cached_adaptive_pool_v1 Chris@16: (const cached_adaptive_pool_v1 Chris@16: &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: //! Chris@16: //!This node allocator shares a segregated storage between all instances of Chris@16: //!cached_adaptive_pool with equal sizeof(T) placed in the same Chris@16: //!memory segment. But also caches some nodes privately to Chris@16: //!avoid some synchronization overhead. 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 cached_adaptive_pool Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: : public ipcdetail::cached_allocator_impl Chris@16: < T Chris@16: , ipcdetail::shared_adaptive_node_pool Chris@16: < SegmentManager Chris@16: , sizeof_value::value Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > Chris@16: , 2> Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: { Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: typedef ipcdetail::cached_allocator_impl Chris@16: < T Chris@16: , ipcdetail::shared_adaptive_node_pool Chris@16: < SegmentManager Chris@16: , sizeof_value::value Chris@16: , NodesPerBlock Chris@16: , MaxFreeBlocks Chris@16: , OverheadPercent Chris@16: > Chris@16: , 2> base_t; Chris@16: 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 cached_adaptive_pool Chris@16: other; Chris@16: }; Chris@16: Chris@16: cached_adaptive_pool(SegmentManager *segment_mngr, Chris@16: std::size_t max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES) Chris@16: : base_t(segment_mngr, max_cached_nodes) Chris@16: {} Chris@16: Chris@16: template Chris@16: cached_adaptive_pool Chris@16: (const cached_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 cached_adaptive_pool from Chris@16: //!cached_adaptive_pool Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef cached_adaptive_pool other; Chris@16: }; Chris@16: Chris@16: private: Chris@16: //!Not assignable from Chris@16: //!related cached_adaptive_pool Chris@16: template Chris@16: cached_adaptive_pool& operator= Chris@16: (const cached_adaptive_pool&); Chris@16: Chris@16: //!Not assignable from Chris@16: //!other cached_adaptive_pool Chris@16: cached_adaptive_pool& operator=(const cached_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: cached_adaptive_pool(segment_manager *segment_mngr); Chris@16: Chris@16: //!Copy constructor from other cached_adaptive_pool. Increments the reference Chris@16: //!count of the associated node pool. Never throws Chris@16: cached_adaptive_pool(const cached_adaptive_pool &other); Chris@16: Chris@16: //!Copy constructor from related cached_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: cached_adaptive_pool Chris@16: (const cached_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: ~cached_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: multiallocation_chain allocate_individual(size_type num_elements); 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: //!Sets the new max cached nodes value. This can provoke deallocations Chris@16: //!if "newmax" is less than current cached nodes. Never throws Chris@16: void set_max_cached_nodes(size_type newmax); Chris@16: Chris@16: //!Returns the max cached nodes parameter. Chris@16: //!Never throws Chris@16: size_type get_max_cached_nodes() const; 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 cached_adaptive_pool Chris@16: template inline Chris@16: bool operator==(const cached_adaptive_pool &alloc1, Chris@16: const cached_adaptive_pool &alloc2); Chris@16: Chris@16: //!Inequality test for same type Chris@16: //!of cached_adaptive_pool Chris@16: template inline Chris@16: bool operator!=(const cached_adaptive_pool &alloc1, Chris@16: const cached_adaptive_pool &alloc2); Chris@16: Chris@16: #endif Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //#ifndef BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP Chris@16: