1 // <memory> -*- C++ -*-
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * Copyright (c) 1997-1999
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
45 * This is a Standard C++ Library header. You should @c #include this header
46 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
52 #pragma GCC system_header
54 #include <bits/stl_algobase.h>
55 #include <bits/stl_alloc.h>
56 #include <bits/stl_construct.h>
57 #include <bits/stl_iterator_base_types.h> //for iterator_traits
58 #include <bits/stl_uninitialized.h>
59 #include <bits/stl_raw_storage_iter.h>
65 * This is a helper function. The unused second parameter exists to
66 * permit the real get_temporary_buffer to use template parameter deduction.
68 * XXX This should perhaps use the pool.
71 template<typename _Tp>
73 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
75 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
76 __len = INT_MAX / sizeof(_Tp);
80 _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
82 return pair<_Tp*, ptrdiff_t>(__tmp, __len);
85 return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
89 * @brief This is a mostly-useless wrapper around malloc().
90 * @param len The number of objects of type Tp.
91 * @return See full description.
93 * Reinventing the wheel, but this time with prettier spokes!
95 * This function tries to obtain storage for @c len adjacent Tp objects.
96 * The objects themselves are not constructed, of course. A pair<> is
97 * returned containing "the buffer s address and capacity (in the units of
98 * sizeof(Tp)), or a pair of 0 values if no storage can be obtained."
99 * Note that the capacity obtained may be less than that requested if the
100 * memory is unavailable; you should compare len with the .second return
103 template<typename _Tp>
104 inline pair<_Tp*,ptrdiff_t>
105 get_temporary_buffer(ptrdiff_t __len)
106 { return __get_temporary_buffer(__len, (_Tp*) 0); }
109 * @brief The companion to get_temporary_buffer().
110 * @param p A buffer previously allocated by get_temporary_buffer.
113 * Frees the memory pointed to by p.
115 template<typename _Tp>
117 return_temporary_buffer(_Tp* __p)
121 * A wrapper class to provide auto_ptr with reference semantics. For
122 * example, an auto_ptr can be assigned (or constructed from) the result of
123 * a function which returns an auto_ptr by value.
125 * All the auto_ptr_ref stuff should happen behind the scenes.
127 template<typename _Tp1>
133 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
138 * @brief A simple smart pointer providing strict ownership semantics.
142 * An @c auto_ptr owns the object it holds a pointer to. Copying an
143 * @c auto_ptr copies the pointer and transfers ownership to the destination.
144 * If more than one @c auto_ptr owns the same object at the same time the
145 * behavior of the program is undefined.
147 * The uses of @c auto_ptr include providing temporary exception-safety for
148 * dynamically allocated memory, passing ownership of dynamically allocated
149 * memory to a function, and returning dynamically allocated memory from a
150 * function. @c auto_ptr does not meet the CopyConstructible and Assignable
151 * requirements for Standard Library <a href="tables.html#65">container</a>
152 * elements and thus instantiating a Standard Library container with an
153 * @c auto_ptr results in undefined behavior.
155 * Quoted from [20.4.5]/3.
157 * Good examples of what can and cannot be done with auto_ptr can be found
158 * in the libstdc++ testsuite.
161 * _GLIBCPP_RESOLVE_LIB_DEFECTS
162 * 127. auto_ptr<> conversion issues
163 * These resolutions have all been incorporated.
166 template<typename _Tp>
173 /// The pointed-to type.
174 typedef _Tp element_type;
177 * @brief An %auto_ptr is usually constructed from a raw pointer.
178 * @param p A pointer (defaults to NULL).
180 * This object now @e owns the object pointed to by @a p.
183 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
186 * @brief An %auto_ptr can be constructed from another %auto_ptr.
187 * @param a Another %auto_ptr of the same type.
189 * This object now @e owns the object previously owned by @a a,
190 * which has given up ownsership.
192 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
195 * @brief An %auto_ptr can be constructed from another %auto_ptr.
196 * @param a Another %auto_ptr of a different but related type.
198 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
200 * This object now @e owns the object previously owned by @a a,
201 * which has given up ownsership.
203 template<typename _Tp1>
204 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
207 * @brief %auto_ptr assignment operator.
208 * @param a Another %auto_ptr of the same type.
210 * This object now @e owns the object previously owned by @a a,
211 * which has given up ownsership. The object that this one @e
212 * used to own and track has been deleted.
215 operator=(auto_ptr& __a) throw()
217 reset(__a.release());
222 * @brief %auto_ptr assignment operator.
223 * @param a Another %auto_ptr of a different but related type.
225 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
227 * This object now @e owns the object previously owned by @a a,
228 * which has given up ownsership. The object that this one @e
229 * used to own and track has been deleted.
231 template<typename _Tp1>
233 operator=(auto_ptr<_Tp1>& __a) throw()
235 reset(__a.release());
240 * When the %auto_ptr goes out of scope, the object it owns is deleted.
241 * If it no longer owns anything (i.e., @c get() is @c NULL), then this
245 * The C++ standard says there is supposed to be an empty throw
246 * specification here, but omitting it is standard conforming. Its
247 * presence can be detected only if _Tp::~_Tp() throws, but this is
248 * prohibited. [17.4.3.6]/2
251 ~auto_ptr() { delete _M_ptr; }
254 * @brief Smart pointer dereferencing.
256 * If this %auto_ptr no longer owns anything, then this
257 * operation will crash. (For a smart pointer, "no longer owns
258 * anything" is the same as being a null pointer, and you know
259 * what happens when you dereference one of those...)
262 operator*() const throw() { return *_M_ptr; }
265 * @brief Smart pointer dereferencing.
267 * This returns the pointer itself, which the language then will
268 * automatically cause to be dereferenced.
271 operator->() const throw() { return _M_ptr; }
274 * @brief Bypassing the smart pointer.
275 * @return The raw pointer being managed.
277 * You can get a copy of the pointer that this object owns, for
278 * situations such as passing to a function which only accepts a raw
281 * @note This %auto_ptr still owns the memory.
284 get() const throw() { return _M_ptr; }
287 * @brief Bypassing the smart pointer.
288 * @return The raw pointer being managed.
290 * You can get a copy of the pointer that this object owns, for
291 * situations such as passing to a function which only accepts a raw
294 * @note This %auto_ptr no longer owns the memory. When this object
295 * goes out of scope, nothing will happen.
300 element_type* __tmp = _M_ptr;
306 * @brief Forcibly deletes the managed object.
307 * @param p A pointer (defaults to NULL).
309 * This object now @e owns the object pointed to by @a p. The previous
310 * object has been deleted.
313 reset(element_type* __p = 0) throw()
323 * @brief Automatic conversions
325 * These operations convert an %auto_ptr into and from an auto_ptr_ref
326 * automatically as needed. This allows constructs such as
328 * auto_ptr<Derived> func_returning_auto_ptr(.....);
330 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
333 auto_ptr(auto_ptr_ref<element_type> __ref) throw()
334 : _M_ptr(__ref._M_ptr) { }
337 operator=(auto_ptr_ref<element_type> __ref) throw()
339 if (__ref._M_ptr != this->get())
342 _M_ptr = __ref._M_ptr;
347 template<typename _Tp1>
348 operator auto_ptr_ref<_Tp1>() throw()
349 { return auto_ptr_ref<_Tp1>(this->release()); }
351 template<typename _Tp1>
352 operator auto_ptr<_Tp1>() throw()
353 { return auto_ptr<_Tp1>(this->release()); }