1 // Temporary buffer implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file stl_tempbuf.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
57 #ifndef _STL_TEMPBUF_H
58 #define _STL_TEMPBUF_H 1
60 #include <bits/stl_algobase.h>
61 #include <bits/stl_construct.h>
63 _GLIBCXX_BEGIN_NAMESPACE(std
)
66 * @brief Allocates a temporary buffer.
67 * @param len The number of objects of type Tp.
68 * @return See full description.
70 * Reinventing the wheel, but this time with prettier spokes!
72 * This function tries to obtain storage for @c len adjacent Tp
73 * objects. The objects themselves are not constructed, of course.
74 * A pair<> is returned containing <em>the buffer s address and
75 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if
76 * no storage can be obtained.</em> Note that the capacity obtained
77 * may be less than that requested if the memory is unavailable;
78 * you should compare len with the .second return value.
80 * Provides the nothrow exception guarantee.
82 template<typename _Tp
>
84 get_temporary_buffer(ptrdiff_t __len
)
86 const ptrdiff_t __max
=
87 __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
93 _Tp
* __tmp
= static_cast<_Tp
*>(::operator new(__len
* sizeof(_Tp
),
96 return std::pair
<_Tp
*, ptrdiff_t>(__tmp
, __len
);
99 return std::pair
<_Tp
*, ptrdiff_t>(static_cast<_Tp
*>(0), 0);
103 * @brief The companion to get_temporary_buffer().
104 * @param p A buffer previously allocated by get_temporary_buffer.
107 * Frees the memory pointed to by p.
109 template<typename _Tp
>
111 return_temporary_buffer(_Tp
* __p
)
112 { ::operator delete(__p
, std::nothrow
); }
116 * This class is used in two places: stl_algo.h and ext/memory,
117 * where it is wrapped as the temporary_buffer class. See
118 * temporary_buffer docs for more notes.
120 template<typename _ForwardIterator
, typename _Tp
>
121 class _Temporary_buffer
123 // concept requirements
124 __glibcxx_class_requires(_ForwardIterator
, _ForwardIteratorConcept
)
127 typedef _Tp value_type
;
128 typedef value_type
* pointer
;
129 typedef pointer iterator
;
130 typedef ptrdiff_t size_type
;
133 size_type _M_original_len
;
138 /// As per Table mumble.
143 /// Returns the size requested by the constructor; may be >size().
145 requested_size() const
146 { return _M_original_len
; }
148 /// As per Table mumble.
151 { return _M_buffer
; }
153 /// As per Table mumble.
156 { return _M_buffer
+ _M_len
; }
159 * Constructs a temporary buffer of a size somewhere between
160 * zero and the size of the given range.
162 _Temporary_buffer(_ForwardIterator __first
, _ForwardIterator __last
);
166 std::_Destroy(_M_buffer
, _M_buffer
+ _M_len
);
167 std::return_temporary_buffer(_M_buffer
);
171 // Disable copy constructor and assignment operator.
172 _Temporary_buffer(const _Temporary_buffer
&);
175 operator=(const _Temporary_buffer
&);
180 struct __uninitialized_construct_buf_dispatch
182 template<typename _ForwardIterator
, typename _Tp
>
184 __ucr(_ForwardIterator __first
, _ForwardIterator __last
,
187 if(__first
== __last
)
190 _ForwardIterator __cur
= __first
;
193 std::_Construct(std::__addressof(*__first
),
194 _GLIBCXX_MOVE(__value
));
195 _ForwardIterator __prev
= __cur
;
197 for(; __cur
!= __last
; ++__cur
, ++__prev
)
198 std::_Construct(std::__addressof(*__cur
),
199 _GLIBCXX_MOVE(*__prev
));
200 __value
= _GLIBCXX_MOVE(*__prev
);
204 std::_Destroy(__first
, __cur
);
205 __throw_exception_again
;
211 struct __uninitialized_construct_buf_dispatch
<true>
213 template<typename _ForwardIterator
, typename _Tp
>
215 __ucr(_ForwardIterator
, _ForwardIterator
, _Tp
&) { }
218 // Constructs objects in the range [first, last).
219 // Note that while these new objects will take valid values,
220 // their exact value is not defined. In particular they may
223 // While __value may altered during this algorithm, it will have
224 // the same value when the algorithm finishes, unless one of the
225 // constructions throws.
227 // Requirements: _ForwardIterator::value_type(_Tp&&) is valid.
228 template<typename _ForwardIterator
, typename _Tp
>
230 __uninitialized_construct_buf(_ForwardIterator __first
,
231 _ForwardIterator __last
,
234 typedef typename
std::iterator_traits
<_ForwardIterator
>::value_type
237 std::__uninitialized_construct_buf_dispatch
<
238 __has_trivial_constructor(_ValueType
)>::
239 __ucr(__first
, __last
, __value
);
242 template<typename _ForwardIterator
, typename _Tp
>
243 _Temporary_buffer
<_ForwardIterator
, _Tp
>::
244 _Temporary_buffer(_ForwardIterator __first
, _ForwardIterator __last
)
245 : _M_original_len(std::distance(__first
, __last
)),
246 _M_len(0), _M_buffer(0)
250 std::pair
<pointer
, size_type
> __p(std::get_temporary_buffer
<
251 value_type
>(_M_original_len
));
252 _M_buffer
= __p
.first
;
255 std::__uninitialized_construct_buf(_M_buffer
, _M_buffer
+ _M_len
,
260 std::return_temporary_buffer(_M_buffer
);
263 __throw_exception_again
;
267 _GLIBCXX_END_NAMESPACE
269 #endif /* _STL_TEMPBUF_H */