xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/bits/stl_tempbuf.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Temporary buffer implementation -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /*
26*38fd1498Szrj  *
27*38fd1498Szrj  * Copyright (c) 1994
28*38fd1498Szrj  * Hewlett-Packard Company
29*38fd1498Szrj  *
30*38fd1498Szrj  * Permission to use, copy, modify, distribute and sell this software
31*38fd1498Szrj  * and its documentation for any purpose is hereby granted without fee,
32*38fd1498Szrj  * provided that the above copyright notice appear in all copies and
33*38fd1498Szrj  * that both that copyright notice and this permission notice appear
34*38fd1498Szrj  * in supporting documentation.  Hewlett-Packard Company makes no
35*38fd1498Szrj  * representations about the suitability of this software for any
36*38fd1498Szrj  * purpose.  It is provided "as is" without express or implied warranty.
37*38fd1498Szrj  *
38*38fd1498Szrj  *
39*38fd1498Szrj  * Copyright (c) 1996,1997
40*38fd1498Szrj  * Silicon Graphics Computer Systems, Inc.
41*38fd1498Szrj  *
42*38fd1498Szrj  * Permission to use, copy, modify, distribute and sell this software
43*38fd1498Szrj  * and its documentation for any purpose is hereby granted without fee,
44*38fd1498Szrj  * provided that the above copyright notice appear in all copies and
45*38fd1498Szrj  * that both that copyright notice and this permission notice appear
46*38fd1498Szrj  * in supporting documentation.  Silicon Graphics makes no
47*38fd1498Szrj  * representations about the suitability of this software for any
48*38fd1498Szrj  * purpose.  It is provided "as is" without express or implied warranty.
49*38fd1498Szrj  */
50*38fd1498Szrj 
51*38fd1498Szrj /** @file bits/stl_tempbuf.h
52*38fd1498Szrj  *  This is an internal header file, included by other library headers.
53*38fd1498Szrj  *  Do not attempt to use it directly. @headername{memory}
54*38fd1498Szrj  */
55*38fd1498Szrj 
56*38fd1498Szrj #ifndef _STL_TEMPBUF_H
57*38fd1498Szrj #define _STL_TEMPBUF_H 1
58*38fd1498Szrj 
59*38fd1498Szrj #include <bits/stl_algobase.h>
60*38fd1498Szrj #include <bits/stl_construct.h>
61*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)62*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
63*38fd1498Szrj {
64*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
65*38fd1498Szrj 
66*38fd1498Szrj   /**
67*38fd1498Szrj    *  @brief Allocates a temporary buffer.
68*38fd1498Szrj    *  @param  __len  The number of objects of type Tp.
69*38fd1498Szrj    *  @return See full description.
70*38fd1498Szrj    *
71*38fd1498Szrj    *  Reinventing the wheel, but this time with prettier spokes!
72*38fd1498Szrj    *
73*38fd1498Szrj    *  This function tries to obtain storage for @c __len adjacent Tp
74*38fd1498Szrj    *  objects.  The objects themselves are not constructed, of course.
75*38fd1498Szrj    *  A pair<> is returned containing <em>the buffer s address and
76*38fd1498Szrj    *  capacity (in the units of sizeof(_Tp)), or a pair of 0 values if
77*38fd1498Szrj    *  no storage can be obtained.</em>  Note that the capacity obtained
78*38fd1498Szrj    *  may be less than that requested if the memory is unavailable;
79*38fd1498Szrj    *  you should compare len with the .second return value.
80*38fd1498Szrj    *
81*38fd1498Szrj    * Provides the nothrow exception guarantee.
82*38fd1498Szrj    */
83*38fd1498Szrj   template<typename _Tp>
84*38fd1498Szrj     pair<_Tp*, ptrdiff_t>
85*38fd1498Szrj     get_temporary_buffer(ptrdiff_t __len) _GLIBCXX_NOEXCEPT
86*38fd1498Szrj     {
87*38fd1498Szrj       const ptrdiff_t __max =
88*38fd1498Szrj 	__gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
89*38fd1498Szrj       if (__len > __max)
90*38fd1498Szrj 	__len = __max;
91*38fd1498Szrj 
92*38fd1498Szrj       while (__len > 0)
93*38fd1498Szrj 	{
94*38fd1498Szrj 	  _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
95*38fd1498Szrj 							std::nothrow));
96*38fd1498Szrj 	  if (__tmp != 0)
97*38fd1498Szrj 	    return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
98*38fd1498Szrj 	  __len /= 2;
99*38fd1498Szrj 	}
100*38fd1498Szrj       return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
101*38fd1498Szrj     }
102*38fd1498Szrj 
103*38fd1498Szrj   /**
104*38fd1498Szrj    *  @brief The companion to get_temporary_buffer().
105*38fd1498Szrj    *  @param  __p  A buffer previously allocated by get_temporary_buffer.
106*38fd1498Szrj    *  @return   None.
107*38fd1498Szrj    *
108*38fd1498Szrj    *  Frees the memory pointed to by __p.
109*38fd1498Szrj    */
110*38fd1498Szrj   template<typename _Tp>
111*38fd1498Szrj     inline void
112*38fd1498Szrj     return_temporary_buffer(_Tp* __p)
113*38fd1498Szrj     { ::operator delete(__p, std::nothrow); }
114*38fd1498Szrj 
115*38fd1498Szrj 
116*38fd1498Szrj   /**
117*38fd1498Szrj    *  This class is used in two places: stl_algo.h and ext/memory,
118*38fd1498Szrj    *  where it is wrapped as the temporary_buffer class.  See
119*38fd1498Szrj    *  temporary_buffer docs for more notes.
120*38fd1498Szrj    */
121*38fd1498Szrj   template<typename _ForwardIterator, typename _Tp>
122*38fd1498Szrj     class _Temporary_buffer
123*38fd1498Szrj     {
124*38fd1498Szrj       // concept requirements
125*38fd1498Szrj       __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
126*38fd1498Szrj 
127*38fd1498Szrj     public:
128*38fd1498Szrj       typedef _Tp         value_type;
129*38fd1498Szrj       typedef value_type* pointer;
130*38fd1498Szrj       typedef pointer     iterator;
131*38fd1498Szrj       typedef ptrdiff_t   size_type;
132*38fd1498Szrj 
133*38fd1498Szrj     protected:
134*38fd1498Szrj       size_type  _M_original_len;
135*38fd1498Szrj       size_type  _M_len;
136*38fd1498Szrj       pointer    _M_buffer;
137*38fd1498Szrj 
138*38fd1498Szrj     public:
139*38fd1498Szrj       /// As per Table mumble.
140*38fd1498Szrj       size_type
141*38fd1498Szrj       size() const
142*38fd1498Szrj       { return _M_len; }
143*38fd1498Szrj 
144*38fd1498Szrj       /// Returns the size requested by the constructor; may be >size().
145*38fd1498Szrj       size_type
146*38fd1498Szrj       requested_size() const
147*38fd1498Szrj       { return _M_original_len; }
148*38fd1498Szrj 
149*38fd1498Szrj       /// As per Table mumble.
150*38fd1498Szrj       iterator
151*38fd1498Szrj       begin()
152*38fd1498Szrj       { return _M_buffer; }
153*38fd1498Szrj 
154*38fd1498Szrj       /// As per Table mumble.
155*38fd1498Szrj       iterator
156*38fd1498Szrj       end()
157*38fd1498Szrj       { return _M_buffer + _M_len; }
158*38fd1498Szrj 
159*38fd1498Szrj       /**
160*38fd1498Szrj        * Constructs a temporary buffer of a size somewhere between
161*38fd1498Szrj        * zero and the size of the given range.
162*38fd1498Szrj        */
163*38fd1498Szrj       _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
164*38fd1498Szrj 
165*38fd1498Szrj       ~_Temporary_buffer()
166*38fd1498Szrj       {
167*38fd1498Szrj 	std::_Destroy(_M_buffer, _M_buffer + _M_len);
168*38fd1498Szrj 	std::return_temporary_buffer(_M_buffer);
169*38fd1498Szrj       }
170*38fd1498Szrj 
171*38fd1498Szrj     private:
172*38fd1498Szrj       // Disable copy constructor and assignment operator.
173*38fd1498Szrj       _Temporary_buffer(const _Temporary_buffer&);
174*38fd1498Szrj 
175*38fd1498Szrj       void
176*38fd1498Szrj       operator=(const _Temporary_buffer&);
177*38fd1498Szrj     };
178*38fd1498Szrj 
179*38fd1498Szrj 
180*38fd1498Szrj   template<bool>
181*38fd1498Szrj     struct __uninitialized_construct_buf_dispatch
182*38fd1498Szrj     {
183*38fd1498Szrj       template<typename _Pointer, typename _ForwardIterator>
184*38fd1498Szrj         static void
185*38fd1498Szrj         __ucr(_Pointer __first, _Pointer __last,
186*38fd1498Szrj 	      _ForwardIterator __seed)
187*38fd1498Szrj         {
188*38fd1498Szrj 	  if(__first == __last)
189*38fd1498Szrj 	    return;
190*38fd1498Szrj 
191*38fd1498Szrj 	  _Pointer __cur = __first;
192*38fd1498Szrj 	  __try
193*38fd1498Szrj 	    {
194*38fd1498Szrj 	      std::_Construct(std::__addressof(*__first),
195*38fd1498Szrj 			      _GLIBCXX_MOVE(*__seed));
196*38fd1498Szrj 	      _Pointer __prev = __cur;
197*38fd1498Szrj 	      ++__cur;
198*38fd1498Szrj 	      for(; __cur != __last; ++__cur, ++__prev)
199*38fd1498Szrj 		std::_Construct(std::__addressof(*__cur),
200*38fd1498Szrj 				_GLIBCXX_MOVE(*__prev));
201*38fd1498Szrj 	      *__seed = _GLIBCXX_MOVE(*__prev);
202*38fd1498Szrj 	    }
203*38fd1498Szrj 	  __catch(...)
204*38fd1498Szrj 	    {
205*38fd1498Szrj 	      std::_Destroy(__first, __cur);
206*38fd1498Szrj 	      __throw_exception_again;
207*38fd1498Szrj 	    }
208*38fd1498Szrj 	}
209*38fd1498Szrj     };
210*38fd1498Szrj 
211*38fd1498Szrj   template<>
212*38fd1498Szrj     struct __uninitialized_construct_buf_dispatch<true>
213*38fd1498Szrj     {
214*38fd1498Szrj       template<typename _Pointer, typename _ForwardIterator>
215*38fd1498Szrj         static void
216*38fd1498Szrj         __ucr(_Pointer, _Pointer, _ForwardIterator) { }
217*38fd1498Szrj     };
218*38fd1498Szrj 
219*38fd1498Szrj   // Constructs objects in the range [first, last).
220*38fd1498Szrj   // Note that while these new objects will take valid values,
221*38fd1498Szrj   // their exact value is not defined. In particular they may
222*38fd1498Szrj   // be 'moved from'.
223*38fd1498Szrj   //
224*38fd1498Szrj   // While *__seed may be altered during this algorithm, it will have
225*38fd1498Szrj   // the same value when the algorithm finishes, unless one of the
226*38fd1498Szrj   // constructions throws.
227*38fd1498Szrj   //
228*38fd1498Szrj   // Requirements: _Pointer::value_type(_Tp&&) is valid.
229*38fd1498Szrj   template<typename _Pointer, typename _ForwardIterator>
230*38fd1498Szrj     inline void
231*38fd1498Szrj     __uninitialized_construct_buf(_Pointer __first, _Pointer __last,
232*38fd1498Szrj 				  _ForwardIterator __seed)
233*38fd1498Szrj     {
234*38fd1498Szrj       typedef typename std::iterator_traits<_Pointer>::value_type
235*38fd1498Szrj 	_ValueType;
236*38fd1498Szrj 
237*38fd1498Szrj       std::__uninitialized_construct_buf_dispatch<
238*38fd1498Szrj         __has_trivial_constructor(_ValueType)>::
239*38fd1498Szrj 	  __ucr(__first, __last, __seed);
240*38fd1498Szrj     }
241*38fd1498Szrj 
242*38fd1498Szrj   template<typename _ForwardIterator, typename _Tp>
243*38fd1498Szrj     _Temporary_buffer<_ForwardIterator, _Tp>::
244*38fd1498Szrj     _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
245*38fd1498Szrj     : _M_original_len(std::distance(__first, __last)),
246*38fd1498Szrj       _M_len(0), _M_buffer(0)
247*38fd1498Szrj     {
248*38fd1498Szrj       __try
249*38fd1498Szrj 	{
250*38fd1498Szrj 	  std::pair<pointer, size_type> __p(std::get_temporary_buffer<
251*38fd1498Szrj 					    value_type>(_M_original_len));
252*38fd1498Szrj 	  _M_buffer = __p.first;
253*38fd1498Szrj 	  _M_len = __p.second;
254*38fd1498Szrj 	  if (_M_buffer)
255*38fd1498Szrj 	    std::__uninitialized_construct_buf(_M_buffer, _M_buffer + _M_len,
256*38fd1498Szrj 					       __first);
257*38fd1498Szrj 	}
258*38fd1498Szrj       __catch(...)
259*38fd1498Szrj 	{
260*38fd1498Szrj 	  std::return_temporary_buffer(_M_buffer);
261*38fd1498Szrj 	  _M_buffer = 0;
262*38fd1498Szrj 	  _M_len = 0;
263*38fd1498Szrj 	  __throw_exception_again;
264*38fd1498Szrj 	}
265*38fd1498Szrj     }
266*38fd1498Szrj 
267*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
268*38fd1498Szrj } // namespace
269*38fd1498Szrj 
270*38fd1498Szrj #endif /* _STL_TEMPBUF_H */
271*38fd1498Szrj 
272