1 // Temporary buffer implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_tempbuf.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/stl_algobase.h>
66 #include <bits/stl_construct.h>
67 #include <bits/stl_uninitialized.h>
69 _GLIBCXX_BEGIN_NAMESPACE(std
)
73 * This is a helper function. The unused second parameter exists to
74 * permit the real get_temporary_buffer to use template parameter deduction.
77 template<typename _Tp
>
79 __get_temporary_buffer(ptrdiff_t __len
, _Tp
*)
81 const ptrdiff_t __max
=
82 __gnu_cxx::__numeric_traits
<ptrdiff_t>::__max
/ sizeof(_Tp
);
88 _Tp
* __tmp
= static_cast<_Tp
*>(::operator new(__len
* sizeof(_Tp
),
91 return std::pair
<_Tp
*, ptrdiff_t>(__tmp
, __len
);
94 return std::pair
<_Tp
*, ptrdiff_t>(static_cast<_Tp
*>(0), 0);
98 * @brief Allocates a temporary buffer.
99 * @param len The number of objects of type Tp.
100 * @return See full description.
102 * Reinventing the wheel, but this time with prettier spokes!
104 * This function tries to obtain storage for @c len adjacent Tp
105 * objects. The objects themselves are not constructed, of course.
106 * A pair<> is returned containing "the buffer s address and
107 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if
108 * no storage can be obtained." Note that the capacity obtained
109 * may be less than that requested if the memory is unavailable;
110 * you should compare len with the .second return value.
112 * Provides the nothrow exception guarantee.
114 template<typename _Tp
>
115 inline pair
<_Tp
*, ptrdiff_t>
116 get_temporary_buffer(ptrdiff_t __len
)
117 { return std::__get_temporary_buffer(__len
, static_cast<_Tp
*>(0)); }
120 * @brief The companion to get_temporary_buffer().
121 * @param p A buffer previously allocated by get_temporary_buffer.
124 * Frees the memory pointed to by p.
126 template<typename _Tp
>
128 return_temporary_buffer(_Tp
* __p
)
129 { ::operator delete(__p
, std::nothrow
); }
134 * This class is used in two places: stl_algo.h and ext/memory,
135 * where it is wrapped as the temporary_buffer class. See
136 * temporary_buffer docs for more notes.
139 template<typename _ForwardIterator
, typename _Tp
>
140 class _Temporary_buffer
142 // concept requirements
143 __glibcxx_class_requires(_ForwardIterator
, _ForwardIteratorConcept
)
146 typedef _Tp value_type
;
147 typedef value_type
* pointer
;
148 typedef pointer iterator
;
149 typedef ptrdiff_t size_type
;
152 size_type _M_original_len
;
157 /// As per Table mumble.
162 /// Returns the size requested by the constructor; may be >size().
164 requested_size() const
165 { return _M_original_len
; }
167 /// As per Table mumble.
170 { return _M_buffer
; }
172 /// As per Table mumble.
175 { return _M_buffer
+ _M_len
; }
178 * Constructs a temporary buffer of a size somewhere between
179 * zero and the size of the given range.
181 _Temporary_buffer(_ForwardIterator __first
, _ForwardIterator __last
);
185 std::_Destroy(_M_buffer
, _M_buffer
+ _M_len
);
186 std::return_temporary_buffer(_M_buffer
);
190 // Disable copy constructor and assignment operator.
191 _Temporary_buffer(const _Temporary_buffer
&);
194 operator=(const _Temporary_buffer
&);
197 template<typename _ForwardIterator
, typename _Tp
>
198 _Temporary_buffer
<_ForwardIterator
, _Tp
>::
199 _Temporary_buffer(_ForwardIterator __first
, _ForwardIterator __last
)
200 : _M_original_len(std::distance(__first
, __last
)),
201 _M_len(0), _M_buffer(0)
205 std::pair
<pointer
, size_type
> __p(std::get_temporary_buffer
<
206 value_type
>(_M_original_len
));
207 _M_buffer
= __p
.first
;
209 if (!__is_pod(_Tp
) && _M_len
> 0)
210 std::uninitialized_fill_n(_M_buffer
, _M_len
, *__first
);
214 std::return_temporary_buffer(_M_buffer
);
217 __throw_exception_again
;
221 _GLIBCXX_END_NAMESPACE
223 #endif /* _TEMPBUF_H */