std_memory.h revision 102782
118334Speter// <memory> -*- C++ -*-
290075Sobrien
3169689Skan// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4169689Skan//
518334Speter// This file is part of the GNU ISO C++ Library.  This library is free
690075Sobrien// software; you can redistribute it and/or modify it under the
718334Speter// terms of the GNU General Public License as published by the
890075Sobrien// Free Software Foundation; either version 2, or (at your option)
990075Sobrien// any later version.
1090075Sobrien
1190075Sobrien// This library is distributed in the hope that it will be useful,
1218334Speter// but WITHOUT ANY WARRANTY; without even the implied warranty of
1390075Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1490075Sobrien// GNU General Public License for more details.
1590075Sobrien
1690075Sobrien// You should have received a copy of the GNU General Public License along
1718334Speter// with this library; see the file COPYING.  If not, write to the Free
1818334Speter// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1990075Sobrien// USA.
20169689Skan
21169689Skan// As a special exception, you may use this file as part of a free software
2218334Speter// library without restriction.  Specifically, if other files instantiate
2318334Speter// templates or use macros or inline functions from this file, or you compile
2418334Speter// this file and link it with other files to produce an executable, this
2518334Speter// file does not by itself cause the resulting executable to be covered by
2618334Speter// the GNU General Public License.  This exception does not however
2718334Speter// invalidate any other reasons why the executable file might be covered by
2818334Speter// the GNU General Public License.
2918334Speter
3018334Speter/*
3150397Sobrien * Copyright (c) 1997-1999
3250397Sobrien * Silicon Graphics Computer Systems, Inc.
33132718Skan *
34132718Skan * Permission to use, copy, modify, distribute and sell this software
3518334Speter * and its documentation for any purpose is hereby granted without fee,
3618334Speter * provided that the above copyright notice appear in all copies and
3718334Speter * that both that copyright notice and this permission notice appear
3818334Speter * in supporting documentation.  Silicon Graphics makes no
3918334Speter * representations about the suitability of this software for any
4018334Speter * purpose.  It is provided "as is" without express or implied warranty.
4118334Speter *
42117395Skan */
4390075Sobrien
4450397Sobrien/** @file memory
4590075Sobrien *  This is a Standard C++ Library header.  You should @c #include this header
4618334Speter *  in your programs, rather than any of the "st[dl]_*.h" implementation files.
4790075Sobrien */
4890075Sobrien
4990075Sobrien#ifndef _CPP_MEMORY
5090075Sobrien#define _CPP_MEMORY 1
5190075Sobrien
52169689Skan#pragma GCC system_header
53132718Skan
54169689Skan#include <bits/stl_algobase.h>
55169689Skan#include <bits/stl_alloc.h>
5618334Speter#include <bits/stl_construct.h>
5718334Speter#include <bits/stl_iterator_base_types.h> //for iterator_traits
5890075Sobrien#include <bits/stl_uninitialized.h>
5990075Sobrien#include <bits/stl_raw_storage_iter.h>
6018334Speter
6118334Speter// Since this entire file is within namespace std, there's no reason to
62169689Skan// waste two spaces along the left column.  Thus the leading indentation is
63169689Skan// slightly violated from here on.
64169689Skannamespace std
6518334Speter{
6690075Sobrien/**
6790075Sobrien *  @if maint
6818334Speter *  This is a helper function.  The unused second parameter exists to
6990075Sobrien *  permit the real get_temporary_buffer to use template parameter deduction.
70117395Skan *
71169689Skan *  XXX This should perhaps use the pool.
7290075Sobrien *  @endif
73117395Skan*/
7490075Sobrientemplate <typename _Tp>
75169689Skanpair<_Tp*, ptrdiff_t>
76169689Skan__get_temporary_buffer(ptrdiff_t __len, _Tp*)
7790075Sobrien{
78132718Skan  if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
79132718Skan    __len = INT_MAX / sizeof(_Tp);
80132718Skan
8190075Sobrien  while (__len > 0) {
8290075Sobrien    _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
83132718Skan    if (__tmp != 0)
8490075Sobrien      return pair<_Tp*, ptrdiff_t>(__tmp, __len);
8518334Speter    __len /= 2;
8618334Speter  }
8718334Speter
88132718Skan  return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
8918334Speter}
9018334Speter
9118334Speter/**
9218334Speter *  @brief This is a mostly-useless wrapper around malloc().
9318334Speter *  @param  len  The number of objects of type Tp.
9418334Speter *  @return   See full description.
9518334Speter *
9618334Speter *  Reinventing the wheel, but this time with prettier spokes!
9718334Speter *
9818334Speter *  This function tries to obtain storage for @c len adjacent Tp objects.
9918334Speter *  The objects themselves are not constructed, of course.  A pair<> is
10018334Speter *  returned containing "the buffer s address and capacity (in the units of
10118334Speter *  sizeof(Tp)), or a pair of 0 values if no storage can be obtained."
102169689Skan *  Note that the capacity obtained may be less than that requested if the
103169689Skan *  memory is unavailable; you should compare len with the .second return
10418334Speter *  value.
105169689Skan*/
106169689Skantemplate<typename _Tp>
107169689Skan  inline pair<_Tp*,ptrdiff_t>
108169689Skan  get_temporary_buffer(ptrdiff_t __len)
109169689Skan  {
11090075Sobrien    return __get_temporary_buffer(__len, (_Tp*) 0);
11118334Speter  }
112132718Skan
113132718Skan/**
114132718Skan *  @brief The companion to get_temporary_buffer().
115132718Skan *  @param  p  A buffer previously allocated by get_temporary_buffer.
116132718Skan *  @return   None.
117132718Skan *
118132718Skan *  Frees the memory pointed to by p.
119132718Skan */
120132718Skantemplate<typename _Tp>
121132718Skan  void
122132718Skan  return_temporary_buffer(_Tp* __p)
123132718Skan  {
124132718Skan    std::free(__p);
125132718Skan  }
126132718Skan
127132718Skan
128132718Skan/**
129169689Skan *  A wrapper class to provide auto_ptr with reference semantics.  For
13050397Sobrien *  example, an auto_ptr can be assigned (or constructed from) the result of
131132718Skan *  a function which returns an auto_ptr by value.
132132718Skan *
13350397Sobrien *  All the auto_ptr_ref stuff should happen behind the scenes.
13450397Sobrien*/
135132718Skantemplate<typename _Tp1>
136132718Skan  struct auto_ptr_ref
137132718Skan{
13850397Sobrien   _Tp1* _M_ptr;
13950397Sobrien
140132718Skan   explicit
141169689Skan   auto_ptr_ref(_Tp1* __p)
14218334Speter   : _M_ptr(__p) {}
143169689Skan};
144169689Skan
145169689Skan
146169689Skan/**
147169689Skan *  @brief  A simple smart pointer providing strict ownership semantics.
148169689Skan *
149169689Skan *  The Standard says:
150169689Skan *  <pre>
151169689Skan *  An @c auto_ptr owns the object it holds a pointer to.  Copying an
15218334Speter *  @c auto_ptr copies the pointer and transfers ownership to the destination.
153169689Skan *  If more than one @c auto_ptr owns the same object at the same time the
154169689Skan *  behavior of the program is undefined.
155169689Skan *
156169689Skan *  The uses of @c auto_ptr include providing temporary exception-safety for
157169689Skan *  dynamically allocated memory, passing ownership of dynamically allocated
158169689Skan *  memory to a function, and returning dynamically allocated memory from a
159169689Skan *  function.  @c auto_ptr does not meet the CopyConstructible and Assignable
160169689Skan *  requirements for Standard Library <a href="tables.html#65">container</a>
161169689Skan *  elements and thus instantiating a Standard Library container with an
162169689Skan *  @c auto_ptr results in undefined behavior.
163169689Skan *  </pre>
164169689Skan *  Quoted from [20.4.5]/3.
165169689Skan *
166169689Skan *  Good examples of what can and cannot be done with auto_ptr can be found
167169689Skan *  in the libstdc++ testsuite.
168169689Skan *
169169689Skan *  @if maint
170169689Skan *  _GLIBCPP_RESOLVE_LIB_DEFECTS
171169689Skan *  127.  auto_ptr<> conversion issues
172169689Skan *  These resolutions have all been incorporated.
173169689Skan *  @endif
174169689Skan*/
175169689Skantemplate<typename _Tp>
176169689Skan  class auto_ptr
177169689Skan{
178169689Skanprivate:
179169689Skan  _Tp* _M_ptr;
180169689Skan
181169689Skanpublic:
182169689Skan  /// The pointed-to type.
183117395Skan  typedef _Tp element_type;
18490075Sobrien
18518334Speter  /**
18618334Speter   *  @brief  An %auto_ptr is usually constructed from a raw pointer.
18718334Speter   *  @param  p  A pointer (defaults to NULL).
18890075Sobrien   *
18918334Speter   *  This object now @e owns the object pointed to by @a p.
190169689Skan  */
191169689Skan  explicit
19218334Speter  auto_ptr(element_type* __p = 0) throw()
193169689Skan  : _M_ptr(__p) { }
194169689Skan
19590075Sobrien  /**
196169689Skan   *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
197169689Skan   *  @param  a  Another %auto_ptr of the same type.
19890075Sobrien   *
199169689Skan   *  This object now @e owns the object previously owned by @a a, which has
200169689Skan   *  given up ownsership.
20190075Sobrien  */
202169689Skan  auto_ptr(auto_ptr& __a) throw()
20318334Speter  : _M_ptr(__a.release()) { }
204169689Skan
205169689Skan  /**
206169689Skan   *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
207169689Skan   *  @param  a  Another %auto_ptr of a different but related type.
208169689Skan   *
20918334Speter   *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
210169689Skan   *
21118334Speter   *  This object now @e owns the object previously owned by @a a, which has
21218334Speter   *  given up ownsership.
213169689Skan  */
214169689Skan  template<typename _Tp1>
21518334Speter    auto_ptr(auto_ptr<_Tp1>& __a) throw()
216169689Skan    : _M_ptr(__a.release()) { }
217169689Skan
21818334Speter  /**
219117395Skan   *  @brief  %auto_ptr assignment operator.
220169689Skan   *  @param  a  Another %auto_ptr of the same type.
22118334Speter   *
222169689Skan   *  This object now @e owns the object previously owned by @a a, which has
223169689Skan   *  given up ownsership.  The object that this one @e used to own and
22418334Speter   *  track has been deleted.
225169689Skan  */
226169689Skan  auto_ptr&
227169689Skan  operator=(auto_ptr& __a) throw()
22818334Speter    {
22918334Speter      reset(__a.release());
230169689Skan      return *this;
23118334Speter    }
232169689Skan
233169689Skan  /**
23418334Speter   *  @brief  %auto_ptr assignment operator.
235169689Skan   *  @param  a  Another %auto_ptr of a different but related type.
236169689Skan   *
237169689Skan   *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
238169689Skan   *
23918334Speter   *  This object now @e owns the object previously owned by @a a, which has
24018334Speter   *  given up ownsership.  The object that this one @e used to own and
241169689Skan   *  track has been deleted.
242169689Skan  */
24350397Sobrien  template <typename _Tp1>
244169689Skan    auto_ptr&
245169689Skan    operator=(auto_ptr<_Tp1>& __a) throw()
24650397Sobrien    {
24750397Sobrien      reset(__a.release());
248169689Skan      return *this;
24990075Sobrien    }
250169689Skan
251169689Skan  /**
252169689Skan   *  When the %auto_ptr goes out of scope, the object it owns is deleted.
25390075Sobrien   *  If it no longer owns anything (i.e., @c get() is @c NULL), then this
254169689Skan   *  has no effect.
25590075Sobrien   *
256169689Skan   *  @if maint
257169689Skan   *  The C++ standard says there is supposed to be an empty throw
258169689Skan   *  specification here, but omitting it is standard conforming.  Its
259169689Skan   *  presence can be detected only if _Tp::~_Tp() throws, but this is
260169689Skan   *  prohibited.  [17.4.3.6]/2
26190075Sobrien   *  @end maint
262169689Skan  */
263169689Skan  ~auto_ptr() { delete _M_ptr; }
26490075Sobrien
26590075Sobrien  /**
266169689Skan   *  @brief  Smart pointer dereferencing.
26790075Sobrien   *
268169689Skan   *  If this %auto_ptr no longer owns anything, then this operation will
269169689Skan   *  crash.  (For a smart pointer, "no longer owns anything" is the same as
27090075Sobrien   *  being a null pointer, and you know what happens when you dereference
271169689Skan   *  one of those...)
27290075Sobrien  */
273169689Skan  element_type&
274169689Skan  operator*() const throw() { return *_M_ptr; }
275169689Skan
27690075Sobrien  /**
277169689Skan   *  @brief  Smart pointer dereferencing.
27890075Sobrien   *
27990075Sobrien   *  This returns the pointer itself, which the language then will
280169689Skan   *  automatically cause to be dereferenced.
281169689Skan  */
28290075Sobrien  element_type*
283169689Skan  operator->() const throw() { return _M_ptr; }
284169689Skan
28590075Sobrien  /**
286169689Skan   *  @brief  Bypassing the smart pointer.
28790075Sobrien   *  @return  The raw pointer being managed.
288169689Skan   *
289169689Skan   *  You can get a copy of the pointer that this object owns, for
290169689Skan   *  situations such as passing to a function which only accepts a raw
291169689Skan   *  pointer.
292169689Skan   *
29390075Sobrien   *  @note  This %auto_ptr still owns the memory.
294169689Skan  */
295169689Skan  element_type*
296169689Skan  get() const throw() { return _M_ptr; }
297169689Skan
298169689Skan  /**
29990075Sobrien   *  @brief  Bypassing the smart pointer.
300117395Skan   *  @return  The raw pointer being managed.
30190075Sobrien   *
302169689Skan   *  You can get a copy of the pointer that this object owns, for
303169689Skan   *  situations such as passing to a function which only accepts a raw
304169689Skan   *  pointer.
305169689Skan   *
306169689Skan   *  @note  This %auto_ptr no longer owns the memory.  When this object
307169689Skan   *  goes out of scope, nothing will happen.
308169689Skan  */
309169689Skan  element_type*
310169689Skan  release() throw()
311169689Skan    {
31290075Sobrien      element_type* __tmp = _M_ptr;
313169689Skan      _M_ptr = 0;
31490075Sobrien      return __tmp;
31590075Sobrien    }
316169689Skan
317169689Skan  /**
31890075Sobrien   *  @brief  Forcibly deletes the managed object.
319169689Skan   *  @param  p  A pointer (defaults to NULL).
320169689Skan   *
321169689Skan   *  This object now @e owns the object pointed to by @a p.  The previous
322169689Skan   *  object has been deleted.
323169689Skan  */
32490075Sobrien  void
325169689Skan  reset(element_type* __p = 0) throw()
326169689Skan    {
327169689Skan      if (__p != _M_ptr)
328169689Skan        {
329169689Skan          delete _M_ptr;
330169689Skan          _M_ptr = __p;
33190075Sobrien        }
332169689Skan    }
333169689Skan
33490075Sobrien  /** @{
335169689Skan   *  @brief  Automatic conversions
336169689Skan   *
33790075Sobrien   *  These operations convert an %auto_ptr into and from an auto_ptr_ref
338169689Skan   *  automatically as needed.  This allows constructs such as
339169689Skan   *  @code
340169689Skan   *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
341169689Skan   *    ...
34290075Sobrien   *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
343169689Skan   *  @endcode
344169689Skan  */
345169689Skan  auto_ptr(auto_ptr_ref<element_type> __ref) throw()
346169689Skan    : _M_ptr(__ref._M_ptr) {}
34790075Sobrien
348169689Skan  auto_ptr&
349169689Skan  operator=(auto_ptr_ref<element_type> __ref) throw()
35090075Sobrien    {
351169689Skan      if (__ref._M_ptr != this->get())
352169689Skan        {
353169689Skan          delete _M_ptr;
354169689Skan          _M_ptr = __ref._M_ptr;
355169689Skan        }
356169689Skan      return *this;
357169689Skan    }
358169689Skan
359169689Skan  template<typename _Tp1>
360169689Skan    operator auto_ptr_ref<_Tp1>() throw()
361169689Skan      { return auto_ptr_ref<_Tp1>(this->release()); }
362169689Skan
363169689Skan  template<typename _Tp1>
364169689Skan    operator auto_ptr<_Tp1>() throw()
365169689Skan      { return auto_ptr<_Tp1>(this->release()); }
366169689Skan  /** @}  */
367169689Skan};
368169689Skan
369169689Skan} // namespace std
370169689Skan
371169689Skan#endif /* _CPP_MEMORY */
372169689Skan