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 #ifndef _STLP_INTERNAL_TEMPBUF_H
00031 #define _STLP_INTERNAL_TEMPBUF_H
00032
00033 # ifndef _STLP_CLIMITS
00034 # include <climits>
00035 # endif
00036 # ifndef _STLP_CSTDLIB
00037 # include <cstdlib>
00038 # endif
00039 # ifndef _STLP_INTERNAL_UNINITIALIZED_H
00040 # include <stl/_uninitialized.h>
00041 # endif
00042
00043 _STLP_BEGIN_NAMESPACE
00044
00045 template <class _Tp>
00046 pair<_Tp*, ptrdiff_t> _STLP_CALL
00047 __get_temporary_buffer(ptrdiff_t __len, _Tp*);
00048
00049 #ifndef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
00050
00051 template <class _Tp>
00052 inline pair<_Tp*, ptrdiff_t> _STLP_CALL get_temporary_buffer(ptrdiff_t __len) {
00053 return __get_temporary_buffer(__len, (_Tp*) 0);
00054 }
00055
00056 # if ! defined(_STLP_NO_EXTENSIONS)
00057
00058
00059
00060
00061
00062 template <class _Tp>
00063 inline pair<_Tp*, ptrdiff_t> _STLP_CALL
00064 get_temporary_buffer(ptrdiff_t __len, _Tp*) {
00065 return __get_temporary_buffer(__len, (_Tp*) 0);
00066 }
00067 # endif
00068 #endif
00069
00070 template <class _Tp>
00071 inline void _STLP_CALL return_temporary_buffer(_Tp* __p) {
00072
00073 free((char*)__p);
00074 }
00075
00076 template <class _ForwardIterator, class _Tp>
00077 class _Temporary_buffer {
00078 private:
00079 ptrdiff_t _M_original_len;
00080 ptrdiff_t _M_len;
00081 _Tp* _M_buffer;
00082
00083 void _M_allocate_buffer() {
00084 _M_original_len = _M_len;
00085 _M_buffer = 0;
00086
00087 if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp)))
00088 _M_len = INT_MAX / sizeof(_Tp);
00089
00090 while (_M_len > 0) {
00091 _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp));
00092 if (_M_buffer)
00093 break;
00094 _M_len /= 2;
00095 }
00096 }
00097
00098 void _M_initialize_buffer(const _Tp&, const __true_type&) {}
00099 void _M_initialize_buffer(const _Tp& val, const __false_type&) {
00100 uninitialized_fill_n(_M_buffer, _M_len, val);
00101 }
00102
00103 public:
00104 ptrdiff_t size() const { return _M_len; }
00105 ptrdiff_t requested_size() const { return _M_original_len; }
00106 _Tp* begin() { return _M_buffer; }
00107 _Tp* end() { return _M_buffer + _M_len; }
00108
00109 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) {
00110
00111 # if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION < 730
00112 typedef typename __type_traits<_Tp>::is_POD_type _Trivial;
00113 # else
00114 typedef typename __type_traits<_Tp>::has_trivial_default_constructor _Trivial;
00115 # endif
00116 _STLP_TRY {
00117 _M_len = distance(__first, __last);
00118 _M_allocate_buffer();
00119 if (_M_len > 0)
00120 _M_initialize_buffer(*__first, _Trivial());
00121 }
00122 _STLP_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0);
00123 }
00124
00125 ~_Temporary_buffer() {
00126 _Destroy(_M_buffer, _M_buffer + _M_len);
00127 free(_M_buffer);
00128 }
00129
00130 private:
00131
00132 _Temporary_buffer(const _Temporary_buffer<_ForwardIterator, _Tp>&) {}
00133 void operator=(const _Temporary_buffer<_ForwardIterator, _Tp>&) {}
00134 };
00135
00136 # ifndef _STLP_NO_EXTENSIONS
00137
00138
00139
00140 template <class _ForwardIterator,
00141 class _Tp
00142 #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
00143 = typename iterator_traits<_ForwardIterator>::value_type
00144 #endif
00145 >
00146 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
00147 {
00148 temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
00149 : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {}
00150 ~temporary_buffer() {}
00151 };
00152
00153 # endif
00154
00155 _STLP_END_NAMESPACE
00156
00157 # ifndef _STLP_LINK_TIME_INSTANTIATION
00158 # include <stl/_tempbuf.c>
00159 # endif
00160
00161 #endif
00162
00163
00164
00165