std_memory.h revision 132720
1// <memory> -*- C++ -*-
2
3// Copyright (C) 2001, 2002, 2004 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 _GLIBCXX_MEMORY
50#define _GLIBCXX_MEMORY 1
51
52#pragma GCC system_header
53
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>
61
62namespace std
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   *
69   *  XXX This should perhaps use the pool.
70   *  @endif
71   */
72  template<typename _Tp>
73    pair<_Tp*, ptrdiff_t>
74    __get_temporary_buffer(ptrdiff_t __len, _Tp*)
75    {
76      if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
77	__len = INT_MAX / sizeof(_Tp);
78
79      while (__len > 0)
80	{
81	  _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
82							nothrow));
83	  if (__tmp != 0)
84	    return pair<_Tp*, ptrdiff_t>(__tmp, __len);
85	  __len /= 2;
86	}
87      return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
88    }
89
90  /**
91   *  @brief Allocates a temporary buffer.
92   *  @param  len  The number of objects of type Tp.
93   *  @return See full description.
94   *
95   *  Reinventing the wheel, but this time with prettier spokes!
96   *
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.
104   *
105   * Provides the nothrow exception guarantee.
106   */
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)); }
111
112  /**
113   *  @brief The companion to get_temporary_buffer().
114   *  @param  p  A buffer previously allocated by get_temporary_buffer.
115   *  @return   None.
116   *
117   *  Frees the memory pointed to by p.
118   */
119  template<typename _Tp>
120    void
121    return_temporary_buffer(_Tp* __p)
122    { ::operator delete(__p, nothrow); }
123
124  /**
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.
128   *
129   *  All the auto_ptr_ref stuff should happen behind the scenes.
130   */
131  template<typename _Tp1>
132    struct auto_ptr_ref
133    {
134      _Tp1* _M_ptr;
135
136      explicit
137      auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
138    };
139
140
141  /**
142   *  @brief  A simple smart pointer providing strict ownership semantics.
143   *
144   *  The Standard says:
145   *  <pre>
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.
150   *
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.
160   *  </pre>
161   *  Quoted from [20.4.5]/3.
162   *
163   *  Good examples of what can and cannot be done with auto_ptr can
164   *  be found in the libstdc++ testsuite.
165   *
166   *  @if maint
167   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
168   *  127.  auto_ptr<> conversion issues
169   *  These resolutions have all been incorporated.
170   *  @endif
171   */
172  template<typename _Tp>
173    class auto_ptr
174    {
175    private:
176      _Tp* _M_ptr;
177
178    public:
179      /// The pointed-to type.
180      typedef _Tp element_type;
181
182      /**
183       *  @brief  An %auto_ptr is usually constructed from a raw pointer.
184       *  @param  p  A pointer (defaults to NULL).
185       *
186       *  This object now @e owns the object pointed to by @a p.
187       */
188      explicit
189      auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
190
191      /**
192       *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
193       *  @param  a  Another %auto_ptr of the same type.
194       *
195       *  This object now @e owns the object previously owned by @a a,
196       *  which has given up ownsership.
197       */
198      auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
199
200      /**
201       *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
202       *  @param  a  Another %auto_ptr of a different but related type.
203       *
204       *  A pointer-to-Tp1 must be convertible to a
205       *  pointer-to-Tp/element_type.
206       *
207       *  This object now @e owns the object previously owned by @a a,
208       *  which has given up ownsership.
209       */
210      template<typename _Tp1>
211        auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
212
213      /**
214       *  @brief  %auto_ptr assignment operator.
215       *  @param  a  Another %auto_ptr of the same type.
216       *
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.
220       */
221      auto_ptr&
222      operator=(auto_ptr& __a) throw()
223      {
224	reset(__a.release());
225	return *this;
226      }
227
228      /**
229       *  @brief  %auto_ptr assignment operator.
230       *  @param  a  Another %auto_ptr of a different but related type.
231       *
232       *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
233       *
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.
237       */
238      template<typename _Tp1>
239        auto_ptr&
240        operator=(auto_ptr<_Tp1>& __a) throw()
241        {
242	  reset(__a.release());
243	  return *this;
244	}
245
246      /**
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.
250       *
251       *  @if maint
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
256       *  @end maint
257       */
258      ~auto_ptr() { delete _M_ptr; }
259
260      /**
261       *  @brief  Smart pointer dereferencing.
262       *
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...)
267       */
268      element_type&
269      operator*() const throw()
270      {
271	_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
272	return *_M_ptr;
273      }
274
275      /**
276       *  @brief  Smart pointer dereferencing.
277       *
278       *  This returns the pointer itself, which the language then will
279       *  automatically cause to be dereferenced.
280       */
281      element_type*
282      operator->() const throw()
283      {
284	_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
285	return _M_ptr;
286      }
287
288      /**
289       *  @brief  Bypassing the smart pointer.
290       *  @return  The raw pointer being managed.
291       *
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
294       *  a raw pointer.
295       *
296       *  @note  This %auto_ptr still owns the memory.
297       */
298      element_type*
299      get() const throw() { return _M_ptr; }
300
301      /**
302       *  @brief  Bypassing the smart pointer.
303       *  @return  The raw pointer being managed.
304       *
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
307       *  a raw pointer.
308       *
309       *  @note  This %auto_ptr no longer owns the memory.  When this object
310       *  goes out of scope, nothing will happen.
311       */
312      element_type*
313      release() throw()
314      {
315	element_type* __tmp = _M_ptr;
316	_M_ptr = 0;
317	return __tmp;
318      }
319
320      /**
321       *  @brief  Forcibly deletes the managed object.
322       *  @param  p  A pointer (defaults to NULL).
323       *
324       *  This object now @e owns the object pointed to by @a p.  The
325       *  previous object has been deleted.
326       */
327      void
328      reset(element_type* __p = 0) throw()
329      {
330	if (__p != _M_ptr)
331	  {
332	    delete _M_ptr;
333	    _M_ptr = __p;
334	  }
335      }
336
337      /** @{
338       *  @brief  Automatic conversions
339       *
340       *  These operations convert an %auto_ptr into and from an auto_ptr_ref
341       *  automatically as needed.  This allows constructs such as
342       *  @code
343       *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
344       *    ...
345       *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
346       *  @endcode
347       */
348      auto_ptr(auto_ptr_ref<element_type> __ref) throw()
349      : _M_ptr(__ref._M_ptr) { }
350
351      auto_ptr&
352      operator=(auto_ptr_ref<element_type> __ref) throw()
353      {
354	if (__ref._M_ptr != this->get())
355	  {
356	    delete _M_ptr;
357	    _M_ptr = __ref._M_ptr;
358	  }
359	return *this;
360      }
361
362      template<typename _Tp1>
363        operator auto_ptr_ref<_Tp1>() throw()
364        { return auto_ptr_ref<_Tp1>(this->release()); }
365
366      template<typename _Tp1>
367        operator auto_ptr<_Tp1>() throw()
368        { return auto_ptr<_Tp1>(this->release()); }
369      /** @}  */
370  };
371} // namespace std
372
373#endif /* _GLIBCXX_MEMORY */
374