]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Temporary buffer implementation -*- C++ -*- |
2 | ||
8d9254fc | 3 | // Copyright (C) 2001-2020 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996,1997 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
f910786b | 51 | /** @file bits/stl_tempbuf.h |
729e3d3f | 52 | * This is an internal header file, included by other library headers. |
f910786b | 53 | * Do not attempt to use it directly. @headername{memory} |
725dc051 BK |
54 | */ |
55 | ||
046d30f4 PC |
56 | #ifndef _STL_TEMPBUF_H |
57 | #define _STL_TEMPBUF_H 1 | |
725dc051 | 58 | |
acb8a4ef PC |
59 | #include <bits/stl_algobase.h> |
60 | #include <bits/stl_construct.h> | |
725dc051 | 61 | |
12ffa228 BK |
62 | namespace std _GLIBCXX_VISIBILITY(default) |
63 | { | |
64 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 65 | |
f48d9d19 FD |
66 | namespace __detail |
67 | { | |
68 | template<typename _Tp> | |
69 | inline void | |
70 | __return_temporary_buffer(_Tp* __p, | |
71 | size_t __len __attribute__((__unused__))) | |
72 | { | |
73 | #if __cpp_sized_deallocation | |
2be3193b | 74 | ::operator delete(__p, __len * sizeof(_Tp)); |
f48d9d19 FD |
75 | #else |
76 | ::operator delete(__p); | |
77 | #endif | |
78 | } | |
79 | } | |
80 | ||
acb8a4ef | 81 | /** |
d87135d4 | 82 | * @brief Allocates a temporary buffer. |
93c66bc6 | 83 | * @param __len The number of objects of type Tp. |
d87135d4 PC |
84 | * @return See full description. |
85 | * | |
86 | * Reinventing the wheel, but this time with prettier spokes! | |
87 | * | |
93c66bc6 | 88 | * This function tries to obtain storage for @c __len adjacent Tp |
d87135d4 | 89 | * objects. The objects themselves are not constructed, of course. |
2a60a9f6 | 90 | * A pair<> is returned containing <em>the buffer s address and |
93c66bc6 | 91 | * capacity (in the units of sizeof(_Tp)), or a pair of 0 values if |
2a60a9f6 | 92 | * no storage can be obtained.</em> Note that the capacity obtained |
d87135d4 PC |
93 | * may be less than that requested if the memory is unavailable; |
94 | * you should compare len with the .second return value. | |
95 | * | |
96 | * Provides the nothrow exception guarantee. | |
acb8a4ef PC |
97 | */ |
98 | template<typename _Tp> | |
99 | pair<_Tp*, ptrdiff_t> | |
6b14c6d7 | 100 | get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOEXCEPT |
acb8a4ef PC |
101 | { |
102 | const ptrdiff_t __max = | |
103 | __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp); | |
104 | if (__len > __max) | |
105 | __len = __max; | |
aa7df52e JW |
106 | |
107 | while (__len > 0) | |
acb8a4ef | 108 | { |
aa7df52e | 109 | _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), |
acb8a4ef PC |
110 | std::nothrow)); |
111 | if (__tmp != 0) | |
112 | return std::pair<_Tp*, ptrdiff_t>(__tmp, __len); | |
113 | __len /= 2; | |
114 | } | |
115 | return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0); | |
116 | } | |
117 | ||
acb8a4ef PC |
118 | /** |
119 | * @brief The companion to get_temporary_buffer(). | |
93c66bc6 | 120 | * @param __p A buffer previously allocated by get_temporary_buffer. |
acb8a4ef PC |
121 | * @return None. |
122 | * | |
93c66bc6 | 123 | * Frees the memory pointed to by __p. |
acb8a4ef PC |
124 | */ |
125 | template<typename _Tp> | |
de5e4138 | 126 | inline void |
acb8a4ef | 127 | return_temporary_buffer(_Tp* __p) |
aa7df52e | 128 | { ::operator delete(__p); } |
acb8a4ef | 129 | |
917a9fd4 | 130 | /** |
917a9fd4 SW |
131 | * This class is used in two places: stl_algo.h and ext/memory, |
132 | * where it is wrapped as the temporary_buffer class. See | |
133 | * temporary_buffer docs for more notes. | |
917a9fd4 SW |
134 | */ |
135 | template<typename _ForwardIterator, typename _Tp> | |
136 | class _Temporary_buffer | |
137 | { | |
138 | // concept requirements | |
139 | __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept) | |
140 | ||
43804767 | 141 | public: |
917a9fd4 SW |
142 | typedef _Tp value_type; |
143 | typedef value_type* pointer; | |
144 | typedef pointer iterator; | |
145 | typedef ptrdiff_t size_type; | |
ed6814f7 | 146 | |
917a9fd4 SW |
147 | protected: |
148 | size_type _M_original_len; | |
149 | size_type _M_len; | |
150 | pointer _M_buffer; | |
ed6814f7 | 151 | |
917a9fd4 SW |
152 | public: |
153 | /// As per Table mumble. | |
154 | size_type | |
155 | size() const | |
156 | { return _M_len; } | |
ed6814f7 | 157 | |
917a9fd4 SW |
158 | /// Returns the size requested by the constructor; may be >size(). |
159 | size_type | |
160 | requested_size() const | |
161 | { return _M_original_len; } | |
ed6814f7 | 162 | |
917a9fd4 SW |
163 | /// As per Table mumble. |
164 | iterator | |
165 | begin() | |
166 | { return _M_buffer; } | |
ed6814f7 | 167 | |
917a9fd4 SW |
168 | /// As per Table mumble. |
169 | iterator | |
170 | end() | |
171 | { return _M_buffer + _M_len; } | |
ed6814f7 | 172 | |
917a9fd4 SW |
173 | /** |
174 | * Constructs a temporary buffer of a size somewhere between | |
d31238cf | 175 | * zero and the given length. |
917a9fd4 | 176 | */ |
d31238cf | 177 | _Temporary_buffer(_ForwardIterator __seed, size_type __original_len); |
ed6814f7 | 178 | |
917a9fd4 | 179 | ~_Temporary_buffer() |
ed6814f7 | 180 | { |
917a9fd4 | 181 | std::_Destroy(_M_buffer, _M_buffer + _M_len); |
f48d9d19 | 182 | std::__detail::__return_temporary_buffer(_M_buffer, _M_len); |
917a9fd4 | 183 | } |
ed6814f7 | 184 | |
917a9fd4 SW |
185 | private: |
186 | // Disable copy constructor and assignment operator. | |
187 | _Temporary_buffer(const _Temporary_buffer&); | |
83042fca PC |
188 | |
189 | void | |
190 | operator=(const _Temporary_buffer&); | |
917a9fd4 | 191 | }; |
ed6814f7 | 192 | |
fe27aa8b PC |
193 | |
194 | template<bool> | |
195 | struct __uninitialized_construct_buf_dispatch | |
196 | { | |
80a009e5 | 197 | template<typename _Pointer, typename _ForwardIterator> |
fe27aa8b | 198 | static void |
80a009e5 FD |
199 | __ucr(_Pointer __first, _Pointer __last, |
200 | _ForwardIterator __seed) | |
fe27aa8b | 201 | { |
f48d9d19 | 202 | if (__first == __last) |
fe27aa8b PC |
203 | return; |
204 | ||
80a009e5 | 205 | _Pointer __cur = __first; |
fe27aa8b PC |
206 | __try |
207 | { | |
208 | std::_Construct(std::__addressof(*__first), | |
80a009e5 FD |
209 | _GLIBCXX_MOVE(*__seed)); |
210 | _Pointer __prev = __cur; | |
fe27aa8b PC |
211 | ++__cur; |
212 | for(; __cur != __last; ++__cur, ++__prev) | |
213 | std::_Construct(std::__addressof(*__cur), | |
214 | _GLIBCXX_MOVE(*__prev)); | |
80a009e5 | 215 | *__seed = _GLIBCXX_MOVE(*__prev); |
fe27aa8b PC |
216 | } |
217 | __catch(...) | |
218 | { | |
219 | std::_Destroy(__first, __cur); | |
220 | __throw_exception_again; | |
221 | } | |
222 | } | |
223 | }; | |
224 | ||
225 | template<> | |
226 | struct __uninitialized_construct_buf_dispatch<true> | |
227 | { | |
80a009e5 | 228 | template<typename _Pointer, typename _ForwardIterator> |
fe27aa8b | 229 | static void |
80a009e5 | 230 | __ucr(_Pointer, _Pointer, _ForwardIterator) { } |
fe27aa8b PC |
231 | }; |
232 | ||
233 | // Constructs objects in the range [first, last). | |
234 | // Note that while these new objects will take valid values, | |
235 | // their exact value is not defined. In particular they may | |
236 | // be 'moved from'. | |
237 | // | |
80a009e5 | 238 | // While *__seed may be altered during this algorithm, it will have |
fe27aa8b PC |
239 | // the same value when the algorithm finishes, unless one of the |
240 | // constructions throws. | |
241 | // | |
80a009e5 FD |
242 | // Requirements: _Pointer::value_type(_Tp&&) is valid. |
243 | template<typename _Pointer, typename _ForwardIterator> | |
fe27aa8b | 244 | inline void |
80a009e5 FD |
245 | __uninitialized_construct_buf(_Pointer __first, _Pointer __last, |
246 | _ForwardIterator __seed) | |
fe27aa8b | 247 | { |
80a009e5 | 248 | typedef typename std::iterator_traits<_Pointer>::value_type |
fe27aa8b PC |
249 | _ValueType; |
250 | ||
251 | std::__uninitialized_construct_buf_dispatch< | |
252 | __has_trivial_constructor(_ValueType)>:: | |
80a009e5 | 253 | __ucr(__first, __last, __seed); |
fe27aa8b PC |
254 | } |
255 | ||
917a9fd4 SW |
256 | template<typename _ForwardIterator, typename _Tp> |
257 | _Temporary_buffer<_ForwardIterator, _Tp>:: | |
d31238cf FD |
258 | _Temporary_buffer(_ForwardIterator __seed, size_type __original_len) |
259 | : _M_original_len(__original_len), _M_len(0), _M_buffer(0) | |
917a9fd4 | 260 | { |
f48d9d19 FD |
261 | std::pair<pointer, size_type> __p( |
262 | std::get_temporary_buffer<value_type>(_M_original_len)); | |
263 | ||
264 | if (__p.first) | |
ed6814f7 | 265 | { |
f48d9d19 FD |
266 | __try |
267 | { | |
268 | std::__uninitialized_construct_buf(__p.first, __p.first + __p.second, | |
269 | __seed); | |
270 | _M_buffer = __p.first; | |
271 | _M_len = __p.second; | |
272 | } | |
273 | __catch(...) | |
274 | { | |
275 | std::__detail::__return_temporary_buffer(__p.first, __p.second); | |
276 | __throw_exception_again; | |
277 | } | |
83042fca | 278 | } |
917a9fd4 | 279 | } |
3cbc7af0 | 280 | |
12ffa228 BK |
281 | _GLIBCXX_END_NAMESPACE_VERSION |
282 | } // namespace | |
725dc051 | 283 | |
046d30f4 | 284 | #endif /* _STL_TEMPBUF_H */ |