allocator_common.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 818 行 · 第 1/2 页
HPP
818 行
if(count > this->max_size()) throw bad_alloc(); else if(Version == 1 && count == 1) return pointer(static_cast<value_type*> (pool->allocate_node())); else return pointer(static_cast<value_type*> (pool->get_segment_manager()->allocate(sizeof(T)*count))); } //!Deallocate allocated memory. Never throws void deallocate(const pointer &ptr, size_type count) { (void)count; typedef typename node_pool<0>::type node_pool_t; node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool()); if(Version == 1 && count == 1) pool->deallocate_node(detail::get_pointer(ptr)); else pool->get_segment_manager()->deallocate((void*)detail::get_pointer(ptr)); } //!Allocates just one object. Memory allocated with this function //!must be deallocated only with deallocate_one(). //!Throws boost::interprocess::bad_alloc if there is no enough memory pointer allocate_one() { typedef typename node_pool<0>::type node_pool_t; node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool()); return pointer(static_cast<value_type*>(pool->allocate_node())); } //!Allocates many elements of size == 1 in a contiguous block //!of memory. The minimum number to be allocated is min_elements, //!the preferred and maximum number is //!preferred_elements. The number of actually allocated elements is //!will be assigned to received_size. Memory allocated with this function //!must be deallocated only with deallocate_one(). multiallocation_iterator allocate_individual(std::size_t num_elements) { typedef typename node_pool<0>::type node_pool_t; node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool()); return multiallocation_iterator(pool->allocate_nodes(num_elements)); } //!Deallocates memory previously allocated with allocate_one(). //!You should never use deallocate_one to deallocate memory allocated //!with other functions different from allocate_one(). Never throws void deallocate_one(const pointer &p) { typedef typename node_pool<0>::type node_pool_t; node_pool_t *pool = node_pool<0>::get(this->derived()->get_node_pool()); pool->deallocate_node(detail::get_pointer(p)); } //!Allocates many elements of size == 1 in a contiguous block //!of memory. The minimum number to be allocated is min_elements, //!the preferred and maximum number is //!preferred_elements. The number of actually allocated elements is //!will be assigned to received_size. Memory allocated with this function //!must be deallocated only with deallocate_one(). void deallocate_individual(multiallocation_iterator it) { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_nodes(it.base()); } //!Deallocates all free blocks of the pool void deallocate_free_blocks() { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_free_blocks(); } //!Deprecated, use deallocate_free_blocks. //!Deallocates all free chunks of the pool. void deallocate_free_chunks() { node_pool<0>::get(this->derived()->get_node_pool())->deallocate_free_blocks(); }};template<class T, class NodePool, unsigned int Version>class cached_allocator_impl : public array_allocation_impl <cached_allocator_impl<T, NodePool, Version>, T, typename NodePool::segment_manager>{ cached_allocator_impl & operator=(const cached_allocator_impl& other); typedef array_allocation_impl < cached_allocator_impl <T, NodePool, Version> , T , typename NodePool::segment_manager> base_t; public: typedef NodePool node_pool_t; typedef typename NodePool::segment_manager segment_manager; typedef typename segment_manager::void_pointer void_pointer; typedef typename detail:: pointer_to_other<void_pointer, const void>::type cvoid_pointer; typedef typename base_t::pointer pointer; typedef typename base_t::size_type size_type; typedef typename base_t::multiallocation_iterator multiallocation_iterator; typedef typename base_t::multiallocation_chain multiallocation_chain; typedef typename base_t::value_type value_type; public: enum { DEFAULT_MAX_CACHED_NODES = 64 }; cached_allocator_impl(segment_manager *segment_mngr, std::size_t max_cached_nodes) : m_cache(segment_mngr, max_cached_nodes) {} cached_allocator_impl(const cached_allocator_impl &other) : m_cache(other.m_cache) {} //!Copy constructor from related cached_adaptive_pool_base. If not present, constructs //!a node pool. Increments the reference count of the associated node pool. //!Can throw boost::interprocess::bad_alloc template<class T2, class NodePool2> cached_allocator_impl (const cached_allocator_impl <T2, NodePool2, Version> &other) : m_cache(other.get_segment_manager(), other.get_max_cached_nodes()) {} //!Returns a pointer to the node pool. //!Never throws node_pool_t* get_node_pool() const { return m_cache.get_node_pool(); } //!Returns the segment manager. //!Never throws segment_manager* get_segment_manager()const { return m_cache.get_segment_manager(); } //!Sets the new max cached nodes value. This can provoke deallocations //!if "newmax" is less than current cached nodes. Never throws void set_max_cached_nodes(std::size_t newmax) { m_cache.set_max_cached_nodes(newmax); } //!Returns the max cached nodes parameter. //!Never throws std::size_t get_max_cached_nodes() const { return m_cache.get_max_cached_nodes(); } //!Allocate memory for an array of count elements. //!Throws boost::interprocess::bad_alloc if there is no enough memory pointer allocate(size_type count, cvoid_pointer hint = 0) { (void)hint; void * ret; if(count > this->max_size()) throw bad_alloc(); else if(Version == 1 && count == 1){ ret = m_cache.cached_allocation(); } else{ ret = this->get_segment_manager()->allocate(sizeof(T)*count); } return pointer(static_cast<T*>(ret)); } //!Deallocate allocated memory. Never throws void deallocate(const pointer &ptr, size_type count) { (void)count; if(Version == 1 && count == 1){ m_cache.cached_deallocation(detail::get_pointer(ptr)); } else{ this->get_segment_manager()->deallocate((void*)detail::get_pointer(ptr)); } } //!Allocates just one object. Memory allocated with this function //!must be deallocated only with deallocate_one(). //!Throws boost::interprocess::bad_alloc if there is no enough memory pointer allocate_one() { return pointer(static_cast<value_type*>(this->m_cache.cached_allocation())); } //!Allocates many elements of size == 1 in a contiguous block //!of memory. The minimum number to be allocated is min_elements, //!the preferred and maximum number is //!preferred_elements. The number of actually allocated elements is //!will be assigned to received_size. Memory allocated with this function //!must be deallocated only with deallocate_one(). multiallocation_iterator allocate_individual(std::size_t num_elements) { return multiallocation_iterator(this->m_cache.cached_allocation(num_elements)); } //!Deallocates memory previously allocated with allocate_one(). //!You should never use deallocate_one to deallocate memory allocated //!with other functions different from allocate_one(). Never throws void deallocate_one(const pointer &p) { this->m_cache.cached_deallocation(detail::get_pointer(p)); } //!Allocates many elements of size == 1 in a contiguous block //!of memory. The minimum number to be allocated is min_elements, //!the preferred and maximum number is //!preferred_elements. The number of actually allocated elements is //!will be assigned to received_size. Memory allocated with this function //!must be deallocated only with deallocate_one(). void deallocate_individual(multiallocation_iterator it) { m_cache.cached_deallocation(it.base()); } //!Deallocates all free blocks of the pool void deallocate_free_blocks() { m_cache.get_node_pool()->deallocate_free_blocks(); } //!Swaps allocators. Does not throw. If each allocator is placed in a //!different shared memory segments, the result is undefined. friend void swap(cached_allocator_impl &alloc1, cached_allocator_impl &alloc2) { detail::do_swap(alloc1.mp_node_pool, alloc2.mp_node_pool); alloc1.m_cached_nodes.swap(alloc2.m_cached_nodes); detail::do_swap(alloc1.m_max_cached_nodes, alloc2.m_max_cached_nodes); } void deallocate_cache() { m_cache.deallocate_all_cached_nodes(); } //!Deprecated use deallocate_free_blocks. void deallocate_free_chunks() { m_cache.get_node_pool()->deallocate_free_blocks(); } /// @cond private: cache_impl<node_pool_t> m_cache;};//!Equality test for same type of//!cached_allocator_impltemplate<class T, class N, unsigned int V> inlinebool operator==(const cached_allocator_impl<T, N, V> &alloc1, const cached_allocator_impl<T, N, V> &alloc2) { return alloc1.get_node_pool() == alloc2.get_node_pool(); }//!Inequality test for same type of//!cached_allocator_impltemplate<class T, class N, unsigned int V> inlinebool operator!=(const cached_allocator_impl<T, N, V> &alloc1, const cached_allocator_impl<T, N, V> &alloc2) { return alloc1.get_node_pool() != alloc2.get_node_pool(); }//!Pooled shared memory allocator using adaptive pool. Includes//!a reference count but the class does not delete itself, this is //!responsibility of user classes. Node size (NodeSize) and the number of//!nodes allocated per block (NodesPerBlock) are known at compile timetemplate<class private_node_allocator_t>class shared_pool_impl : public private_node_allocator_t{ public: //!Segment manager typedef typedef typename private_node_allocator_t::segment_manager segment_manager; typedef typename private_node_allocator_t:: multiallocation_iterator multiallocation_iterator; typedef typename private_node_allocator_t:: multiallocation_chain multiallocation_chain; private: typedef typename segment_manager::mutex_family::mutex_type mutex_type; public: //!Constructor from a segment manager. Never throws shared_pool_impl(segment_manager *segment_mngr) : private_node_allocator_t(segment_mngr) {} //!Destructor. Deallocates all allocated blocks. Never throws ~shared_pool_impl() {} //!Allocates array of count elements. Can throw boost::interprocess::bad_alloc void *allocate_node() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- return private_node_allocator_t::allocate_node(); } //!Deallocates an array pointed by ptr. Never throws void deallocate_node(void *ptr) { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::deallocate_node(ptr); } //!Allocates a singly linked list of n nodes ending in null pointer. //!can throw boost::interprocess::bad_alloc void allocate_nodes(multiallocation_chain &nodes, std::size_t n) { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- return private_node_allocator_t::allocate_nodes(nodes, n); } //!Allocates n nodes, pointed by the multiallocation_iterator. //!Can throw boost::interprocess::bad_alloc multiallocation_iterator allocate_nodes(const std::size_t n) { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- return private_node_allocator_t::allocate_nodes(n); } //!Deallocates a linked list of nodes ending in null pointer. Never throws void deallocate_nodes(multiallocation_chain &nodes, std::size_t num) { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::deallocate_nodes(nodes, num); } //!Deallocates a linked list of nodes ending in null pointer. Never throws void deallocate_nodes(multiallocation_chain &nodes) { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::deallocate_nodes(nodes); } //!Deallocates the nodes pointed by the multiallocation iterator. Never throws void deallocate_nodes(multiallocation_iterator it) { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::deallocate_nodes(it); } //!Deallocates all the free blocks of memory. Never throws void deallocate_free_blocks() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::deallocate_free_blocks(); } //!Deallocates all used memory from the common pool. //!Precondition: all nodes allocated from this pool should //!already be deallocated. Otherwise, undefined behavior. Never throws void purge_blocks() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::purge_blocks(); } //!Increments internal reference count and returns new count. Never throws std::size_t inc_ref_count() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- return ++m_header.m_usecount; } //!Decrements internal reference count and returns new count. Never throws std::size_t dec_ref_count() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- assert(m_header.m_usecount > 0); return --m_header.m_usecount; } //!Deprecated, use deallocate_free_blocks. void deallocate_free_chunks() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::deallocate_free_blocks(); } //!Deprecated, use purge_blocks. void purge_chunks() { //----------------------- boost::interprocess::scoped_lock<mutex_type> guard(m_header); //----------------------- private_node_allocator_t::purge_blocks(); } private: //!This struct includes needed data and derives from //!interprocess_mutex to allow EBO when using null_mutex struct header_t : mutex_type { std::size_t m_usecount; //Number of attached allocators header_t() : m_usecount(0) {} } m_header;};} //namespace detail {} //namespace interprocess {} //namespace boost {#include <boost/interprocess/detail/config_end.hpp>#endif //#ifndef BOOST_INTERPROCESS_DETAIL_NODE_ALLOCATOR_COMMON_HPP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?