Chris@102: ////////////////////////////////////////////////////////////////////////////// Chris@102: // Chris@102: // (C) Copyright Ion Gaztanaga 2014-2015. 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_NEW_ALLOCATOR_HPP Chris@102: #define BOOST_CONTAINER_NEW_ALLOCATOR_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: #include Chris@102: #include Chris@102: Chris@102: //!\file Chris@102: Chris@102: namespace boost { Chris@102: namespace container { Chris@102: Chris@102: template Chris@102: struct new_allocator_bool Chris@102: { static const bool value = Value; }; Chris@102: Chris@102: template Chris@102: class new_allocator; Chris@102: Chris@102: //! Specialization of new_allocator for void types Chris@102: template<> Chris@102: class new_allocator Chris@102: { Chris@102: public: Chris@102: typedef void value_type; Chris@102: typedef void * pointer; Chris@102: typedef const void* const_pointer; Chris@102: //!A integral constant of type bool with value true Chris@102: typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool) propagate_on_container_move_assignment; Chris@102: //!A integral constant of type bool with value true Chris@102: typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool) is_always_equal; Chris@102: // reference-to-void members are impossible Chris@102: Chris@102: //!Obtains an new_allocator that allocates Chris@102: //!objects of type T2 Chris@102: template Chris@102: struct rebind Chris@102: { Chris@102: typedef new_allocator< T2> other; Chris@102: }; Chris@102: Chris@102: //!Default constructor Chris@102: //!Never throws Chris@102: new_allocator() BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!Constructor from other new_allocator. Chris@102: //!Never throws Chris@102: new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!Constructor from related new_allocator. Chris@102: //!Never throws Chris@102: template Chris@102: new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!Swaps two allocators, does nothing Chris@102: //!because this new_allocator is stateless Chris@102: friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!An new_allocator always compares to true, as memory allocated with one Chris@102: //!instance can be deallocated by another instance Chris@102: friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: { return true; } Chris@102: Chris@102: //!An new_allocator always compares to false, as memory allocated with one Chris@102: //!instance can be deallocated by another instance Chris@102: friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: { return false; } Chris@102: }; Chris@102: Chris@102: Chris@102: //! This class is a reduced STL-compatible allocator that allocates memory using operator new Chris@102: template Chris@102: class new_allocator Chris@102: { Chris@102: public: Chris@102: typedef T value_type; Chris@102: typedef T * pointer; Chris@102: typedef const T * const_pointer; Chris@102: typedef T & reference; Chris@102: typedef const T & const_reference; Chris@102: typedef std::size_t size_type; Chris@102: typedef std::ptrdiff_t difference_type; Chris@102: //!A integral constant of type bool with value true Chris@102: typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool) propagate_on_container_move_assignment; Chris@102: //!A integral constant of type bool with value true Chris@102: typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool) is_always_equal; Chris@102: Chris@102: //!Obtains an new_allocator that allocates Chris@102: //!objects of type T2 Chris@102: template Chris@102: struct rebind Chris@102: { Chris@102: typedef new_allocator other; Chris@102: }; Chris@102: Chris@102: //!Default constructor Chris@102: //!Never throws Chris@102: new_allocator() BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!Constructor from other new_allocator. Chris@102: //!Never throws Chris@102: new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!Constructor from related new_allocator. Chris@102: //!Never throws Chris@102: template Chris@102: new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!Allocates memory for an array of count elements. Chris@102: //!Throws std::bad_alloc if there is no enough memory Chris@102: pointer allocate(size_type count) Chris@102: { Chris@102: if(BOOST_UNLIKELY(count > this->max_size())) Chris@102: throw_bad_alloc(); Chris@102: return static_cast(::operator new(count*sizeof(T))); Chris@102: } Chris@102: Chris@102: //!Deallocates previously allocated memory. Chris@102: //!Never throws Chris@102: void deallocate(pointer ptr, size_type) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: { ::operator delete((void*)ptr); } Chris@102: Chris@102: //!Returns the maximum number of elements that could be allocated. Chris@102: //!Never throws Chris@102: size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW Chris@102: { return size_type(-1)/sizeof(T); } Chris@102: Chris@102: //!Swaps two allocators, does nothing Chris@102: //!because this new_allocator is stateless Chris@102: friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: {} Chris@102: Chris@102: //!An new_allocator always compares to true, as memory allocated with one Chris@102: //!instance can be deallocated by another instance Chris@102: friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: { return true; } Chris@102: Chris@102: //!An new_allocator always compares to false, as memory allocated with one Chris@102: //!instance can be deallocated by another instance Chris@102: friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW Chris@102: { return false; } Chris@102: }; Chris@102: Chris@102: } //namespace container { Chris@102: } //namespace boost { Chris@102: Chris@102: #include Chris@102: Chris@102: #endif //BOOST_CONTAINER_ALLOCATOR_HPP