00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef __SGI_STL_INTERNAL_TEMPBUF_H
00032 #define __SGI_STL_INTERNAL_TEMPBUF_H
00033
00034
00035 __STL_BEGIN_NAMESPACE
00036
00037 template <class _Tp>
00038 pair<_Tp*, ptrdiff_t>
00039 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
00040 {
00041 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
00042 __len = INT_MAX / sizeof(_Tp);
00043
00044 while (__len > 0) {
00045 _Tp* __tmp = (_Tp*) malloc((size_t)__len * sizeof(_Tp));
00046 if (__tmp != 0)
00047 return pair<_Tp*, ptrdiff_t>(__tmp, __len);
00048 __len /= 2;
00049 }
00050
00051 return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
00052 }
00053
00054 #ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS
00055
00056 template <class _Tp>
00057 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) {
00058 return __get_temporary_buffer(__len, (_Tp*) 0);
00059 }
00060
00061 #endif
00062
00063
00064
00065
00066
00067
00068 template <class _Tp>
00069 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len, _Tp*) {
00070 return __get_temporary_buffer(__len, (_Tp*) 0);
00071 }
00072
00073 template <class _Tp>
00074 void return_temporary_buffer(_Tp* __p) {
00075 free(__p);
00076 }
00077
00078 template <class _ForwardIterator, class _Tp>
00079 class _Temporary_buffer {
00080 private:
00081 ptrdiff_t _M_original_len;
00082 ptrdiff_t _M_len;
00083 _Tp* _M_buffer;
00084
00085 void _M_allocate_buffer() {
00086 _M_original_len = _M_len;
00087 _M_buffer = 0;
00088
00089 if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp)))
00090 _M_len = INT_MAX / sizeof(_Tp);
00091
00092 while (_M_len > 0) {
00093 _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp));
00094 if (_M_buffer)
00095 break;
00096 _M_len /= 2;
00097 }
00098 }
00099
00100 void _M_initialize_buffer(const _Tp&, __true_type) {}
00101 void _M_initialize_buffer(const _Tp& val, __false_type) {
00102 uninitialized_fill_n(_M_buffer, _M_len, val);
00103 }
00104
00105 public:
00106 ptrdiff_t size() const { return _M_len; }
00107 ptrdiff_t requested_size() const { return _M_original_len; }
00108 _Tp* begin() { return _M_buffer; }
00109 _Tp* end() { return _M_buffer + _M_len; }
00110
00111 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) {
00112
00113 # if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION < 730
00114 typedef typename __type_traits<_Tp>::is_POD_type _Trivial;
00115 # else
00116 typedef typename __type_traits<_Tp>::has_trivial_default_constructor
00117 _Trivial;
00118 # endif
00119
00120 __STL_TRY {
00121 _M_len = 0;
00122 distance(__first, __last, _M_len);
00123 _M_allocate_buffer();
00124 if (_M_len > 0)
00125 _M_initialize_buffer(*__first, _Trivial());
00126 }
00127 __STL_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0);
00128 }
00129
00130 ~_Temporary_buffer() {
00131 destroy(_M_buffer, _M_buffer + _M_len);
00132 free(_M_buffer);
00133 }
00134
00135 private:
00136
00137 _Temporary_buffer(const _Temporary_buffer&) {}
00138 void operator=(const _Temporary_buffer&) {}
00139 };
00140
00141
00142
00143 template <class _ForwardIterator,
00144 class _Tp
00145 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
00146 = typename iterator_traits<_ForwardIterator>::value_type
00147 #endif
00148 >
00149 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
00150 {
00151 temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00152 : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {}
00153 ~temporary_buffer() {}
00154 };
00155
00156 __STL_END_NAMESPACE
00157
00158 #endif
00159
00160
00161
00162