]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/vec.cc
target-def.h (TARGET_CXX_GET_COOKIE_SIZE, [...]): Define.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / vec.cc
1 // New abi Support -*- C++ -*-
2
3 // Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // GCC 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
18 // along with GCC; see the file COPYING. If not, write to
19 // the Free Software Foundation, 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, 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 // Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
32
33 #include <cxxabi.h>
34 #include <new>
35 #include <exception>
36 #include <exception_defines.h>
37 #include "unwind-cxx.h"
38
39 namespace __cxxabiv1
40 {
41 namespace
42 {
43 struct uncatch_exception
44 {
45 uncatch_exception();
46 ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
47
48 __cxa_exception* p;
49
50 private:
51 uncatch_exception&
52 operator=(const uncatch_exception&);
53
54 uncatch_exception(const uncatch_exception&);
55 };
56
57 uncatch_exception::uncatch_exception() : p(0)
58 {
59 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
60
61 p = globals->caughtExceptions;
62 p->handlerCount -= 1;
63 globals->caughtExceptions = p->nextException;
64 globals->uncaughtExceptions += 1;
65 }
66 }
67
68 // Allocate and construct array.
69 extern "C" void *
70 __cxa_vec_new(std::size_t element_count,
71 std::size_t element_size,
72 std::size_t padding_size,
73 void (*constructor) (void *),
74 void (*destructor) (void *))
75 {
76 return __cxa_vec_new2(element_count, element_size, padding_size,
77 constructor, destructor,
78 &operator new[], &operator delete []);
79 }
80
81 extern "C" void *
82 __cxa_vec_new2(std::size_t element_count,
83 std::size_t element_size,
84 std::size_t padding_size,
85 void (*constructor) (void *),
86 void (*destructor) (void *),
87 void *(*alloc) (std::size_t),
88 void (*dealloc) (void *))
89 {
90 std::size_t size = element_count * element_size + padding_size;
91 char *base = static_cast <char *> (alloc (size));
92 if (!base)
93 return base;
94
95 if (padding_size)
96 {
97 base += padding_size;
98 reinterpret_cast <std::size_t *> (base)[-1] = element_count;
99 #ifdef __ARM_EABI__
100 // ARM EABI array cookies also contain the element size.
101 reinterpret_cast <std::size_t *> (base)[-2] = element_size;
102 #endif
103 }
104 try
105 {
106 __cxa_vec_ctor(base, element_count, element_size,
107 constructor, destructor);
108 }
109 catch (...)
110 {
111 {
112 uncatch_exception ue;
113 dealloc(base - padding_size);
114 }
115 __throw_exception_again;
116 }
117 return base;
118 }
119
120 extern "C" void *
121 __cxa_vec_new3(std::size_t element_count,
122 std::size_t element_size,
123 std::size_t padding_size,
124 void (*constructor) (void *),
125 void (*destructor) (void *),
126 void *(*alloc) (std::size_t),
127 void (*dealloc) (void *, std::size_t))
128 {
129 std::size_t size = element_count * element_size + padding_size;
130 char *base = static_cast<char *>(alloc (size));
131 if (!base)
132 return base;
133
134 if (padding_size)
135 {
136 base += padding_size;
137 reinterpret_cast<std::size_t *>(base)[-1] = element_count;
138 #ifdef __ARM_EABI__
139 // ARM EABI array cookies also contain the element size.
140 reinterpret_cast <std::size_t *> (base)[-2] = element_size;
141 #endif
142 }
143 try
144 {
145 __cxa_vec_ctor(base, element_count, element_size,
146 constructor, destructor);
147 }
148 catch (...)
149 {
150 {
151 uncatch_exception ue;
152 dealloc(base - padding_size, size);
153 }
154 __throw_exception_again;
155 }
156 return base;
157 }
158
159 // Construct array.
160 extern "C" void
161 __cxa_vec_ctor(void *array_address,
162 std::size_t element_count,
163 std::size_t element_size,
164 void (*constructor) (void *),
165 void (*destructor) (void *))
166 {
167 std::size_t ix = 0;
168 char *ptr = static_cast<char *>(array_address);
169
170 try
171 {
172 if (constructor)
173 for (; ix != element_count; ix++, ptr += element_size)
174 constructor(ptr);
175 }
176 catch (...)
177 {
178 {
179 uncatch_exception ue;
180 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
181 }
182 __throw_exception_again;
183 }
184 }
185
186 // Construct an array by copying.
187 extern "C" void
188 __cxa_vec_cctor(void *dest_array,
189 void *src_array,
190 std::size_t element_count,
191 std::size_t element_size,
192 void (*constructor) (void *, void *),
193 void (*destructor) (void *))
194 {
195 std::size_t ix = 0;
196 char *dest_ptr = static_cast<char *>(dest_array);
197 char *src_ptr = static_cast<char *>(src_array);
198
199 try
200 {
201 if (constructor)
202 for (; ix != element_count;
203 ix++, src_ptr += element_size, dest_ptr += element_size)
204 constructor(dest_ptr, src_ptr);
205 }
206 catch (...)
207 {
208 {
209 uncatch_exception ue;
210 __cxa_vec_cleanup(dest_array, ix, element_size, destructor);
211 }
212 __throw_exception_again;
213 }
214 }
215
216 // Destruct array.
217 extern "C" void
218 __cxa_vec_dtor(void *array_address,
219 std::size_t element_count,
220 std::size_t element_size,
221 void (*destructor) (void *))
222 {
223 if (destructor)
224 {
225 char *ptr = static_cast<char *>(array_address);
226 std::size_t ix = element_count;
227
228 ptr += element_count * element_size;
229
230 try
231 {
232 while (ix--)
233 {
234 ptr -= element_size;
235 destructor(ptr);
236 }
237 }
238 catch (...)
239 {
240 {
241 uncatch_exception ue;
242 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
243 }
244 __throw_exception_again;
245 }
246 }
247 }
248
249 // Destruct array as a result of throwing an exception.
250 // [except.ctor]/3 If a destructor called during stack unwinding
251 // exits with an exception, terminate is called.
252 extern "C" void
253 __cxa_vec_cleanup(void *array_address,
254 std::size_t element_count,
255 std::size_t element_size,
256 void (*destructor) (void *))
257 {
258 if (destructor)
259 {
260 char *ptr = static_cast <char *> (array_address);
261 std::size_t ix = element_count;
262
263 ptr += element_count * element_size;
264
265 try
266 {
267 while (ix--)
268 {
269 ptr -= element_size;
270 destructor(ptr);
271 }
272 }
273 catch (...)
274 {
275 std::terminate();
276 }
277 }
278 }
279
280 // Destruct and release array.
281 extern "C" void
282 __cxa_vec_delete(void *array_address,
283 std::size_t element_size,
284 std::size_t padding_size,
285 void (*destructor) (void *))
286 {
287 __cxa_vec_delete2(array_address, element_size, padding_size,
288 destructor,
289 &operator delete []);
290 }
291
292 extern "C" void
293 __cxa_vec_delete2(void *array_address,
294 std::size_t element_size,
295 std::size_t padding_size,
296 void (*destructor) (void *),
297 void (*dealloc) (void *))
298 {
299 if (!array_address)
300 return;
301
302 char* base = static_cast<char *>(array_address);
303
304 if (padding_size)
305 {
306 std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
307 base -= padding_size;
308 try
309 {
310 __cxa_vec_dtor(array_address, element_count, element_size,
311 destructor);
312 }
313 catch (...)
314 {
315 {
316 uncatch_exception ue;
317 dealloc(base);
318 }
319 __throw_exception_again;
320 }
321 }
322 dealloc(base);
323 }
324
325 extern "C" void
326 __cxa_vec_delete3(void *array_address,
327 std::size_t element_size,
328 std::size_t padding_size,
329 void (*destructor) (void *),
330 void (*dealloc) (void *, std::size_t))
331 {
332 if (!array_address)
333 return;
334
335 char* base = static_cast <char *> (array_address);
336 std::size_t size = 0;
337
338 if (padding_size)
339 {
340 std::size_t element_count = reinterpret_cast<std::size_t *> (base)[-1];
341 base -= padding_size;
342 size = element_count * element_size + padding_size;
343 try
344 {
345 __cxa_vec_dtor(array_address, element_count, element_size,
346 destructor);
347 }
348 catch (...)
349 {
350 {
351 uncatch_exception ue;
352 dealloc(base, size);
353 }
354 __throw_exception_again;
355 }
356 }
357 dealloc(base, size);
358 }
359 } // namespace __cxxabiv1
360