1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2014-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_NEW_ALLOCATOR_HPP
12 #define BOOST_CONTAINER_NEW_ALLOCATOR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/container/throw_exception.hpp>
35 struct new_allocator_bool
36 { static const bool value = Value; };
43 //! Specialization of new_allocator for void types
45 class new_allocator<void>
48 typedef void value_type;
49 typedef void * pointer;
50 typedef const void* const_pointer;
51 //!A integral constant of type bool with value true
52 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
53 //!A integral constant of type bool with value true
54 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
55 // reference-to-void members are impossible
57 //!Obtains an new_allocator that allocates
62 typedef new_allocator< T2> other;
65 //!Default constructor
67 new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
70 //!Constructor from other new_allocator.
72 new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
75 //!Copy assignment operator from other new_allocator.
77 new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
82 //!Constructor from related new_allocator.
85 new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
88 //!Swaps two allocators, does nothing
89 //!because this new_allocator is stateless
90 friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
93 //!An new_allocator always compares to true, as memory allocated with one
94 //!instance can be deallocated by another instance
95 friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
98 //!An new_allocator always compares to false, as memory allocated with one
99 //!instance can be deallocated by another instance
100 friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
105 //! This class is a reduced STL-compatible allocator that allocates memory using operator new
110 typedef T value_type;
112 typedef const T * const_pointer;
113 typedef T & reference;
114 typedef const T & const_reference;
115 typedef std::size_t size_type;
116 typedef std::ptrdiff_t difference_type;
117 //!A integral constant of type bool with value true
118 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
119 //!A integral constant of type bool with value true
120 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
122 //!Obtains an new_allocator that allocates
123 //!objects of type T2
127 typedef new_allocator<T2> other;
130 //!Default constructor
132 new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
135 //!Constructor from other new_allocator.
137 new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
140 //!Copy assignment operator from other new_allocator.
142 new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
147 //!Constructor from related new_allocator.
150 new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
153 //!Allocates memory for an array of count elements.
154 //!Throws std::bad_alloc if there is no enough memory
155 pointer allocate(size_type count)
157 const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
158 if(BOOST_UNLIKELY(count > max_count))
160 return static_cast<T*>(::operator new(count*sizeof(T)));
163 //!Deallocates previously allocated memory.
165 void deallocate(pointer ptr, size_type) BOOST_NOEXCEPT_OR_NOTHROW
166 { ::operator delete((void*)ptr); }
168 //!Returns the maximum number of elements that could be allocated.
170 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
171 { return std::size_t(-1)/(2*sizeof(T)); }
173 //!Swaps two allocators, does nothing
174 //!because this new_allocator is stateless
175 friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
178 //!An new_allocator always compares to true, as memory allocated with one
179 //!instance can be deallocated by another instance
180 friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
183 //!An new_allocator always compares to false, as memory allocated with one
184 //!instance can be deallocated by another instance
185 friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
189 } //namespace container {
190 } //namespace boost {
192 #include <boost/container/detail/config_end.hpp>
194 #endif //BOOST_CONTAINER_NEW_ALLOCATOR_HPP