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 //!Constructor from related new_allocator.
78 new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
81 //!Swaps two allocators, does nothing
82 //!because this new_allocator is stateless
83 friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
86 //!An new_allocator always compares to true, as memory allocated with one
87 //!instance can be deallocated by another instance
88 friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
91 //!An new_allocator always compares to false, as memory allocated with one
92 //!instance can be deallocated by another instance
93 friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
98 //! This class is a reduced STL-compatible allocator that allocates memory using operator new
103 typedef T value_type;
105 typedef const T * const_pointer;
106 typedef T & reference;
107 typedef const T & const_reference;
108 typedef std::size_t size_type;
109 typedef std::ptrdiff_t difference_type;
110 //!A integral constant of type bool with value true
111 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
112 //!A integral constant of type bool with value true
113 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
115 //!Obtains an new_allocator that allocates
116 //!objects of type T2
120 typedef new_allocator<T2> other;
123 //!Default constructor
125 new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
128 //!Constructor from other new_allocator.
130 new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
133 //!Constructor from related new_allocator.
136 new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
139 //!Allocates memory for an array of count elements.
140 //!Throws std::bad_alloc if there is no enough memory
141 pointer allocate(size_type count)
143 if(BOOST_UNLIKELY(count > this->max_size()))
145 return static_cast<T*>(::operator new(count*sizeof(T)));
148 //!Deallocates previously allocated memory.
150 void deallocate(pointer ptr, size_type) BOOST_NOEXCEPT_OR_NOTHROW
151 { ::operator delete((void*)ptr); }
153 //!Returns the maximum number of elements that could be allocated.
155 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
156 { return size_type(-1)/sizeof(T); }
158 //!Swaps two allocators, does nothing
159 //!because this new_allocator is stateless
160 friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
163 //!An new_allocator always compares to true, as memory allocated with one
164 //!instance can be deallocated by another instance
165 friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
168 //!An new_allocator always compares to false, as memory allocated with one
169 //!instance can be deallocated by another instance
170 friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
174 } //namespace container {
175 } //namespace boost {
177 #include <boost/container/detail/config_end.hpp>
179 #endif //BOOST_CONTAINER_NEW_ALLOCATOR_HPP