]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/bits/stl_tempbuf.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / bits / stl_tempbuf.h
1 // Temporary buffer implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32  *
33  * Copyright (c) 1994
34  * Hewlett-Packard Company
35  *
36  * Permission to use, copy, modify, distribute and sell this software
37  * and its documentation for any purpose is hereby granted without fee,
38  * provided that the above copyright notice appear in all copies and
39  * that both that copyright notice and this permission notice appear
40  * in supporting documentation.  Hewlett-Packard Company makes no
41  * representations about the suitability of this software for any
42  * purpose.  It is provided "as is" without express or implied warranty.
43  *
44  *
45  * Copyright (c) 1996,1997
46  * Silicon Graphics Computer Systems, Inc.
47  *
48  * Permission to use, copy, modify, distribute and sell this software
49  * and its documentation for any purpose is hereby granted without fee,
50  * provided that the above copyright notice appear in all copies and
51  * that both that copyright notice and this permission notice appear
52  * in supporting documentation.  Silicon Graphics makes no
53  * representations about the suitability of this software for any
54  * purpose.  It is provided "as is" without express or implied warranty.
55  */
56
57 /** @file stl_tempbuf.h
58  *  This is an internal header file, included by other library headers.
59  *  You should not attempt to use it directly.
60  */
61
62 #ifndef _STL_TEMPBUF_H
63 #define _STL_TEMPBUF_H 1
64
65 #include <bits/stl_algobase.h>
66 #include <bits/stl_construct.h>
67 #include <bits/stl_uninitialized.h>
68
69 _GLIBCXX_BEGIN_NAMESPACE(std)
70
71   /**
72    *  @brief Allocates a temporary buffer.
73    *  @param  len  The number of objects of type Tp.
74    *  @return See full description.
75    *
76    *  Reinventing the wheel, but this time with prettier spokes!
77    *
78    *  This function tries to obtain storage for @c len adjacent Tp
79    *  objects.  The objects themselves are not constructed, of course.
80    *  A pair<> is returned containing "the buffer s address and
81    *  capacity (in the units of sizeof(Tp)), or a pair of 0 values if
82    *  no storage can be obtained."  Note that the capacity obtained
83    *  may be less than that requested if the memory is unavailable;
84    *  you should compare len with the .second return value.
85    *
86    * Provides the nothrow exception guarantee.
87    */
88   template<typename _Tp>
89     pair<_Tp*, ptrdiff_t>
90     get_temporary_buffer(ptrdiff_t __len)
91     {
92       const ptrdiff_t __max =
93         __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
94       if (__len > __max)
95         __len = __max;
96       
97       while (__len > 0) 
98         {
99           _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 
100                                                         std::nothrow));
101           if (__tmp != 0)
102             return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
103           __len /= 2;
104         }
105       return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
106     }
107
108   /**
109    *  @brief The companion to get_temporary_buffer().
110    *  @param  p  A buffer previously allocated by get_temporary_buffer.
111    *  @return   None.
112    *
113    *  Frees the memory pointed to by p.
114    */
115   template<typename _Tp>
116     inline void
117     return_temporary_buffer(_Tp* __p)
118     { ::operator delete(__p, std::nothrow); }
119
120
121   /**
122    *  This class is used in two places: stl_algo.h and ext/memory,
123    *  where it is wrapped as the temporary_buffer class.  See
124    *  temporary_buffer docs for more notes.
125    */
126   template<typename _ForwardIterator, typename _Tp>
127     class _Temporary_buffer
128     {
129       // concept requirements
130       __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
131
132     public:
133       typedef _Tp         value_type;
134       typedef value_type* pointer;
135       typedef pointer     iterator;
136       typedef ptrdiff_t   size_type;
137
138     protected:
139       size_type  _M_original_len;
140       size_type  _M_len;
141       pointer    _M_buffer;
142
143     public:
144       /// As per Table mumble.
145       size_type
146       size() const
147       { return _M_len; }
148
149       /// Returns the size requested by the constructor; may be >size().
150       size_type
151       requested_size() const
152       { return _M_original_len; }
153
154       /// As per Table mumble.
155       iterator
156       begin()
157       { return _M_buffer; }
158
159       /// As per Table mumble.
160       iterator
161       end()
162       { return _M_buffer + _M_len; }
163
164       /**
165        * Constructs a temporary buffer of a size somewhere between
166        * zero and the size of the given range.
167        */
168       _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
169
170       ~_Temporary_buffer()
171       {
172         std::_Destroy(_M_buffer, _M_buffer + _M_len);
173         std::return_temporary_buffer(_M_buffer);
174       }
175
176     private:
177       // Disable copy constructor and assignment operator.
178       _Temporary_buffer(const _Temporary_buffer&);
179
180       void
181       operator=(const _Temporary_buffer&);
182     };
183
184   template<typename _ForwardIterator, typename _Tp>
185     _Temporary_buffer<_ForwardIterator, _Tp>::
186     _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
187     : _M_original_len(std::distance(__first, __last)),
188       _M_len(0), _M_buffer(0)
189     {
190       try
191         {
192           std::pair<pointer, size_type> __p(std::get_temporary_buffer<
193                                             value_type>(_M_original_len));
194           _M_buffer = __p.first;
195           _M_len = __p.second;
196           if (!__is_pod(_Tp) && _M_len > 0)
197             std::uninitialized_fill_n(_M_buffer, _M_len, *__first);
198         }
199       catch(...)
200         {
201           std::return_temporary_buffer(_M_buffer);
202           _M_buffer = 0;
203           _M_len = 0;
204           __throw_exception_again;
205         }
206     }
207
208 _GLIBCXX_END_NAMESPACE
209
210 #endif /* _STL_TEMPBUF_H */
211