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