Chris@102
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
2 //
|
Chris@102
|
3 // (C) Copyright Ion Gaztanaga 2014-2015. Distributed under the Boost
|
Chris@102
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@102
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6 //
|
Chris@102
|
7 // See http://www.boost.org/libs/container for documentation.
|
Chris@102
|
8 //
|
Chris@102
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
10
|
Chris@102
|
11 #ifndef BOOST_CONTAINER_NEW_ALLOCATOR_HPP
|
Chris@102
|
12 #define BOOST_CONTAINER_NEW_ALLOCATOR_HPP
|
Chris@102
|
13
|
Chris@102
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@102
|
15 # include <boost/config.hpp>
|
Chris@102
|
16 #endif
|
Chris@102
|
17
|
Chris@102
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@102
|
19 # pragma once
|
Chris@102
|
20 #endif
|
Chris@102
|
21
|
Chris@102
|
22 #include <boost/container/detail/config_begin.hpp>
|
Chris@102
|
23 #include <boost/container/detail/workaround.hpp>
|
Chris@102
|
24 #include <boost/container/throw_exception.hpp>
|
Chris@102
|
25 #include <cstddef>
|
Chris@102
|
26
|
Chris@102
|
27 //!\file
|
Chris@102
|
28
|
Chris@102
|
29 namespace boost {
|
Chris@102
|
30 namespace container {
|
Chris@102
|
31
|
Chris@102
|
32 template<bool Value>
|
Chris@102
|
33 struct new_allocator_bool
|
Chris@102
|
34 { static const bool value = Value; };
|
Chris@102
|
35
|
Chris@102
|
36 template<class T>
|
Chris@102
|
37 class new_allocator;
|
Chris@102
|
38
|
Chris@102
|
39 //! Specialization of new_allocator for void types
|
Chris@102
|
40 template<>
|
Chris@102
|
41 class new_allocator<void>
|
Chris@102
|
42 {
|
Chris@102
|
43 public:
|
Chris@102
|
44 typedef void value_type;
|
Chris@102
|
45 typedef void * pointer;
|
Chris@102
|
46 typedef const void* const_pointer;
|
Chris@102
|
47 //!A integral constant of type bool with value true
|
Chris@102
|
48 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
|
Chris@102
|
49 //!A integral constant of type bool with value true
|
Chris@102
|
50 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
|
Chris@102
|
51 // reference-to-void members are impossible
|
Chris@102
|
52
|
Chris@102
|
53 //!Obtains an new_allocator that allocates
|
Chris@102
|
54 //!objects of type T2
|
Chris@102
|
55 template<class T2>
|
Chris@102
|
56 struct rebind
|
Chris@102
|
57 {
|
Chris@102
|
58 typedef new_allocator< T2> other;
|
Chris@102
|
59 };
|
Chris@102
|
60
|
Chris@102
|
61 //!Default constructor
|
Chris@102
|
62 //!Never throws
|
Chris@102
|
63 new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
64 {}
|
Chris@102
|
65
|
Chris@102
|
66 //!Constructor from other new_allocator.
|
Chris@102
|
67 //!Never throws
|
Chris@102
|
68 new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
69 {}
|
Chris@102
|
70
|
Chris@102
|
71 //!Constructor from related new_allocator.
|
Chris@102
|
72 //!Never throws
|
Chris@102
|
73 template<class T2>
|
Chris@102
|
74 new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
75 {}
|
Chris@102
|
76
|
Chris@102
|
77 //!Swaps two allocators, does nothing
|
Chris@102
|
78 //!because this new_allocator is stateless
|
Chris@102
|
79 friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
80 {}
|
Chris@102
|
81
|
Chris@102
|
82 //!An new_allocator always compares to true, as memory allocated with one
|
Chris@102
|
83 //!instance can be deallocated by another instance
|
Chris@102
|
84 friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
85 { return true; }
|
Chris@102
|
86
|
Chris@102
|
87 //!An new_allocator always compares to false, as memory allocated with one
|
Chris@102
|
88 //!instance can be deallocated by another instance
|
Chris@102
|
89 friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
90 { return false; }
|
Chris@102
|
91 };
|
Chris@102
|
92
|
Chris@102
|
93
|
Chris@102
|
94 //! This class is a reduced STL-compatible allocator that allocates memory using operator new
|
Chris@102
|
95 template<class T>
|
Chris@102
|
96 class new_allocator
|
Chris@102
|
97 {
|
Chris@102
|
98 public:
|
Chris@102
|
99 typedef T value_type;
|
Chris@102
|
100 typedef T * pointer;
|
Chris@102
|
101 typedef const T * const_pointer;
|
Chris@102
|
102 typedef T & reference;
|
Chris@102
|
103 typedef const T & const_reference;
|
Chris@102
|
104 typedef std::size_t size_type;
|
Chris@102
|
105 typedef std::ptrdiff_t difference_type;
|
Chris@102
|
106 //!A integral constant of type bool with value true
|
Chris@102
|
107 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
|
Chris@102
|
108 //!A integral constant of type bool with value true
|
Chris@102
|
109 typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
|
Chris@102
|
110
|
Chris@102
|
111 //!Obtains an new_allocator that allocates
|
Chris@102
|
112 //!objects of type T2
|
Chris@102
|
113 template<class T2>
|
Chris@102
|
114 struct rebind
|
Chris@102
|
115 {
|
Chris@102
|
116 typedef new_allocator<T2> other;
|
Chris@102
|
117 };
|
Chris@102
|
118
|
Chris@102
|
119 //!Default constructor
|
Chris@102
|
120 //!Never throws
|
Chris@102
|
121 new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
122 {}
|
Chris@102
|
123
|
Chris@102
|
124 //!Constructor from other new_allocator.
|
Chris@102
|
125 //!Never throws
|
Chris@102
|
126 new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
127 {}
|
Chris@102
|
128
|
Chris@102
|
129 //!Constructor from related new_allocator.
|
Chris@102
|
130 //!Never throws
|
Chris@102
|
131 template<class T2>
|
Chris@102
|
132 new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
133 {}
|
Chris@102
|
134
|
Chris@102
|
135 //!Allocates memory for an array of count elements.
|
Chris@102
|
136 //!Throws std::bad_alloc if there is no enough memory
|
Chris@102
|
137 pointer allocate(size_type count)
|
Chris@102
|
138 {
|
Chris@102
|
139 if(BOOST_UNLIKELY(count > this->max_size()))
|
Chris@102
|
140 throw_bad_alloc();
|
Chris@102
|
141 return static_cast<T*>(::operator new(count*sizeof(T)));
|
Chris@102
|
142 }
|
Chris@102
|
143
|
Chris@102
|
144 //!Deallocates previously allocated memory.
|
Chris@102
|
145 //!Never throws
|
Chris@102
|
146 void deallocate(pointer ptr, size_type) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
147 { ::operator delete((void*)ptr); }
|
Chris@102
|
148
|
Chris@102
|
149 //!Returns the maximum number of elements that could be allocated.
|
Chris@102
|
150 //!Never throws
|
Chris@102
|
151 size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
152 { return size_type(-1)/sizeof(T); }
|
Chris@102
|
153
|
Chris@102
|
154 //!Swaps two allocators, does nothing
|
Chris@102
|
155 //!because this new_allocator is stateless
|
Chris@102
|
156 friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
157 {}
|
Chris@102
|
158
|
Chris@102
|
159 //!An new_allocator always compares to true, as memory allocated with one
|
Chris@102
|
160 //!instance can be deallocated by another instance
|
Chris@102
|
161 friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
162 { return true; }
|
Chris@102
|
163
|
Chris@102
|
164 //!An new_allocator always compares to false, as memory allocated with one
|
Chris@102
|
165 //!instance can be deallocated by another instance
|
Chris@102
|
166 friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
|
Chris@102
|
167 { return false; }
|
Chris@102
|
168 };
|
Chris@102
|
169
|
Chris@102
|
170 } //namespace container {
|
Chris@102
|
171 } //namespace boost {
|
Chris@102
|
172
|
Chris@102
|
173 #include <boost/container/detail/config_end.hpp>
|
Chris@102
|
174
|
Chris@102
|
175 #endif //BOOST_CONTAINER_ALLOCATOR_HPP
|