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
38 pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t len, T*) {
39 if (len > ptrdiff_t(INT_MAX / sizeof(T)))
40 len = INT_MAX / sizeof(T);
43 T* tmp = (T*) malloc((size_t)len * sizeof(T));
45 return pair<T*, ptrdiff_t>(tmp, len);
49 return pair<T*, ptrdiff_t>((T*)0, 0);
53 void return_temporary_buffer(T* p) {
57 template <class ForwardIterator,
59 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
60 = iterator_traits<ForwardIterator>::value_type
61 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
63 class temporary_buffer {
65 ptrdiff_t original_len;
69 void allocate_buffer() {
73 if (len > (ptrdiff_t)(INT_MAX / sizeof(T)))
74 len = INT_MAX / sizeof(T);
77 buffer = (T*) malloc(len * sizeof(T));
84 void initialize_buffer(const T&, __true_type) {}
85 void initialize_buffer(const T& val, __false_type) {
86 uninitialized_fill_n(buffer, len, val);
90 ptrdiff_t size() const { return len; }
91 ptrdiff_t requested_size() const { return original_len; }
92 T* begin() { return buffer; }
93 T* end() { return buffer + len; }
95 temporary_buffer(ForwardIterator first, ForwardIterator last) {
98 distance(first, last, len);
101 initialize_buffer(*first,
102 __type_traits<T>::has_trivial_default_constructor());
104 __STL_UNWIND(free(buffer); buffer = 0; len = 0);
107 ~temporary_buffer() {
108 destroy(buffer, buffer + len);
113 temporary_buffer(const temporary_buffer&) {}
114 void operator=(const temporary_buffer&) {}
119 #endif /* __SGI_STL_INTERNAL_TEMPBUF_H */