1 // <memory> -*- C++ -*-
2 
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10 
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.
15 
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,
19 // USA.
20 
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.
29 
30 /*
31  * Copyright (c) 1997-1999
32  * Silicon Graphics Computer Systems, Inc.
33  *
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.
41  *
42  */
43 
44 /** @file memory
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.
47  */
48 
49 #ifndef _CPP_MEMORY
50 #define _CPP_MEMORY 1
51 
52 #pragma GCC system_header
53 
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>
60 
61 namespace std
62 {
63 
64   /**
65    *  @if maint
66    *  This is a helper function.  The unused second parameter exists to
67    *  permit the real get_temporary_buffer to use template parameter deduction.
68    *  @endif
69   */
70   template <class _Tp>
71   pair<_Tp*, ptrdiff_t>
72   __get_temporary_buffer(ptrdiff_t __len, _Tp*)
73   {
74     if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
75       __len = INT_MAX / sizeof(_Tp);
76 
77     while (__len > 0) {
78       _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
79       if (__tmp != 0)
80 	return pair<_Tp*, ptrdiff_t>(__tmp, __len);
81       __len /= 2;
82     }
83 
84     return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
85   }
86 
87   /**
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.
91    *
92    *  Reinventing the wheel, but this time with prettier spokes!
93    *
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
100    *  value.
101   */
102   template <class _Tp>
103   inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) {
104     return __get_temporary_buffer(__len, (_Tp*) 0);
105   }
106 
107   /**
108    *  @brief The companion to get_temporary_buffer().
109    *  @param  p  A buffer previously allocated by get_temporary_buffer.
110    *  @return   None.
111    *
112    *  Frees the memory pointed to by p.
113    */
114   template <class _Tp>
115   void return_temporary_buffer(_Tp* __p) {
116     std::free(__p);
117   }
118 
119 
120 template <class _Tp1>
121   struct auto_ptr_ref
122 {
123    _Tp1* _M_ptr;
124    auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
125 };
126 
127 /**
128  *  A simple smart pointer providing strict ownership semantics.  (More later.)
129 */
130 template <class _Tp>
131   class auto_ptr
132 {
133 private:
134   _Tp* _M_ptr;
135 
136 public:
137   typedef _Tp element_type;
138 
139   explicit auto_ptr(_Tp* __p = 0) throw() : _M_ptr(__p) {}
140   auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) {}
141 
142   template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) throw()
143     : _M_ptr(__a.release()) {}
144 
145   auto_ptr& operator=(auto_ptr& __a) throw() {
146     reset(__a.release());
147     return *this;
148   }
149 
150   template <class _Tp1>
151   auto_ptr& operator=(auto_ptr<_Tp1>& __a) throw() {
152     reset(__a.release());
153     return *this;
154   }
155 
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; }
161 
162   _Tp& operator*() const throw() {
163     return *_M_ptr;
164   }
165   _Tp* operator->() const throw() {
166     return _M_ptr;
167   }
168   _Tp* get() const throw() {
169     return _M_ptr;
170   }
171   _Tp* release() throw() {
172     _Tp* __tmp = _M_ptr;
173     _M_ptr = 0;
174     return __tmp;
175   }
176   void reset(_Tp* __p = 0) throw() {
177     if (__p != _M_ptr) {
178       delete _M_ptr;
179       _M_ptr = __p;
180     }
181   }
182 
183 public:
184   auto_ptr(auto_ptr_ref<_Tp> __ref) throw()
185     : _M_ptr(__ref._M_ptr) {}
186 
187   auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) throw() {
188     if (__ref._M_ptr != this->get()) {
189       delete _M_ptr;
190       _M_ptr = __ref._M_ptr;
191     }
192     return *this;
193   }
194 
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()); }
199 };
200 
201 } // namespace std
202 
203 #endif /* _CPP_MEMORY */
204 
205