Chris@102: ////////////////////////////////////////////////////////////////////////////// Chris@102: // Chris@102: // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost Chris@102: // Software License, Version 1.0. (See accompanying file Chris@102: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: // See http://www.boost.org/libs/container for documentation. Chris@102: // Chris@102: ////////////////////////////////////////////////////////////////////////////// Chris@102: Chris@102: #ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP Chris@102: #define BOOST_CONTAINER_DETAIL_NODE_POOL_HPP Chris@102: Chris@102: #ifndef BOOST_CONFIG_HPP Chris@102: # include Chris@102: #endif Chris@102: Chris@102: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: #include Chris@102: #include Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost { Chris@102: namespace container { Chris@102: namespace container_detail { Chris@102: Chris@102: //!Pooled memory allocator using single segregated storage. Includes Chris@102: //!a reference count but the class does not delete itself, this is Chris@102: //!responsibility of user classes. Node size (NodeSize) and the number of Chris@102: //!nodes allocated per block (NodesPerBlock) are known at compile time Chris@102: template< std::size_t NodeSize, std::size_t NodesPerBlock > Chris@102: class private_node_pool Chris@102: //Inherit from the implementation to avoid template bloat Chris@102: : public boost::container::container_detail:: Chris@102: private_node_pool_impl Chris@102: { Chris@102: typedef boost::container::container_detail:: Chris@102: private_node_pool_impl base_t; Chris@102: //Non-copyable Chris@102: private_node_pool(const private_node_pool &); Chris@102: private_node_pool &operator=(const private_node_pool &); Chris@102: Chris@102: public: Chris@102: typedef typename base_t::multiallocation_chain multiallocation_chain; Chris@102: static const std::size_t nodes_per_block = NodesPerBlock; Chris@102: Chris@102: //!Constructor from a segment manager. Never throws Chris@102: private_node_pool() Chris@102: : base_t(0, NodeSize, NodesPerBlock) Chris@102: {} Chris@102: Chris@102: }; Chris@102: Chris@102: template< std::size_t NodeSize Chris@102: , std::size_t NodesPerBlock Chris@102: > Chris@102: class shared_node_pool Chris@102: : public private_node_pool Chris@102: { Chris@102: private: Chris@102: typedef private_node_pool private_node_allocator_t; Chris@102: Chris@102: public: Chris@102: typedef typename private_node_allocator_t::free_nodes_t free_nodes_t; Chris@102: typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain; Chris@102: Chris@102: //!Constructor from a segment manager. Never throws Chris@102: shared_node_pool() Chris@102: : private_node_allocator_t(){} Chris@102: Chris@102: //!Destructor. Deallocates all allocated blocks. Never throws Chris@102: ~shared_node_pool() Chris@102: {} Chris@102: Chris@102: //!Allocates array of count elements. Can throw std::bad_alloc Chris@102: void *allocate_node() Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: return private_node_allocator_t::allocate_node(); Chris@102: } Chris@102: Chris@102: //!Deallocates an array pointed by ptr. Never throws Chris@102: void deallocate_node(void *ptr) Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: private_node_allocator_t::deallocate_node(ptr); Chris@102: } Chris@102: Chris@102: //!Allocates a singly linked list of n nodes ending in null pointer. Chris@102: //!can throw std::bad_alloc Chris@102: void allocate_nodes(const std::size_t n, multiallocation_chain &chain) Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: return private_node_allocator_t::allocate_nodes(n, chain); Chris@102: } Chris@102: Chris@102: void deallocate_nodes(multiallocation_chain &chain) Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: private_node_allocator_t::deallocate_nodes(chain); Chris@102: } Chris@102: Chris@102: //!Deallocates all the free blocks of memory. Never throws Chris@102: void deallocate_free_blocks() Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: private_node_allocator_t::deallocate_free_blocks(); Chris@102: } Chris@102: Chris@102: //!Deallocates all blocks. Never throws Chris@102: void purge_blocks() Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: private_node_allocator_t::purge_blocks(); Chris@102: } Chris@102: Chris@102: std::size_t num_free_nodes() Chris@102: { Chris@102: //----------------------- Chris@102: scoped_lock guard(mutex_); Chris@102: //----------------------- Chris@102: return private_node_allocator_t::num_free_nodes(); Chris@102: } Chris@102: Chris@102: private: Chris@102: default_mutex mutex_; Chris@102: }; Chris@102: Chris@102: } //namespace container_detail { Chris@102: } //namespace container { Chris@102: } //namespace boost { Chris@102: Chris@102: #include Chris@102: Chris@102: #endif //#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP