std_memory.h revision 97403
1240116Smarcel// <memory> -*- C++ -*-
2240116Smarcel
3240116Smarcel// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4240116Smarcel//
5240116Smarcel// This file is part of the GNU ISO C++ Library.  This library is free
6240116Smarcel// software; you can redistribute it and/or modify it under the
7240116Smarcel// terms of the GNU General Public License as published by the
8240116Smarcel// Free Software Foundation; either version 2, or (at your option)
9240116Smarcel// any later version.
10240116Smarcel
11240116Smarcel// This library is distributed in the hope that it will be useful,
12240116Smarcel// but WITHOUT ANY WARRANTY; without even the implied warranty of
13240116Smarcel// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14240116Smarcel// GNU General Public License for more details.
15240116Smarcel
16240116Smarcel// You should have received a copy of the GNU General Public License along
17240116Smarcel// with this library; see the file COPYING.  If not, write to the Free
18240116Smarcel// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19240116Smarcel// USA.
20240116Smarcel
21240116Smarcel// As a special exception, you may use this file as part of a free software
22240116Smarcel// library without restriction.  Specifically, if other files instantiate
23240116Smarcel// templates or use macros or inline functions from this file, or you compile
24240116Smarcel// this file and link it with other files to produce an executable, this
25240116Smarcel// file does not by itself cause the resulting executable to be covered by
26240116Smarcel// the GNU General Public License.  This exception does not however
27240116Smarcel// invalidate any other reasons why the executable file might be covered by
28240116Smarcel// the GNU General Public License.
29240116Smarcel
30240116Smarcel/*
31240116Smarcel * Copyright (c) 1997-1999
32240116Smarcel * Silicon Graphics Computer Systems, Inc.
33240116Smarcel *
34240116Smarcel * Permission to use, copy, modify, distribute and sell this software
35240116Smarcel * and its documentation for any purpose is hereby granted without fee,
36240116Smarcel * provided that the above copyright notice appear in all copies and
37240116Smarcel * that both that copyright notice and this permission notice appear
38240116Smarcel * in supporting documentation.  Silicon Graphics makes no
39240116Smarcel * representations about the suitability of this software for any
40240116Smarcel * purpose.  It is provided "as is" without express or implied warranty.
41240116Smarcel *
42240116Smarcel */
43240116Smarcel
44240116Smarcel/** @file memory
45240116Smarcel *  This is a Standard C++ Library header.  You should @c #include this header
46240116Smarcel *  in your programs, rather than any of the "st[dl]_*.h" implementation files.
47240116Smarcel */
48240116Smarcel
49240116Smarcel#ifndef _CPP_MEMORY
50240116Smarcel#define _CPP_MEMORY 1
51240116Smarcel
52240116Smarcel#pragma GCC system_header
53240116Smarcel
54240116Smarcel#include <bits/stl_algobase.h>
55240116Smarcel#include <bits/stl_alloc.h>
56240116Smarcel#include <bits/stl_construct.h>
57240116Smarcel#include <bits/stl_iterator_base_types.h> //for iterator_traits
58240116Smarcel#include <bits/stl_uninitialized.h>
59240116Smarcel#include <bits/stl_raw_storage_iter.h>
60240116Smarcel
61240116Smarcelnamespace std
62240116Smarcel{
63240116Smarcel
64240116Smarcel  /**
65240116Smarcel   *  @if maint
66240116Smarcel   *  This is a helper function.  The unused second parameter exists to
67240116Smarcel   *  permit the real get_temporary_buffer to use template parameter deduction.
68240116Smarcel   *  @endif
69240116Smarcel  */
70240116Smarcel  template <class _Tp>
71240116Smarcel  pair<_Tp*, ptrdiff_t>
72240116Smarcel  __get_temporary_buffer(ptrdiff_t __len, _Tp*)
73240116Smarcel  {
74240116Smarcel    if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
75240116Smarcel      __len = INT_MAX / sizeof(_Tp);
76240116Smarcel
77240116Smarcel    while (__len > 0) {
78240116Smarcel      _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
79240116Smarcel      if (__tmp != 0)
80240116Smarcel	return pair<_Tp*, ptrdiff_t>(__tmp, __len);
81240116Smarcel      __len /= 2;
82240116Smarcel    }
83240116Smarcel
84240116Smarcel    return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
85240116Smarcel  }
86240116Smarcel
87240116Smarcel  /**
88240116Smarcel   *  @brief This is a mostly-useless wrapper around malloc().
89240116Smarcel   *  @param  len  The number of objects of type Tp.
90240116Smarcel   *  @return   See full description.
91240116Smarcel   *
92240116Smarcel   *  Reinventing the wheel, but this time with prettier spokes!
93240116Smarcel   *
94240116Smarcel   *  This function tries to obtain storage for @c len adjacent Tp objects.
95240116Smarcel   *  The objects themselves are not constructed, of course.  A pair<> is
96240116Smarcel   *  returned containing "the buffer s address and capacity (in the units of
97240116Smarcel   *  sizeof(Tp)), or a pair of 0 values if no storage can be obtained."
98240116Smarcel   *  Note that the capacity obtained may be less than that requested if the
99240116Smarcel   *  memory is unavailable; you should compare len with the .second return
100240116Smarcel   *  value.
101240116Smarcel  */
102240116Smarcel  template <class _Tp>
103240116Smarcel  inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) {
104240116Smarcel    return __get_temporary_buffer(__len, (_Tp*) 0);
105240116Smarcel  }
106240116Smarcel
107240116Smarcel  /**
108240116Smarcel   *  @brief The companion to get_temporary_buffer().
109240116Smarcel   *  @param  p  A buffer previously allocated by get_temporary_buffer.
110240116Smarcel   *  @return   None.
111240116Smarcel   *
112240116Smarcel   *  Frees the memory pointed to by p.
113240116Smarcel   */
114240116Smarcel  template <class _Tp>
115240116Smarcel  void return_temporary_buffer(_Tp* __p) {
116240116Smarcel    std::free(__p);
117240116Smarcel  }
118240116Smarcel
119240116Smarcel
120240116Smarceltemplate <class _Tp1>
121240116Smarcel  struct auto_ptr_ref
122240116Smarcel{
123240116Smarcel   _Tp1* _M_ptr;
124240116Smarcel   auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
125240116Smarcel};
126240116Smarcel
127240116Smarcel/**
128240116Smarcel *  A simple smart pointer providing strict ownership semantics.  (More later.)
129240116Smarcel*/
130240116Smarceltemplate <class _Tp>
131240116Smarcel  class auto_ptr
132240116Smarcel{
133240116Smarcelprivate:
134240116Smarcel  _Tp* _M_ptr;
135240116Smarcel
136240116Smarcelpublic:
137240116Smarcel  typedef _Tp element_type;
138240116Smarcel
139240116Smarcel  explicit auto_ptr(_Tp* __p = 0) throw() : _M_ptr(__p) {}
140240116Smarcel  auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) {}
141240116Smarcel
142240116Smarcel  template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) throw()
143240116Smarcel    : _M_ptr(__a.release()) {}
144240116Smarcel
145240116Smarcel  auto_ptr& operator=(auto_ptr& __a) throw() {
146240116Smarcel    reset(__a.release());
147240116Smarcel    return *this;
148240116Smarcel  }
149240116Smarcel
150240116Smarcel  template <class _Tp1>
151240116Smarcel  auto_ptr& operator=(auto_ptr<_Tp1>& __a) throw() {
152240116Smarcel    reset(__a.release());
153240116Smarcel    return *this;
154240116Smarcel  }
155240116Smarcel
156240116Smarcel  // Note: The C++ standard says there is supposed to be an empty throw
157240116Smarcel  // specification here, but omitting it is standard conforming.  Its
158240116Smarcel  // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
159240116Smarcel  // this is prohibited.
160240116Smarcel  ~auto_ptr() { delete _M_ptr; }
161240116Smarcel
162240116Smarcel  _Tp& operator*() const throw() {
163240116Smarcel    return *_M_ptr;
164240116Smarcel  }
165240116Smarcel  _Tp* operator->() const throw() {
166240116Smarcel    return _M_ptr;
167240116Smarcel  }
168240116Smarcel  _Tp* get() const throw() {
169240116Smarcel    return _M_ptr;
170240116Smarcel  }
171240116Smarcel  _Tp* release() throw() {
172240116Smarcel    _Tp* __tmp = _M_ptr;
173240116Smarcel    _M_ptr = 0;
174240116Smarcel    return __tmp;
175240116Smarcel  }
176240116Smarcel  void reset(_Tp* __p = 0) throw() {
177240116Smarcel    if (__p != _M_ptr) {
178240116Smarcel      delete _M_ptr;
179240116Smarcel      _M_ptr = __p;
180240116Smarcel    }
181240116Smarcel  }
182240116Smarcel
183240116Smarcelpublic:
184240116Smarcel  auto_ptr(auto_ptr_ref<_Tp> __ref) throw()
185240116Smarcel    : _M_ptr(__ref._M_ptr) {}
186240116Smarcel
187240116Smarcel  auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) throw() {
188240116Smarcel    if (__ref._M_ptr != this->get()) {
189240116Smarcel      delete _M_ptr;
190240116Smarcel      _M_ptr = __ref._M_ptr;
191240116Smarcel    }
192240116Smarcel    return *this;
193240116Smarcel  }
194240116Smarcel
195240116Smarcel  template <class _Tp1> operator auto_ptr_ref<_Tp1>() throw()
196240116Smarcel    { return auto_ptr_ref<_Tp>(this->release()); }
197240116Smarcel  template <class _Tp1> operator auto_ptr<_Tp1>() throw()
198240116Smarcel    { return auto_ptr<_Tp1>(this->release()); }
199240116Smarcel};
200240116Smarcel
201240116Smarcel} // namespace std
202240116Smarcel
203240116Smarcel#endif /* _CPP_MEMORY */
204240116Smarcel
205240116Smarcel