]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Temporary buffer implementation -*- C++ -*- |
2 | ||
bc2631e0 | 3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
43804767 | 4 | // Free Software Foundation, Inc. |
42526146 PE |
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 | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
42526146 PE |
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 | ||
725dc051 BK |
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 | ||
729e3d3f PE |
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. | |
725dc051 BK |
60 | */ |
61 | ||
046d30f4 PC |
62 | #ifndef _STL_TEMPBUF_H |
63 | #define _STL_TEMPBUF_H 1 | |
725dc051 | 64 | |
acb8a4ef PC |
65 | #include <bits/stl_algobase.h> |
66 | #include <bits/stl_construct.h> | |
67 | #include <bits/stl_uninitialized.h> | |
725dc051 | 68 | |
3cbc7af0 BK |
69 | _GLIBCXX_BEGIN_NAMESPACE(std) |
70 | ||
acb8a4ef | 71 | /** |
d87135d4 PC |
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. | |
acb8a4ef PC |
87 | */ |
88 | template<typename _Tp> | |
89 | pair<_Tp*, ptrdiff_t> | |
d87135d4 | 90 | get_temporary_buffer(ptrdiff_t __len) |
acb8a4ef PC |
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 | ||
acb8a4ef PC |
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> | |
de5e4138 | 116 | inline void |
acb8a4ef | 117 | return_temporary_buffer(_Tp* __p) |
de5e4138 | 118 | { ::operator delete(__p, std::nothrow); } |
acb8a4ef PC |
119 | |
120 | ||
917a9fd4 | 121 | /** |
917a9fd4 SW |
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. | |
917a9fd4 SW |
125 | */ |
126 | template<typename _ForwardIterator, typename _Tp> | |
127 | class _Temporary_buffer | |
128 | { | |
129 | // concept requirements | |
130 | __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept) | |
131 | ||
43804767 | 132 | public: |
917a9fd4 SW |
133 | typedef _Tp value_type; |
134 | typedef value_type* pointer; | |
135 | typedef pointer iterator; | |
136 | typedef ptrdiff_t size_type; | |
ed6814f7 | 137 | |
917a9fd4 SW |
138 | protected: |
139 | size_type _M_original_len; | |
140 | size_type _M_len; | |
141 | pointer _M_buffer; | |
ed6814f7 | 142 | |
917a9fd4 SW |
143 | public: |
144 | /// As per Table mumble. | |
145 | size_type | |
146 | size() const | |
147 | { return _M_len; } | |
ed6814f7 | 148 | |
917a9fd4 SW |
149 | /// Returns the size requested by the constructor; may be >size(). |
150 | size_type | |
151 | requested_size() const | |
152 | { return _M_original_len; } | |
ed6814f7 | 153 | |
917a9fd4 SW |
154 | /// As per Table mumble. |
155 | iterator | |
156 | begin() | |
157 | { return _M_buffer; } | |
ed6814f7 | 158 | |
917a9fd4 SW |
159 | /// As per Table mumble. |
160 | iterator | |
161 | end() | |
162 | { return _M_buffer + _M_len; } | |
ed6814f7 | 163 | |
917a9fd4 SW |
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); | |
ed6814f7 | 169 | |
917a9fd4 | 170 | ~_Temporary_buffer() |
ed6814f7 | 171 | { |
917a9fd4 SW |
172 | std::_Destroy(_M_buffer, _M_buffer + _M_len); |
173 | std::return_temporary_buffer(_M_buffer); | |
174 | } | |
ed6814f7 | 175 | |
917a9fd4 SW |
176 | private: |
177 | // Disable copy constructor and assignment operator. | |
178 | _Temporary_buffer(const _Temporary_buffer&); | |
83042fca PC |
179 | |
180 | void | |
181 | operator=(const _Temporary_buffer&); | |
917a9fd4 | 182 | }; |
ed6814f7 | 183 | |
917a9fd4 SW |
184 | template<typename _ForwardIterator, typename _Tp> |
185 | _Temporary_buffer<_ForwardIterator, _Tp>:: | |
186 | _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) | |
ed6814f7 | 187 | : _M_original_len(std::distance(__first, __last)), |
83042fca | 188 | _M_len(0), _M_buffer(0) |
917a9fd4 | 189 | { |
bc2631e0 | 190 | __try |
83042fca | 191 | { |
acb8a4ef PC |
192 | std::pair<pointer, size_type> __p(std::get_temporary_buffer< |
193 | value_type>(_M_original_len)); | |
83042fca PC |
194 | _M_buffer = __p.first; |
195 | _M_len = __p.second; | |
ff2ea587 PC |
196 | if (!__is_pod(_Tp) && _M_len > 0) |
197 | std::uninitialized_fill_n(_M_buffer, _M_len, *__first); | |
83042fca | 198 | } |
bc2631e0 | 199 | __catch(...) |
ed6814f7 | 200 | { |
83042fca | 201 | std::return_temporary_buffer(_M_buffer); |
ed6814f7 | 202 | _M_buffer = 0; |
83042fca | 203 | _M_len = 0; |
ed6814f7 | 204 | __throw_exception_again; |
83042fca | 205 | } |
917a9fd4 | 206 | } |
3cbc7af0 BK |
207 | |
208 | _GLIBCXX_END_NAMESPACE | |
725dc051 | 209 | |
046d30f4 | 210 | #endif /* _STL_TEMPBUF_H */ |
725dc051 | 211 |