4 * Hewlett-Packard Company
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
15 * Copyright (c) 1996,1997
16 * Silicon Graphics Computer Systems, Inc.
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
31 #ifndef __SGI_STL_INTERNAL_TEMPBUF_H
32 #define __SGI_STL_INTERNAL_TEMPBUF_H
39 __get_temporary_buffer(ptrdiff_t __len
, _Tp
*)
41 if (__len
> ptrdiff_t(INT_MAX
/ sizeof(_Tp
)))
42 __len
= INT_MAX
/ sizeof(_Tp
);
45 _Tp
* __tmp
= (_Tp
*) malloc((size_t)__len
* sizeof(_Tp
));
47 return pair
<_Tp
*, ptrdiff_t>(__tmp
, __len
);
51 return pair
<_Tp
*, ptrdiff_t>((_Tp
*)0, 0);
54 #ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS
57 inline pair
<_Tp
*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len
) {
58 return __get_temporary_buffer(__len
, (_Tp
*) 0);
61 #endif /* __STL_EXPLICIT_FUNCTION_TMPL_ARGS */
63 // This overload is not required by the standard; it is an extension.
64 // It is supported for backward compatibility with the HP STL, and
65 // because not all compilers support the language feature (explicit
66 // function template arguments) that is required for the standard
67 // version of get_temporary_buffer.
69 inline pair
<_Tp
*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len
, _Tp
*) {
70 return __get_temporary_buffer(__len
, (_Tp
*) 0);
74 void return_temporary_buffer(_Tp
* __p
) {
78 template <class _ForwardIterator
, class _Tp
>
79 class _Temporary_buffer
{
81 ptrdiff_t _M_original_len
;
85 void _M_allocate_buffer() {
86 _M_original_len
= _M_len
;
89 if (_M_len
> (ptrdiff_t)(INT_MAX
/ sizeof(_Tp
)))
90 _M_len
= INT_MAX
/ sizeof(_Tp
);
93 _M_buffer
= (_Tp
*) malloc(_M_len
* sizeof(_Tp
));
100 void _M_initialize_buffer(const _Tp
&, __true_type
) {}
101 void _M_initialize_buffer(const _Tp
& val
, __false_type
) {
102 uninitialized_fill_n(_M_buffer
, _M_len
, val
);
106 ptrdiff_t size() const { return _M_len
; }
107 ptrdiff_t requested_size() const { return _M_original_len
; }
108 _Tp
* begin() { return _M_buffer
; }
109 _Tp
* end() { return _M_buffer
+ _M_len
; }
111 _Temporary_buffer(_ForwardIterator __first
, _ForwardIterator __last
) {
112 typedef typename __type_traits
<_Tp
>::has_trivial_default_constructor
116 distance(__first
, __last
, _M_len
);
117 _M_allocate_buffer();
119 _M_initialize_buffer(*__first
, _Trivial());
121 __STL_UNWIND(free(_M_buffer
); _M_buffer
= 0; _M_len
= 0);
124 ~_Temporary_buffer() {
125 destroy(_M_buffer
, _M_buffer
+ _M_len
);
130 // Disable copy constructor and assignment operator.
131 _Temporary_buffer(const _Temporary_buffer
&) {}
132 void operator=(const _Temporary_buffer
&) {}
135 // Class temporary_buffer is not part of the standard. It is an extension.
137 template <class _ForwardIterator
,
139 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
140 = typename iterator_traits
<_ForwardIterator
>::value_type
141 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
143 struct temporary_buffer
: public _Temporary_buffer
<_ForwardIterator
, _Tp
>
145 temporary_buffer(_ForwardIterator __first
, _ForwardIterator __last
)
146 : _Temporary_buffer
<_ForwardIterator
, _Tp
>(__first
, __last
) {}
147 ~temporary_buffer() {}
152 #endif /* __SGI_STL_INTERNAL_TEMPBUF_H */