memory

00001 // <memory> -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2004 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 * Copyright (c) 1997-1999 00032 * Silicon Graphics Computer Systems, Inc. 00033 * 00034 * Permission to use, copy, modify, distribute and sell this software 00035 * and its documentation for any purpose is hereby granted without fee, 00036 * provided that the above copyright notice appear in all copies and 00037 * that both that copyright notice and this permission notice appear 00038 * in supporting documentation. Silicon Graphics makes no 00039 * representations about the suitability of this software for any 00040 * purpose. It is provided "as is" without express or implied warranty. 00041 * 00042 */ 00043 00044 /** @file memory 00045 * This is a Standard C++ Library header. You should @c #include this header 00046 * in your programs, rather than any of the "st[dl]_*.h" implementation files. 00047 */ 00048 00049 #ifndef _GLIBCXX_MEMORY 00050 #define _GLIBCXX_MEMORY 1 00051 00052 #pragma GCC system_header 00053 00054 #include <bits/stl_algobase.h> 00055 #include <bits/allocator.h> 00056 #include <bits/stl_construct.h> 00057 #include <bits/stl_iterator_base_types.h> //for iterator_traits 00058 #include <bits/stl_uninitialized.h> 00059 #include <bits/stl_raw_storage_iter.h> 00060 #include <debug/debug.h> 00061 00062 namespace std 00063 { 00064 /** 00065 * @if maint 00066 * This is a helper function. The unused second parameter exists to 00067 * permit the real get_temporary_buffer to use template parameter deduction. 00068 * 00069 * XXX This should perhaps use the pool. 00070 * @endif 00071 */ 00072 template<typename _Tp> 00073 pair<_Tp*, ptrdiff_t> 00074 __get_temporary_buffer(ptrdiff_t __len, _Tp*) 00075 { 00076 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp))) 00077 __len = INT_MAX / sizeof(_Tp); 00078 00079 while (__len > 0) 00080 { 00081 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 00082 nothrow)); 00083 if (__tmp != 0) 00084 return pair<_Tp*, ptrdiff_t>(__tmp, __len); 00085 __len /= 2; 00086 } 00087 return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0); 00088 } 00089 00090 /** 00091 * @brief Allocates a temporary buffer. 00092 * @param len The number of objects of type Tp. 00093 * @return See full description. 00094 * 00095 * Reinventing the wheel, but this time with prettier spokes! 00096 * 00097 * This function tries to obtain storage for @c len adjacent Tp 00098 * objects. The objects themselves are not constructed, of course. 00099 * A pair<> is returned containing "the buffer s address and 00100 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if 00101 * no storage can be obtained." Note that the capacity obtained 00102 * may be less than that requested if the memory is unavailable; 00103 * you should compare len with the .second return value. 00104 * 00105 * Provides the nothrow exception guarantee. 00106 */ 00107 template<typename _Tp> 00108 inline pair<_Tp*,ptrdiff_t> 00109 get_temporary_buffer(ptrdiff_t __len) 00110 { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); } 00111 00112 /** 00113 * @brief The companion to get_temporary_buffer(). 00114 * @param p A buffer previously allocated by get_temporary_buffer. 00115 * @return None. 00116 * 00117 * Frees the memory pointed to by p. 00118 */ 00119 template<typename _Tp> 00120 void 00121 return_temporary_buffer(_Tp* __p) 00122 { ::operator delete(__p, nothrow); } 00123 00124 /** 00125 * A wrapper class to provide auto_ptr with reference semantics. 00126 * For example, an auto_ptr can be assigned (or constructed from) 00127 * the result of a function which returns an auto_ptr by value. 00128 * 00129 * All the auto_ptr_ref stuff should happen behind the scenes. 00130 */ 00131 template<typename _Tp1> 00132 struct auto_ptr_ref 00133 { 00134 _Tp1* _M_ptr; 00135 00136 explicit 00137 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { } 00138 }; 00139 00140 00141 /** 00142 * @brief A simple smart pointer providing strict ownership semantics. 00143 * 00144 * The Standard says: 00145 * <pre> 00146 * An @c auto_ptr owns the object it holds a pointer to. Copying 00147 * an @c auto_ptr copies the pointer and transfers ownership to the 00148 * destination. If more than one @c auto_ptr owns the same object 00149 * at the same time the behavior of the program is undefined. 00150 * 00151 * The uses of @c auto_ptr include providing temporary 00152 * exception-safety for dynamically allocated memory, passing 00153 * ownership of dynamically allocated memory to a function, and 00154 * returning dynamically allocated memory from a function. @c 00155 * auto_ptr does not meet the CopyConstructible and Assignable 00156 * requirements for Standard Library <a 00157 * href="tables.html#65">container</a> elements and thus 00158 * instantiating a Standard Library container with an @c auto_ptr 00159 * results in undefined behavior. 00160 * </pre> 00161 * Quoted from [20.4.5]/3. 00162 * 00163 * Good examples of what can and cannot be done with auto_ptr can 00164 * be found in the libstdc++ testsuite. 00165 * 00166 * @if maint 00167 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00168 * 127. auto_ptr<> conversion issues 00169 * These resolutions have all been incorporated. 00170 * @endif 00171 */ 00172 template<typename _Tp> 00173 class auto_ptr 00174 { 00175 private: 00176 _Tp* _M_ptr; 00177 00178 public: 00179 /// The pointed-to type. 00180 typedef _Tp element_type; 00181 00182 /** 00183 * @brief An %auto_ptr is usually constructed from a raw pointer. 00184 * @param p A pointer (defaults to NULL). 00185 * 00186 * This object now @e owns the object pointed to by @a p. 00187 */ 00188 explicit 00189 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { } 00190 00191 /** 00192 * @brief An %auto_ptr can be constructed from another %auto_ptr. 00193 * @param a Another %auto_ptr of the same type. 00194 * 00195 * This object now @e owns the object previously owned by @a a, 00196 * which has given up ownsership. 00197 */ 00198 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { } 00199 00200 /** 00201 * @brief An %auto_ptr can be constructed from another %auto_ptr. 00202 * @param a Another %auto_ptr of a different but related type. 00203 * 00204 * A pointer-to-Tp1 must be convertible to a 00205 * pointer-to-Tp/element_type. 00206 * 00207 * This object now @e owns the object previously owned by @a a, 00208 * which has given up ownsership. 00209 */ 00210 template<typename _Tp1> 00211 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { } 00212 00213 /** 00214 * @brief %auto_ptr assignment operator. 00215 * @param a Another %auto_ptr of the same type. 00216 * 00217 * This object now @e owns the object previously owned by @a a, 00218 * which has given up ownsership. The object that this one @e 00219 * used to own and track has been deleted. 00220 */ 00221 auto_ptr& 00222 operator=(auto_ptr& __a) throw() 00223 { 00224 reset(__a.release()); 00225 return *this; 00226 } 00227 00228 /** 00229 * @brief %auto_ptr assignment operator. 00230 * @param a Another %auto_ptr of a different but related type. 00231 * 00232 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type. 00233 * 00234 * This object now @e owns the object previously owned by @a a, 00235 * which has given up ownsership. The object that this one @e 00236 * used to own and track has been deleted. 00237 */ 00238 template<typename _Tp1> 00239 auto_ptr& 00240 operator=(auto_ptr<_Tp1>& __a) throw() 00241 { 00242 reset(__a.release()); 00243 return *this; 00244 } 00245 00246 /** 00247 * When the %auto_ptr goes out of scope, the object it owns is 00248 * deleted. If it no longer owns anything (i.e., @c get() is 00249 * @c NULL), then this has no effect. 00250 * 00251 * @if maint 00252 * The C++ standard says there is supposed to be an empty throw 00253 * specification here, but omitting it is standard conforming. Its 00254 * presence can be detected only if _Tp::~_Tp() throws, but this is 00255 * prohibited. [17.4.3.6]/2 00256 * @end maint 00257 */ 00258 ~auto_ptr() { delete _M_ptr; } 00259 00260 /** 00261 * @brief Smart pointer dereferencing. 00262 * 00263 * If this %auto_ptr no longer owns anything, then this 00264 * operation will crash. (For a smart pointer, "no longer owns 00265 * anything" is the same as being a null pointer, and you know 00266 * what happens when you dereference one of those...) 00267 */ 00268 element_type& 00269 operator*() const throw() 00270 { 00271 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0); 00272 return *_M_ptr; 00273 } 00274 00275 /** 00276 * @brief Smart pointer dereferencing. 00277 * 00278 * This returns the pointer itself, which the language then will 00279 * automatically cause to be dereferenced. 00280 */ 00281 element_type* 00282 operator->() const throw() 00283 { 00284 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0); 00285 return _M_ptr; 00286 } 00287 00288 /** 00289 * @brief Bypassing the smart pointer. 00290 * @return The raw pointer being managed. 00291 * 00292 * You can get a copy of the pointer that this object owns, for 00293 * situations such as passing to a function which only accepts 00294 * a raw pointer. 00295 * 00296 * @note This %auto_ptr still owns the memory. 00297 */ 00298 element_type* 00299 get() const throw() { return _M_ptr; } 00300 00301 /** 00302 * @brief Bypassing the smart pointer. 00303 * @return The raw pointer being managed. 00304 * 00305 * You can get a copy of the pointer that this object owns, for 00306 * situations such as passing to a function which only accepts 00307 * a raw pointer. 00308 * 00309 * @note This %auto_ptr no longer owns the memory. When this object 00310 * goes out of scope, nothing will happen. 00311 */ 00312 element_type* 00313 release() throw() 00314 { 00315 element_type* __tmp = _M_ptr; 00316 _M_ptr = 0; 00317 return __tmp; 00318 } 00319 00320 /** 00321 * @brief Forcibly deletes the managed object. 00322 * @param p A pointer (defaults to NULL). 00323 * 00324 * This object now @e owns the object pointed to by @a p. The 00325 * previous object has been deleted. 00326 */ 00327 void 00328 reset(element_type* __p = 0) throw() 00329 { 00330 if (__p != _M_ptr) 00331 { 00332 delete _M_ptr; 00333 _M_ptr = __p; 00334 } 00335 } 00336 00337 /** @{ 00338 * @brief Automatic conversions 00339 * 00340 * These operations convert an %auto_ptr into and from an auto_ptr_ref 00341 * automatically as needed. This allows constructs such as 00342 * @code 00343 * auto_ptr<Derived> func_returning_auto_ptr(.....); 00344 * ... 00345 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....); 00346 * @endcode 00347 */ 00348 auto_ptr(auto_ptr_ref<element_type> __ref) throw() 00349 : _M_ptr(__ref._M_ptr) { } 00350 00351 auto_ptr& 00352 operator=(auto_ptr_ref<element_type> __ref) throw() 00353 { 00354 if (__ref._M_ptr != this->get()) 00355 { 00356 delete _M_ptr; 00357 _M_ptr = __ref._M_ptr; 00358 } 00359 return *this; 00360 } 00361 00362 template<typename _Tp1> 00363 operator auto_ptr_ref<_Tp1>() throw() 00364 { return auto_ptr_ref<_Tp1>(this->release()); } 00365 00366 template<typename _Tp1> 00367 operator auto_ptr<_Tp1>() throw() 00368 { return auto_ptr<_Tp1>(this->release()); } 00369 /** @} */ 00370 }; 00371 } // namespace std 00372 00373 #endif /* _GLIBCXX_MEMORY */

Generated on Tue Sep 7 10:05:09 2004 for libstdc++-v3 Source by doxygen 1.3.8