1 // <memory> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 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.
49 #ifndef _GLIBCXX_MEMORY
50 #define _GLIBCXX_MEMORY 1
52 #pragma GCC system_header
54 #include <bits/stl_algobase.h>
55 #include <bits/allocator.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>
60 #include <debug/debug.h>
66 * This is a helper function. The unused second parameter exists to
67 * permit the real get_temporary_buffer to use template parameter deduction.
69 * XXX This should perhaps use the pool.
72 template<typename _Tp
>
74 __get_temporary_buffer(ptrdiff_t __len
, _Tp
*)
76 if (__len
> ptrdiff_t(INT_MAX
/ sizeof(_Tp
)))
77 __len
= INT_MAX
/ sizeof(_Tp
);
81 _Tp
* __tmp
= static_cast<_Tp
*>(::operator new(__len
* sizeof(_Tp
),
84 return pair
<_Tp
*, ptrdiff_t>(__tmp
, __len
);
87 return pair
<_Tp
*, ptrdiff_t>(static_cast<_Tp
*>(0), 0);
91 * @brief Allocates a temporary buffer.
92 * @param len The number of objects of type Tp.
93 * @return See full description.
95 * Reinventing the wheel, but this time with prettier spokes!
97 * This function tries to obtain storage for @c len adjacent Tp
98 * objects. The objects themselves are not constructed, of course.
99 * A pair<> is returned containing "the buffer s address and
100 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if
101 * no storage can be obtained." Note that the capacity obtained
102 * may be less than that requested if the memory is unavailable;
103 * you should compare len with the .second return value.
105 * Provides the nothrow exception guarantee.
107 template<typename _Tp
>
108 inline pair
<_Tp
*,ptrdiff_t>
109 get_temporary_buffer(ptrdiff_t __len
)
110 { return std::__get_temporary_buffer(__len
, static_cast<_Tp
*>(0)); }
113 * @brief The companion to get_temporary_buffer().
114 * @param p A buffer previously allocated by get_temporary_buffer.
117 * Frees the memory pointed to by p.
119 template<typename _Tp
>
121 return_temporary_buffer(_Tp
* __p
)
122 { ::operator delete(__p
, nothrow
); }
125 * A wrapper class to provide auto_ptr with reference semantics.
126 * For example, an auto_ptr can be assigned (or constructed from)
127 * the result of a function which returns an auto_ptr by value.
129 * All the auto_ptr_ref stuff should happen behind the scenes.
131 template<typename _Tp1
>
137 auto_ptr_ref(_Tp1
* __p
): _M_ptr(__p
) { }
142 * @brief A simple smart pointer providing strict ownership semantics.
146 * An @c auto_ptr owns the object it holds a pointer to. Copying
147 * an @c auto_ptr copies the pointer and transfers ownership to the
148 * destination. If more than one @c auto_ptr owns the same object
149 * at the same time the behavior of the program is undefined.
151 * The uses of @c auto_ptr include providing temporary
152 * exception-safety for dynamically allocated memory, passing
153 * ownership of dynamically allocated memory to a function, and
154 * returning dynamically allocated memory from a function. @c
155 * auto_ptr does not meet the CopyConstructible and Assignable
156 * requirements for Standard Library <a
157 * href="tables.html#65">container</a> elements and thus
158 * instantiating a Standard Library container with an @c auto_ptr
159 * results in undefined behavior.
161 * Quoted from [20.4.5]/3.
163 * Good examples of what can and cannot be done with auto_ptr can
164 * be found in the libstdc++ testsuite.
167 * _GLIBCXX_RESOLVE_LIB_DEFECTS
168 * 127. auto_ptr<> conversion issues
169 * These resolutions have all been incorporated.
172 template<typename _Tp
>
179 /// The pointed-to type.
180 typedef _Tp element_type
;
183 * @brief An %auto_ptr is usually constructed from a raw pointer.
184 * @param p A pointer (defaults to NULL).
186 * This object now @e owns the object pointed to by @a p.
189 auto_ptr(element_type
* __p
= 0) throw() : _M_ptr(__p
) { }
192 * @brief An %auto_ptr can be constructed from another %auto_ptr.
193 * @param a Another %auto_ptr of the same type.
195 * This object now @e owns the object previously owned by @a a,
196 * which has given up ownsership.
198 auto_ptr(auto_ptr
& __a
) throw() : _M_ptr(__a
.release()) { }
201 * @brief An %auto_ptr can be constructed from another %auto_ptr.
202 * @param a Another %auto_ptr of a different but related type.
204 * A pointer-to-Tp1 must be convertible to a
205 * pointer-to-Tp/element_type.
207 * This object now @e owns the object previously owned by @a a,
208 * which has given up ownsership.
210 template<typename _Tp1
>
211 auto_ptr(auto_ptr
<_Tp1
>& __a
) throw() : _M_ptr(__a
.release()) { }
214 * @brief %auto_ptr assignment operator.
215 * @param a Another %auto_ptr of the same type.
217 * This object now @e owns the object previously owned by @a a,
218 * which has given up ownsership. The object that this one @e
219 * used to own and track has been deleted.
222 operator=(auto_ptr
& __a
) throw()
224 reset(__a
.release());
229 * @brief %auto_ptr assignment operator.
230 * @param a Another %auto_ptr of a different but related type.
232 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
234 * This object now @e owns the object previously owned by @a a,
235 * which has given up ownsership. The object that this one @e
236 * used to own and track has been deleted.
238 template<typename _Tp1
>
240 operator=(auto_ptr
<_Tp1
>& __a
) throw()
242 reset(__a
.release());
247 * When the %auto_ptr goes out of scope, the object it owns is
248 * deleted. If it no longer owns anything (i.e., @c get() is
249 * @c NULL), then this has no effect.
252 * The C++ standard says there is supposed to be an empty throw
253 * specification here, but omitting it is standard conforming. Its
254 * presence can be detected only if _Tp::~_Tp() throws, but this is
255 * prohibited. [17.4.3.6]/2
258 ~auto_ptr() { delete _M_ptr
; }
261 * @brief Smart pointer dereferencing.
263 * If this %auto_ptr no longer owns anything, then this
264 * operation will crash. (For a smart pointer, "no longer owns
265 * anything" is the same as being a null pointer, and you know
266 * what happens when you dereference one of those...)
269 operator*() const throw()
271 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
276 * @brief Smart pointer dereferencing.
278 * This returns the pointer itself, which the language then will
279 * automatically cause to be dereferenced.
282 operator->() const throw()
284 _GLIBCXX_DEBUG_ASSERT(_M_ptr
!= 0);
289 * @brief Bypassing the smart pointer.
290 * @return The raw pointer being managed.
292 * You can get a copy of the pointer that this object owns, for
293 * situations such as passing to a function which only accepts
296 * @note This %auto_ptr still owns the memory.
299 get() const throw() { return _M_ptr
; }
302 * @brief Bypassing the smart pointer.
303 * @return The raw pointer being managed.
305 * You can get a copy of the pointer that this object owns, for
306 * situations such as passing to a function which only accepts
309 * @note This %auto_ptr no longer owns the memory. When this object
310 * goes out of scope, nothing will happen.
315 element_type
* __tmp
= _M_ptr
;
321 * @brief Forcibly deletes the managed object.
322 * @param p A pointer (defaults to NULL).
324 * This object now @e owns the object pointed to by @a p. The
325 * previous object has been deleted.
328 reset(element_type
* __p
= 0) throw()
338 * @brief Automatic conversions
340 * These operations convert an %auto_ptr into and from an auto_ptr_ref
341 * automatically as needed. This allows constructs such as
343 * auto_ptr<Derived> func_returning_auto_ptr(.....);
345 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
348 auto_ptr(auto_ptr_ref
<element_type
> __ref
) throw()
349 : _M_ptr(__ref
._M_ptr
) { }
352 operator=(auto_ptr_ref
<element_type
> __ref
) throw()
354 if (__ref
._M_ptr
!= this->get())
357 _M_ptr
= __ref
._M_ptr
;
362 template<typename _Tp1
>
363 operator auto_ptr_ref
<_Tp1
>() throw()
364 { return auto_ptr_ref
<_Tp1
>(this->release()); }
366 template<typename _Tp1
>
367 operator auto_ptr
<_Tp1
>() throw()
368 { return auto_ptr
<_Tp1
>(this->release()); }
373 #endif /* _GLIBCXX_MEMORY */