xref: /openbsd/gnu/gcc/libstdc++-v3/include/ext/memory (revision 404b540a)
1*404b540aSrobert// Memory extensions -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert// Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert//
5*404b540aSrobert// This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert// software; you can redistribute it and/or modify it under the
7*404b540aSrobert// terms of the GNU General Public License as published by the
8*404b540aSrobert// Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert// any later version.
10*404b540aSrobert
11*404b540aSrobert// This library is distributed in the hope that it will be useful,
12*404b540aSrobert// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert// GNU General Public License for more details.
15*404b540aSrobert
16*404b540aSrobert// You should have received a copy of the GNU General Public License along
17*404b540aSrobert// with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert// USA.
20*404b540aSrobert
21*404b540aSrobert// As a special exception, you may use this file as part of a free software
22*404b540aSrobert// library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert// templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert// this file and link it with other files to produce an executable, this
25*404b540aSrobert// file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert// the GNU General Public License.  This exception does not however
27*404b540aSrobert// invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert// the GNU General Public License.
29*404b540aSrobert
30*404b540aSrobert/*
31*404b540aSrobert *
32*404b540aSrobert * Copyright (c) 1994
33*404b540aSrobert * Hewlett-Packard Company
34*404b540aSrobert *
35*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software
36*404b540aSrobert * and its documentation for any purpose is hereby granted without fee,
37*404b540aSrobert * provided that the above copyright notice appear in all copies and
38*404b540aSrobert * that both that copyright notice and this permission notice appear
39*404b540aSrobert * in supporting documentation.  Hewlett-Packard Company makes no
40*404b540aSrobert * representations about the suitability of this software for any
41*404b540aSrobert * purpose.  It is provided "as is" without express or implied warranty.
42*404b540aSrobert *
43*404b540aSrobert *
44*404b540aSrobert * Copyright (c) 1996
45*404b540aSrobert * Silicon Graphics Computer Systems, Inc.
46*404b540aSrobert *
47*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software
48*404b540aSrobert * and its documentation for any purpose is hereby granted without fee,
49*404b540aSrobert * provided that the above copyright notice appear in all copies and
50*404b540aSrobert * that both that copyright notice and this permission notice appear
51*404b540aSrobert * in supporting documentation.  Silicon Graphics makes no
52*404b540aSrobert * representations about the suitability of this software for any
53*404b540aSrobert * purpose.  It is provided "as is" without express or implied warranty.
54*404b540aSrobert */
55*404b540aSrobert
56*404b540aSrobert/** @file ext/memory
57*404b540aSrobert *  This file is a GNU extension to the Standard C++ Library (possibly
58*404b540aSrobert *  containing extensions from the HP/SGI STL subset).
59*404b540aSrobert */
60*404b540aSrobert
61*404b540aSrobert#ifndef _EXT_MEMORY
62*404b540aSrobert#define _EXT_MEMORY 1
63*404b540aSrobert
64*404b540aSrobert#pragma GCC system_header
65*404b540aSrobert
66*404b540aSrobert#include <memory>
67*404b540aSrobert#include <bits/stl_tempbuf.h>
68*404b540aSrobert
69*404b540aSrobert_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
70*404b540aSrobert
71*404b540aSrobert  using std::ptrdiff_t;
72*404b540aSrobert  using std::pair;
73*404b540aSrobert  using std::__iterator_category;
74*404b540aSrobert  using std::_Temporary_buffer;
75*404b540aSrobert
76*404b540aSrobert  template<typename _InputIter, typename _Size, typename _ForwardIter>
77*404b540aSrobert    pair<_InputIter, _ForwardIter>
78*404b540aSrobert    __uninitialized_copy_n(_InputIter __first, _Size __count,
79*404b540aSrobert			   _ForwardIter __result, std::input_iterator_tag)
80*404b540aSrobert    {
81*404b540aSrobert      _ForwardIter __cur = __result;
82*404b540aSrobert      try
83*404b540aSrobert	{
84*404b540aSrobert	  for (; __count > 0 ; --__count, ++__first, ++__cur)
85*404b540aSrobert	    std::_Construct(&*__cur, *__first);
86*404b540aSrobert	  return pair<_InputIter, _ForwardIter>(__first, __cur);
87*404b540aSrobert	}
88*404b540aSrobert      catch(...)
89*404b540aSrobert	{
90*404b540aSrobert	  std::_Destroy(__result, __cur);
91*404b540aSrobert	  __throw_exception_again;
92*404b540aSrobert	}
93*404b540aSrobert    }
94*404b540aSrobert
95*404b540aSrobert  template<typename _RandomAccessIter, typename _Size, typename _ForwardIter>
96*404b540aSrobert    inline pair<_RandomAccessIter, _ForwardIter>
97*404b540aSrobert    __uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
98*404b540aSrobert			   _ForwardIter __result,
99*404b540aSrobert			   std::random_access_iterator_tag)
100*404b540aSrobert    {
101*404b540aSrobert      _RandomAccessIter __last = __first + __count;
102*404b540aSrobert      return (pair<_RandomAccessIter, _ForwardIter>
103*404b540aSrobert	      (__last, std::uninitialized_copy(__first, __last, __result)));
104*404b540aSrobert    }
105*404b540aSrobert
106*404b540aSrobert  template<typename _InputIter, typename _Size, typename _ForwardIter>
107*404b540aSrobert    inline pair<_InputIter, _ForwardIter>
108*404b540aSrobert    __uninitialized_copy_n(_InputIter __first, _Size __count,
109*404b540aSrobert			 _ForwardIter __result)
110*404b540aSrobert    { return __uninitialized_copy_n(__first, __count, __result,
111*404b540aSrobert				    __iterator_category(__first)); }
112*404b540aSrobert
113*404b540aSrobert  /**
114*404b540aSrobert   *  @brief Copies the range [first,last) into result.
115*404b540aSrobert   *  @param  first  An input iterator.
116*404b540aSrobert   *  @param  last   An input iterator.
117*404b540aSrobert   *  @param  result An output iterator.
118*404b540aSrobert   *  @return   result + (first - last)
119*404b540aSrobert   *  @ingroup SGIextensions
120*404b540aSrobert   *
121*404b540aSrobert   *  Like copy(), but does not require an initialized output range.
122*404b540aSrobert  */
123*404b540aSrobert  template<typename _InputIter, typename _Size, typename _ForwardIter>
124*404b540aSrobert    inline pair<_InputIter, _ForwardIter>
125*404b540aSrobert    uninitialized_copy_n(_InputIter __first, _Size __count,
126*404b540aSrobert			 _ForwardIter __result)
127*404b540aSrobert    { return __uninitialized_copy_n(__first, __count, __result,
128*404b540aSrobert				    __iterator_category(__first)); }
129*404b540aSrobert
130*404b540aSrobert
131*404b540aSrobert  // An alternative version of uninitialized_copy_n that constructs
132*404b540aSrobert  // and destroys objects with a user-provided allocator.
133*404b540aSrobert  template<typename _InputIter, typename _Size, typename _ForwardIter,
134*404b540aSrobert           typename _Allocator>
135*404b540aSrobert    pair<_InputIter, _ForwardIter>
136*404b540aSrobert    __uninitialized_copy_n_a(_InputIter __first, _Size __count,
137*404b540aSrobert			     _ForwardIter __result,
138*404b540aSrobert			     _Allocator __alloc)
139*404b540aSrobert    {
140*404b540aSrobert      _ForwardIter __cur = __result;
141*404b540aSrobert      try
142*404b540aSrobert	{
143*404b540aSrobert	  for (; __count > 0 ; --__count, ++__first, ++__cur)
144*404b540aSrobert	    __alloc.construct(&*__cur, *__first);
145*404b540aSrobert	  return pair<_InputIter, _ForwardIter>(__first, __cur);
146*404b540aSrobert	}
147*404b540aSrobert      catch(...)
148*404b540aSrobert	{
149*404b540aSrobert	  std::_Destroy(__result, __cur, __alloc);
150*404b540aSrobert	  __throw_exception_again;
151*404b540aSrobert	}
152*404b540aSrobert    }
153*404b540aSrobert
154*404b540aSrobert  template<typename _InputIter, typename _Size, typename _ForwardIter,
155*404b540aSrobert           typename _Tp>
156*404b540aSrobert    inline pair<_InputIter, _ForwardIter>
157*404b540aSrobert    __uninitialized_copy_n_a(_InputIter __first, _Size __count,
158*404b540aSrobert			     _ForwardIter __result,
159*404b540aSrobert			     std::allocator<_Tp>)
160*404b540aSrobert    {
161*404b540aSrobert      return uninitialized_copy_n(__first, __count, __result);
162*404b540aSrobert    }
163*404b540aSrobert
164*404b540aSrobert  /**
165*404b540aSrobert   *  This class provides similar behavior and semantics of the standard
166*404b540aSrobert   *  functions get_temporary_buffer() and return_temporary_buffer(), but
167*404b540aSrobert   *  encapsulated in a type vaguely resembling a standard container.
168*404b540aSrobert   *
169*404b540aSrobert   *  By default, a temporary_buffer<Iter> stores space for objects of
170*404b540aSrobert   *  whatever type the Iter iterator points to.  It is constructed from a
171*404b540aSrobert   *  typical [first,last) range, and provides the begin(), end(), size()
172*404b540aSrobert   *  functions, as well as requested_size().  For non-trivial types, copies
173*404b540aSrobert   *  of *first will be used to initialize the storage.
174*404b540aSrobert   *
175*404b540aSrobert   *  @c malloc is used to obtain underlying storage.
176*404b540aSrobert   *
177*404b540aSrobert   *  Like get_temporary_buffer(), not all the requested memory may be
178*404b540aSrobert   *  available.  Ideally, the created buffer will be large enough to hold a
179*404b540aSrobert   *  copy of [first,last), but if size() is less than requested_size(),
180*404b540aSrobert   *  then this didn't happen.
181*404b540aSrobert   *
182*404b540aSrobert   *  @ingroup SGIextensions
183*404b540aSrobert  */
184*404b540aSrobert  template <class _ForwardIterator, class _Tp
185*404b540aSrobert	    = typename std::iterator_traits<_ForwardIterator>::value_type >
186*404b540aSrobert    struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
187*404b540aSrobert    {
188*404b540aSrobert      /// Requests storage large enough to hold a copy of [first,last).
189*404b540aSrobert      temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
190*404b540aSrobert      : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) { }
191*404b540aSrobert
192*404b540aSrobert      /// Destroys objects and frees storage.
193*404b540aSrobert      ~temporary_buffer() { }
194*404b540aSrobert    };
195*404b540aSrobert
196*404b540aSrobert_GLIBCXX_END_NAMESPACE
197*404b540aSrobert
198*404b540aSrobert#endif
199*404b540aSrobert
200