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_ALLOCATOR_HPP Chris@16: #define BOOST_INTERPROCESS_ALLOCATOR_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@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes an allocator that allocates portions of fixed size Chris@16: //!memory buffer (shared memory, mapped file...) Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: Chris@16: //!An STL compatible allocator that uses a segment manager as Chris@16: //!memory 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: template Chris@16: class allocator 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: Chris@16: //Self type Chris@16: typedef allocator self_t; Chris@16: Chris@16: //Pointer to void Chris@16: typedef typename segment_manager::void_pointer aux_pointer_t; Chris@16: Chris@16: //Typedef to const void pointer Chris@16: typedef typename boost::intrusive:: Chris@16: pointer_traits::template Chris@16: rebind_pointer::type cvoid_ptr; Chris@16: Chris@16: //Pointer to the allocator Chris@16: typedef typename boost::intrusive:: Chris@16: pointer_traits::template Chris@16: rebind_pointer::type alloc_ptr_t; Chris@16: Chris@16: //Not assignable from related allocator Chris@16: template Chris@16: allocator& operator=(const allocator&); Chris@16: Chris@16: //Not assignable from other allocator Chris@16: allocator& operator=(const allocator&); Chris@16: Chris@16: //Pointer to the allocator Chris@16: alloc_ptr_t mp_mngr; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: typedef T value_type; 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 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: typedef boost::interprocess::version_type version; Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: //Experimental. Don't use. Chris@16: typedef boost::container::container_detail::transform_multiallocation_chain Chris@16: multiallocation_chain; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!Obtains an allocator that allocates Chris@16: //!objects of type T2 Chris@16: template Chris@16: struct rebind Chris@16: { Chris@16: typedef allocator other; Chris@16: }; Chris@16: Chris@16: //!Returns the segment manager. Chris@16: //!Never throws Chris@16: segment_manager* get_segment_manager()const Chris@16: { return ipcdetail::to_raw_pointer(mp_mngr); } Chris@16: Chris@16: //!Constructor from the segment manager. Chris@16: //!Never throws Chris@16: allocator(segment_manager *segment_mngr) Chris@16: : mp_mngr(segment_mngr) { } Chris@16: Chris@16: //!Constructor from other allocator. Chris@16: //!Never throws Chris@16: allocator(const allocator &other) Chris@16: : mp_mngr(other.get_segment_manager()){ } Chris@16: Chris@16: //!Constructor from related allocator. Chris@16: //!Never throws Chris@16: template Chris@16: allocator(const allocator &other) Chris@16: : mp_mngr(other.get_segment_manager()){} Chris@16: Chris@16: //!Allocates 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_ptr hint = 0) Chris@16: { Chris@16: (void)hint; Chris@16: if(size_overflows(count)){ Chris@16: throw bad_alloc(); Chris@16: } Chris@16: return pointer(static_cast(mp_mngr->allocate(count*sizeof(T)))); Chris@16: } Chris@16: Chris@16: //!Deallocates memory previously allocated. Chris@16: //!Never throws Chris@16: void deallocate(const pointer &ptr, size_type) Chris@16: { mp_mngr->deallocate((void*)ipcdetail::to_raw_pointer(ptr)); } 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: { return mp_mngr->get_size()/sizeof(T); } Chris@16: Chris@16: //!Swap segment manager. Does not throw. If each allocator is placed in Chris@16: //!different memory segments, the result is undefined. Chris@16: friend void swap(self_t &alloc1, self_t &alloc2) Chris@101: { boost::adl_move_swap(alloc1.mp_mngr, alloc2.mp_mngr); } 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@16: return (size_type)mp_mngr->size(ipcdetail::to_raw_pointer(p))/sizeof(T); Chris@16: } 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@101: value_type *reuse_raw = ipcdetail::to_raw_pointer(reuse); Chris@101: pointer const p = mp_mngr->allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse_raw); Chris@101: reuse = reuse_raw; Chris@101: return p; Chris@16: } 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: if(size_overflows(elem_size)){ Chris@16: throw bad_alloc(); Chris@16: } Chris@16: mp_mngr->allocate_many(elem_size*sizeof(T), num_elements, chain); Chris@16: } 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: mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T), chain); Chris@16: } 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: { mp_mngr->deallocate_many(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: { return this->allocate(1); } 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: { this->allocate_many(1, num_elements, 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: { return this->deallocate(p, 1); } 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: { this->deallocate_many(chain); } Chris@16: Chris@16: //!Returns address of mutable object. Chris@16: //!Never throws Chris@16: pointer address(reference value) const Chris@16: { return pointer(boost::addressof(value)); } 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: { return const_pointer(boost::addressof(value)); } Chris@16: Chris@16: //!Constructs an object Chris@16: //!Throws if T's constructor throws Chris@16: //!For backwards compatibility with libraries using C++03 allocators Chris@16: template Chris@16: void construct(const pointer &ptr, BOOST_FWD_REF(P) p) Chris@101: { ::new((void*)ipcdetail::to_raw_pointer(ptr), boost_container_new_t()) value_type(::boost::forward

(p)); } Chris@16: Chris@16: //!Destroys object. Throws if object's Chris@16: //!destructor throws Chris@16: void destroy(const pointer &ptr) Chris@16: { BOOST_ASSERT(ptr != 0); (*ptr).~value_type(); } Chris@16: Chris@16: }; Chris@16: Chris@16: //!Equality test for same type Chris@16: //!of allocator Chris@16: template inline Chris@16: bool operator==(const allocator &alloc1, Chris@16: const allocator &alloc2) Chris@16: { return alloc1.get_segment_manager() == alloc2.get_segment_manager(); } Chris@16: Chris@16: //!Inequality test for same type Chris@16: //!of allocator Chris@16: template inline Chris@16: bool operator!=(const allocator &alloc1, Chris@16: const allocator &alloc2) Chris@16: { return alloc1.get_segment_manager() != alloc2.get_segment_manager(); } Chris@16: Chris@16: } //namespace interprocess { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: template Chris@16: struct has_trivial_destructor; Chris@16: Chris@16: template Chris@16: struct has_trivial_destructor Chris@16: > Chris@16: { Chris@16: static const bool value = true; Chris@16: }; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_ALLOCATOR_HPP Chris@16: