Whole document tree stl_tempbuf.hGo to the documentation of this file.00001 // Temporary buffer implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1994 00033 * Hewlett-Packard Company 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Hewlett-Packard Company makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 * 00043 * 00044 * Copyright (c) 1996,1997 00045 * Silicon Graphics Computer Systems, Inc. 00046 * 00047 * Permission to use, copy, modify, distribute and sell this software 00048 * and its documentation for any purpose is hereby granted without fee, 00049 * provided that the above copyright notice appear in all copies and 00050 * that both that copyright notice and this permission notice appear 00051 * in supporting documentation. Silicon Graphics makes no 00052 * representations about the suitability of this software for any 00053 * purpose. It is provided "as is" without express or implied warranty. 00054 */ 00055 00056 /* NOTE: This is an internal header file, included by other STL headers. 00057 * You should not attempt to use it directly. 00058 */ 00059 00060 #ifndef __SGI_STL_INTERNAL_TEMPBUF_H 00061 #define __SGI_STL_INTERNAL_TEMPBUF_H 00062 00063 namespace std 00064 { 00065 00066 template <class _Tp> 00067 pair<_Tp*, ptrdiff_t> 00068 __get_temporary_buffer(ptrdiff_t __len, _Tp*) 00069 { 00070 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp))) 00071 __len = INT_MAX / sizeof(_Tp); 00072 00073 while (__len > 0) { 00074 _Tp* __tmp = (_Tp*) malloc((size_t)__len * sizeof(_Tp)); 00075 if (__tmp != 0) 00076 return pair<_Tp*, ptrdiff_t>(__tmp, __len); 00077 __len /= 2; 00078 } 00079 00080 return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0); 00081 } 00082 00083 template <class _Tp> 00084 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) { 00085 return __get_temporary_buffer(__len, (_Tp*) 0); 00086 } 00087 00088 // This overload is not required by the standard; it is an extension. 00089 // It is supported for backward compatibility with the HP STL, and 00090 // because not all compilers support the language feature (explicit 00091 // function template arguments) that is required for the standard 00092 // version of get_temporary_buffer. 00093 template <class _Tp> 00094 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len, _Tp*) { 00095 return __get_temporary_buffer(__len, (_Tp*) 0); 00096 } 00097 00098 template <class _Tp> 00099 void return_temporary_buffer(_Tp* __p) { 00100 free(__p); 00101 } 00102 00103 template <class _ForwardIterator, class _Tp> 00104 class _Temporary_buffer { 00105 private: 00106 ptrdiff_t _M_original_len; 00107 ptrdiff_t _M_len; 00108 _Tp* _M_buffer; 00109 00110 void _M_allocate_buffer() { 00111 _M_original_len = _M_len; 00112 _M_buffer = 0; 00113 00114 if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp))) 00115 _M_len = INT_MAX / sizeof(_Tp); 00116 00117 while (_M_len > 0) { 00118 _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp)); 00119 if (_M_buffer) 00120 break; 00121 _M_len /= 2; 00122 } 00123 } 00124 00125 void _M_initialize_buffer(const _Tp&, __true_type) {} 00126 void _M_initialize_buffer(const _Tp& val, __false_type) { 00127 uninitialized_fill_n(_M_buffer, _M_len, val); 00128 } 00129 00130 public: 00131 ptrdiff_t size() const { return _M_len; } 00132 ptrdiff_t requested_size() const { return _M_original_len; } 00133 _Tp* begin() { return _M_buffer; } 00134 _Tp* end() { return _M_buffer + _M_len; } 00135 00136 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) { 00137 // Workaround for a __type_traits bug in the pre-7.3 compiler. 00138 typedef typename __type_traits<_Tp>::has_trivial_default_constructor 00139 _Trivial; 00140 00141 __STL_TRY { 00142 _M_len = 0; 00143 distance(__first, __last, _M_len); 00144 _M_allocate_buffer(); 00145 if (_M_len > 0) 00146 _M_initialize_buffer(*__first, _Trivial()); 00147 } 00148 __STL_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0); 00149 } 00150 00151 ~_Temporary_buffer() { 00152 destroy(_M_buffer, _M_buffer + _M_len); 00153 free(_M_buffer); 00154 } 00155 00156 private: 00157 // Disable copy constructor and assignment operator. 00158 _Temporary_buffer(const _Temporary_buffer&) {} 00159 void operator=(const _Temporary_buffer&) {} 00160 }; 00161 00162 // Class temporary_buffer is not part of the standard. It is an extension. 00163 00164 template <class _ForwardIterator, 00165 class _Tp 00166 = typename iterator_traits<_ForwardIterator>::value_type 00167 > 00168 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp> 00169 { 00170 temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) 00171 : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {} 00172 ~temporary_buffer() {} 00173 }; 00174 00175 } // namespace std 00176 00177 #endif /* __SGI_STL_INTERNAL_TEMPBUF_H */ 00178 00179 // Local Variables: 00180 // mode:C++ 00181 // End: Generated on Mon Apr 8 03:11:44 2002 for libstdc++-v3 Source by ![]() |