]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_tempbuf.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_tempbuf.h
CommitLineData
42526146
PE
1// Temporary buffer implementation -*- C++ -*-
2
818ab71a 3// Copyright (C) 2001-2016 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
62namespace std _GLIBCXX_VISIBILITY(default)
63{
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 65
acb8a4ef 66 /**
d87135d4 67 * @brief Allocates a temporary buffer.
93c66bc6 68 * @param __len The number of objects of type Tp.
d87135d4
PC
69 * @return See full description.
70 *
71 * Reinventing the wheel, but this time with prettier spokes!
72 *
93c66bc6 73 * This function tries to obtain storage for @c __len adjacent Tp
d87135d4 74 * objects. The objects themselves are not constructed, of course.
2a60a9f6 75 * A pair<> is returned containing <em>the buffer s address and
93c66bc6 76 * capacity (in the units of sizeof(_Tp)), or a pair of 0 values if
2a60a9f6 77 * no storage can be obtained.</em> Note that the capacity obtained
d87135d4
PC
78 * may be less than that requested if the memory is unavailable;
79 * you should compare len with the .second return value.
80 *
81 * Provides the nothrow exception guarantee.
acb8a4ef
PC
82 */
83 template<typename _Tp>
84 pair<_Tp*, ptrdiff_t>
6b14c6d7 85 get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOEXCEPT
acb8a4ef
PC
86 {
87 const ptrdiff_t __max =
88 __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
89 if (__len > __max)
90 __len = __max;
91
92 while (__len > 0)
93 {
94 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
95 std::nothrow));
96 if (__tmp != 0)
97 return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
98 __len /= 2;
99 }
100 return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
101 }
102
acb8a4ef
PC
103 /**
104 * @brief The companion to get_temporary_buffer().
93c66bc6 105 * @param __p A buffer previously allocated by get_temporary_buffer.
acb8a4ef
PC
106 * @return None.
107 *
93c66bc6 108 * Frees the memory pointed to by __p.
acb8a4ef
PC
109 */
110 template<typename _Tp>
de5e4138 111 inline void
acb8a4ef 112 return_temporary_buffer(_Tp* __p)
de5e4138 113 { ::operator delete(__p, std::nothrow); }
acb8a4ef
PC
114
115
917a9fd4 116 /**
917a9fd4
SW
117 * This class is used in two places: stl_algo.h and ext/memory,
118 * where it is wrapped as the temporary_buffer class. See
119 * temporary_buffer docs for more notes.
917a9fd4
SW
120 */
121 template<typename _ForwardIterator, typename _Tp>
122 class _Temporary_buffer
123 {
124 // concept requirements
125 __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
126
43804767 127 public:
917a9fd4
SW
128 typedef _Tp value_type;
129 typedef value_type* pointer;
130 typedef pointer iterator;
131 typedef ptrdiff_t size_type;
ed6814f7 132
917a9fd4
SW
133 protected:
134 size_type _M_original_len;
135 size_type _M_len;
136 pointer _M_buffer;
ed6814f7 137
917a9fd4
SW
138 public:
139 /// As per Table mumble.
140 size_type
141 size() const
142 { return _M_len; }
ed6814f7 143
917a9fd4
SW
144 /// Returns the size requested by the constructor; may be >size().
145 size_type
146 requested_size() const
147 { return _M_original_len; }
ed6814f7 148
917a9fd4
SW
149 /// As per Table mumble.
150 iterator
151 begin()
152 { return _M_buffer; }
ed6814f7 153
917a9fd4
SW
154 /// As per Table mumble.
155 iterator
156 end()
157 { return _M_buffer + _M_len; }
ed6814f7 158
917a9fd4
SW
159 /**
160 * Constructs a temporary buffer of a size somewhere between
161 * zero and the size of the given range.
162 */
163 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
ed6814f7 164
917a9fd4 165 ~_Temporary_buffer()
ed6814f7 166 {
917a9fd4
SW
167 std::_Destroy(_M_buffer, _M_buffer + _M_len);
168 std::return_temporary_buffer(_M_buffer);
169 }
ed6814f7 170
917a9fd4
SW
171 private:
172 // Disable copy constructor and assignment operator.
173 _Temporary_buffer(const _Temporary_buffer&);
83042fca
PC
174
175 void
176 operator=(const _Temporary_buffer&);
917a9fd4 177 };
ed6814f7 178
fe27aa8b
PC
179
180 template<bool>
181 struct __uninitialized_construct_buf_dispatch
182 {
80a009e5 183 template<typename _Pointer, typename _ForwardIterator>
fe27aa8b 184 static void
80a009e5
FD
185 __ucr(_Pointer __first, _Pointer __last,
186 _ForwardIterator __seed)
fe27aa8b
PC
187 {
188 if(__first == __last)
189 return;
190
80a009e5 191 _Pointer __cur = __first;
fe27aa8b
PC
192 __try
193 {
194 std::_Construct(std::__addressof(*__first),
80a009e5
FD
195 _GLIBCXX_MOVE(*__seed));
196 _Pointer __prev = __cur;
fe27aa8b
PC
197 ++__cur;
198 for(; __cur != __last; ++__cur, ++__prev)
199 std::_Construct(std::__addressof(*__cur),
200 _GLIBCXX_MOVE(*__prev));
80a009e5 201 *__seed = _GLIBCXX_MOVE(*__prev);
fe27aa8b
PC
202 }
203 __catch(...)
204 {
205 std::_Destroy(__first, __cur);
206 __throw_exception_again;
207 }
208 }
209 };
210
211 template<>
212 struct __uninitialized_construct_buf_dispatch<true>
213 {
80a009e5 214 template<typename _Pointer, typename _ForwardIterator>
fe27aa8b 215 static void
80a009e5 216 __ucr(_Pointer, _Pointer, _ForwardIterator) { }
fe27aa8b
PC
217 };
218
219 // Constructs objects in the range [first, last).
220 // Note that while these new objects will take valid values,
221 // their exact value is not defined. In particular they may
222 // be 'moved from'.
223 //
80a009e5 224 // While *__seed may be altered during this algorithm, it will have
fe27aa8b
PC
225 // the same value when the algorithm finishes, unless one of the
226 // constructions throws.
227 //
80a009e5
FD
228 // Requirements: _Pointer::value_type(_Tp&&) is valid.
229 template<typename _Pointer, typename _ForwardIterator>
fe27aa8b 230 inline void
80a009e5
FD
231 __uninitialized_construct_buf(_Pointer __first, _Pointer __last,
232 _ForwardIterator __seed)
fe27aa8b 233 {
80a009e5 234 typedef typename std::iterator_traits<_Pointer>::value_type
fe27aa8b
PC
235 _ValueType;
236
237 std::__uninitialized_construct_buf_dispatch<
238 __has_trivial_constructor(_ValueType)>::
80a009e5 239 __ucr(__first, __last, __seed);
fe27aa8b
PC
240 }
241
917a9fd4
SW
242 template<typename _ForwardIterator, typename _Tp>
243 _Temporary_buffer<_ForwardIterator, _Tp>::
244 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
ed6814f7 245 : _M_original_len(std::distance(__first, __last)),
83042fca 246 _M_len(0), _M_buffer(0)
917a9fd4 247 {
bc2631e0 248 __try
83042fca 249 {
acb8a4ef
PC
250 std::pair<pointer, size_type> __p(std::get_temporary_buffer<
251 value_type>(_M_original_len));
83042fca
PC
252 _M_buffer = __p.first;
253 _M_len = __p.second;
80a009e5 254 if (_M_buffer)
fe27aa8b 255 std::__uninitialized_construct_buf(_M_buffer, _M_buffer + _M_len,
80a009e5 256 __first);
83042fca 257 }
bc2631e0 258 __catch(...)
ed6814f7 259 {
83042fca 260 std::return_temporary_buffer(_M_buffer);
ed6814f7 261 _M_buffer = 0;
83042fca 262 _M_len = 0;
ed6814f7 263 __throw_exception_again;
83042fca 264 }
917a9fd4 265 }
3cbc7af0 266
12ffa228
BK
267_GLIBCXX_END_NAMESPACE_VERSION
268} // namespace
725dc051 269
046d30f4 270#endif /* _STL_TEMPBUF_H */
725dc051 271