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>
66 * This is a helper function. The unused second parameter exists to
67 * permit the real get_temporary_buffer to use template parameter deduction.
72 __get_temporary_buffer(ptrdiff_t __len
, _Tp
*)
74 if (__len
> ptrdiff_t(INT_MAX
/ sizeof(_Tp
)))
75 __len
= INT_MAX
/ sizeof(_Tp
);
78 _Tp
* __tmp
= (_Tp
*) std::malloc((std::size_t)__len
* sizeof(_Tp
));
80 return pair
<_Tp
*, ptrdiff_t>(__tmp
, __len
);
84 return pair
<_Tp
*, ptrdiff_t>((_Tp
*)0, 0);
88 * @brief This is a mostly-useless wrapper around malloc().
89 * @param len The number of objects of type Tp.
90 * @return See full description.
92 * Reinventing the wheel, but this time with prettier spokes!
94 * This function tries to obtain storage for @c len adjacent Tp objects.
95 * The objects themselves are not constructed, of course. A pair<> is
96 * returned containing "the buffer s address and capacity (in the units of
97 * sizeof(Tp)), or a pair of 0 values if no storage can be obtained."
98 * Note that the capacity obtained may be less than that requested if the
99 * memory is unavailable; you should compare len with the .second return
103 inline pair
<_Tp
*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len
) {
104 return __get_temporary_buffer(__len
, (_Tp
*) 0);
108 * @brief The companion to get_temporary_buffer().
109 * @param p A buffer previously allocated by get_temporary_buffer.
112 * Frees the memory pointed to by p.
115 void return_temporary_buffer(_Tp
* __p
) {
120 template <class _Tp1
>
124 auto_ptr_ref(_Tp1
* __p
) : _M_ptr(__p
) {}
128 * A simple smart pointer providing strict ownership semantics. (More later.)
137 typedef _Tp element_type
;
139 explicit auto_ptr(_Tp
* __p
= 0) throw() : _M_ptr(__p
) {}
140 auto_ptr(auto_ptr
& __a
) throw() : _M_ptr(__a
.release()) {}
142 template <class _Tp1
> auto_ptr(auto_ptr
<_Tp1
>& __a
) throw()
143 : _M_ptr(__a
.release()) {}
145 auto_ptr
& operator=(auto_ptr
& __a
) throw() {
146 reset(__a
.release());
150 template <class _Tp1
>
151 auto_ptr
& operator=(auto_ptr
<_Tp1
>& __a
) throw() {
152 reset(__a
.release());
156 // Note: The C++ standard says there is supposed to be an empty throw
157 // specification here, but omitting it is standard conforming. Its
158 // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
159 // this is prohibited.
160 ~auto_ptr() { delete _M_ptr
; }
162 _Tp
& operator*() const throw() {
165 _Tp
* operator->() const throw() {
168 _Tp
* get() const throw() {
171 _Tp
* release() throw() {
176 void reset(_Tp
* __p
= 0) throw() {
184 auto_ptr(auto_ptr_ref
<_Tp
> __ref
) throw()
185 : _M_ptr(__ref
._M_ptr
) {}
187 auto_ptr
& operator=(auto_ptr_ref
<_Tp
> __ref
) throw() {
188 if (__ref
._M_ptr
!= this->get()) {
190 _M_ptr
= __ref
._M_ptr
;
195 template <class _Tp1
> operator auto_ptr_ref
<_Tp1
>() throw()
196 { return auto_ptr_ref
<_Tp
>(this->release()); }
197 template <class _Tp1
> operator auto_ptr
<_Tp1
>() throw()
198 { return auto_ptr
<_Tp1
>(this->release()); }
203 #endif /* _CPP_MEMORY */