libstdc++
stl_tempbuf.h
Go to the documentation of this file.
1// Temporary buffer implementation -*- C++ -*-
2
3// Copyright (C) 2001-2022 Free Software Foundation, Inc.
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
8// Free Software Foundation; either version 3, or (at your option)
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
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.
19
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/>.
24
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
51/** @file bits/stl_tempbuf.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{memory}
54 */
55
56#ifndef _STL_TEMPBUF_H
57#define _STL_TEMPBUF_H 1
58
59#include <bits/stl_algobase.h>
60#include <bits/stl_construct.h>
61
62namespace std _GLIBCXX_VISIBILITY(default)
63{
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
65
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
74 ::operator delete(__p, __len * sizeof(_Tp));
75#else
76 ::operator delete(__p);
77#endif
78 }
79 }
80
81 /**
82 * @brief Allocates a temporary buffer.
83 * @param __len The number of objects of type Tp.
84 * @return See full description.
85 *
86 * Reinventing the wheel, but this time with prettier spokes!
87 *
88 * This function tries to obtain storage for @c __len adjacent Tp
89 * objects. The objects themselves are not constructed, of course.
90 * A pair<> is returned containing <em>the buffer s address and
91 * capacity (in the units of sizeof(_Tp)), or a pair of 0 values if
92 * no storage can be obtained.</em> Note that the capacity obtained
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.
97 */
98 template<typename _Tp>
99 _GLIBCXX17_DEPRECATED
100 pair<_Tp*, ptrdiff_t>
101 get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOEXCEPT
102 {
103 const ptrdiff_t __max =
104 __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
105 if (__len > __max)
106 __len = __max;
107
108 while (__len > 0)
109 {
110 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
111 std::nothrow));
112 if (__tmp != 0)
113 return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
114 __len = __len == 1 ? 0 : ((__len + 1) / 2);
115 }
116 return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
117 }
118
119 /**
120 * @brief The companion to get_temporary_buffer().
121 * @param __p A buffer previously allocated by get_temporary_buffer.
122 * @return None.
123 *
124 * Frees the memory pointed to by __p.
125 */
126 template<typename _Tp>
127 inline void
129 { ::operator delete(__p); }
130
131 /**
132 * This class is used in two places: stl_algo.h and ext/memory,
133 * where it is wrapped as the temporary_buffer class. See
134 * temporary_buffer docs for more notes.
135 */
136 template<typename _ForwardIterator, typename _Tp>
138 {
139 // concept requirements
140 __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
141
142 public:
143 typedef _Tp value_type;
144 typedef value_type* pointer;
145 typedef pointer iterator;
146 typedef ptrdiff_t size_type;
147
148 protected:
149 size_type _M_original_len;
150 size_type _M_len;
151 pointer _M_buffer;
152
153 public:
154 /// As per Table mumble.
155 size_type
156 size() const
157 { return _M_len; }
158
159 /// Returns the size requested by the constructor; may be >size().
160 size_type
162 { return _M_original_len; }
163
164 /// As per Table mumble.
167 { return _M_buffer; }
168
169 /// As per Table mumble.
172 { return _M_buffer + _M_len; }
173
174 /**
175 * Constructs a temporary buffer of a size somewhere between
176 * zero and the given length.
177 */
178 _Temporary_buffer(_ForwardIterator __seed, size_type __original_len);
179
181 {
182 std::_Destroy(_M_buffer, _M_buffer + _M_len);
183 std::__detail::__return_temporary_buffer(_M_buffer, _M_len);
184 }
185
186 private:
187 // Disable copy constructor and assignment operator.
189
190 void
191 operator=(const _Temporary_buffer&);
192 };
193
194
195 template<bool>
196 struct __uninitialized_construct_buf_dispatch
197 {
198 template<typename _Pointer, typename _ForwardIterator>
199 static void
200 __ucr(_Pointer __first, _Pointer __last,
201 _ForwardIterator __seed)
202 {
203 if (__first == __last)
204 return;
205
206 _Pointer __cur = __first;
207 __try
208 {
210 _GLIBCXX_MOVE(*__seed));
211 _Pointer __prev = __cur;
212 ++__cur;
213 for(; __cur != __last; ++__cur, ++__prev)
215 _GLIBCXX_MOVE(*__prev));
216 *__seed = _GLIBCXX_MOVE(*__prev);
217 }
218 __catch(...)
219 {
220 std::_Destroy(__first, __cur);
221 __throw_exception_again;
222 }
223 }
224 };
225
226 template<>
227 struct __uninitialized_construct_buf_dispatch<true>
228 {
229 template<typename _Pointer, typename _ForwardIterator>
230 static void
231 __ucr(_Pointer, _Pointer, _ForwardIterator) { }
232 };
233
234 // Constructs objects in the range [first, last).
235 // Note that while these new objects will take valid values,
236 // their exact value is not defined. In particular they may
237 // be 'moved from'.
238 //
239 // While *__seed may be altered during this algorithm, it will have
240 // the same value when the algorithm finishes, unless one of the
241 // constructions throws.
242 //
243 // Requirements: _Pointer::value_type(_Tp&&) is valid.
244 template<typename _Pointer, typename _ForwardIterator>
245 inline void
246 __uninitialized_construct_buf(_Pointer __first, _Pointer __last,
247 _ForwardIterator __seed)
248 {
250 _ValueType;
251
252 std::__uninitialized_construct_buf_dispatch<
253 __has_trivial_constructor(_ValueType)>::
254 __ucr(__first, __last, __seed);
255 }
256
257 template<typename _ForwardIterator, typename _Tp>
259 _Temporary_buffer(_ForwardIterator __seed, size_type __original_len)
260 : _M_original_len(__original_len), _M_len(0), _M_buffer(0)
261 {
263 std::get_temporary_buffer<value_type>(_M_original_len));
264
265 if (__p.first)
266 {
267 __try
268 {
269 std::__uninitialized_construct_buf(__p.first, __p.first + __p.second,
270 __seed);
271 _M_buffer = __p.first;
272 _M_len = __p.second;
273 }
274 __catch(...)
275 {
276 std::__detail::__return_temporary_buffer(__p.first, __p.second);
277 __throw_exception_again;
278 }
279 }
280 }
281
282_GLIBCXX_END_NAMESPACE_VERSION
283} // namespace
284
285#endif /* _STL_TEMPBUF_H */
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
ISO C++ entities toplevel namespace is std.
pair< _Tp *, ptrdiff_t > get_temporary_buffer(ptrdiff_t __len) noexcept
Allocates a temporary buffer.
Definition: stl_tempbuf.h:101
void return_temporary_buffer(_Tp *__p)
The companion to get_temporary_buffer().
Definition: stl_tempbuf.h:128
constexpr void _Construct(_Tp *__p, _Args &&... __args)
constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last, _Allocator &__alloc)
Traits class for iterators.
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:187
_T1 first
The first member.
Definition: stl_pair.h:191
_T2 second
The second member.
Definition: stl_pair.h:192
Common iterator class.
iterator end()
As per Table mumble.
Definition: stl_tempbuf.h:171
size_type requested_size() const
Returns the size requested by the constructor; may be >size().
Definition: stl_tempbuf.h:161
size_type size() const
As per Table mumble.
Definition: stl_tempbuf.h:156
iterator begin()
As per Table mumble.
Definition: stl_tempbuf.h:166
_Temporary_buffer(_ForwardIterator __seed, size_type __original_len)
Definition: stl_tempbuf.h:259