1 // New abi Support -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2003, 2004, 2009 Free Software Foundation, Inc.
5 // This file is part of GCC.
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 3, or (at your option)
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.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
31 #include <exception_defines.h>
32 #include "unwind-cxx.h"
38 struct uncatch_exception
41 ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
47 operator=(const uncatch_exception&);
49 uncatch_exception(const uncatch_exception&);
52 uncatch_exception::uncatch_exception() : p(0)
54 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
56 p = globals->caughtExceptions;
58 globals->caughtExceptions = p->nextException;
59 globals->uncaughtExceptions += 1;
63 // Allocate and construct array.
65 __cxa_vec_new(std::size_t element_count,
66 std::size_t element_size,
67 std::size_t padding_size,
68 __cxa_cdtor_type constructor,
69 __cxa_cdtor_type destructor)
71 return __cxa_vec_new2(element_count, element_size, padding_size,
72 constructor, destructor,
73 &operator new[], &operator delete []);
77 __cxa_vec_new2(std::size_t element_count,
78 std::size_t element_size,
79 std::size_t padding_size,
80 __cxa_cdtor_type constructor,
81 __cxa_cdtor_type destructor,
82 void *(*alloc) (std::size_t),
83 void (*dealloc) (void *))
85 std::size_t size = element_count * element_size + padding_size;
86 char *base = static_cast <char *> (alloc (size));
93 reinterpret_cast <std::size_t *> (base)[-1] = element_count;
94 #ifdef _GLIBCXX_ELTSIZE_IN_COOKIE
95 reinterpret_cast <std::size_t *> (base)[-2] = element_size;
100 __cxa_vec_ctor(base, element_count, element_size,
101 constructor, destructor);
106 uncatch_exception ue;
107 // Core issue 901 will probably be resolved such that a
108 // deleted operator delete means not freeing memory here.
110 dealloc(base - padding_size);
112 __throw_exception_again;
118 __cxa_vec_new3(std::size_t element_count,
119 std::size_t element_size,
120 std::size_t padding_size,
121 __cxa_cdtor_type constructor,
122 __cxa_cdtor_type destructor,
123 void *(*alloc) (std::size_t),
124 void (*dealloc) (void *, std::size_t))
126 std::size_t size = element_count * element_size + padding_size;
127 char *base = static_cast<char *>(alloc (size));
133 base += padding_size;
134 reinterpret_cast<std::size_t *>(base)[-1] = element_count;
135 #ifdef _GLIBCXX_ELTSIZE_IN_COOKIE
136 reinterpret_cast <std::size_t *> (base)[-2] = element_size;
141 __cxa_vec_ctor(base, element_count, element_size,
142 constructor, destructor);
147 uncatch_exception ue;
149 dealloc(base - padding_size, size);
151 __throw_exception_again;
157 extern "C" __cxa_vec_ctor_return_type
158 __cxa_vec_ctor(void *array_address,
159 std::size_t element_count,
160 std::size_t element_size,
161 __cxa_cdtor_type constructor,
162 __cxa_cdtor_type destructor)
165 char *ptr = static_cast<char *>(array_address);
170 for (; ix != element_count; ix++, ptr += element_size)
176 uncatch_exception ue;
177 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
179 __throw_exception_again;
181 _GLIBCXX_CXA_VEC_CTOR_RETURN (array_address);
184 // Construct an array by copying.
185 extern "C" __cxa_vec_ctor_return_type
186 __cxa_vec_cctor(void *dest_array,
188 std::size_t element_count,
189 std::size_t element_size,
190 __cxa_cdtor_return_type (*constructor) (void *, void *),
191 __cxa_cdtor_type destructor)
194 char *dest_ptr = static_cast<char *>(dest_array);
195 char *src_ptr = static_cast<char *>(src_array);
200 for (; ix != element_count;
201 ix++, src_ptr += element_size, dest_ptr += element_size)
202 constructor(dest_ptr, src_ptr);
207 uncatch_exception ue;
208 __cxa_vec_cleanup(dest_array, ix, element_size, destructor);
210 __throw_exception_again;
212 _GLIBCXX_CXA_VEC_CTOR_RETURN (dest_array);
217 __cxa_vec_dtor(void *array_address,
218 std::size_t element_count,
219 std::size_t element_size,
220 __cxa_cdtor_type destructor)
224 char *ptr = static_cast<char *>(array_address);
225 std::size_t ix = element_count;
227 ptr += element_count * element_size;
240 uncatch_exception ue;
241 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
243 __throw_exception_again;
248 // Destruct array as a result of throwing an exception.
249 // [except.ctor]/3 If a destructor called during stack unwinding
250 // exits with an exception, terminate is called.
252 __cxa_vec_cleanup(void *array_address,
253 std::size_t element_count,
254 std::size_t element_size,
255 __cxa_cdtor_type destructor) throw()
259 char *ptr = static_cast <char *> (array_address);
260 std::size_t ix = element_count;
262 ptr += element_count * element_size;
279 // Destruct and release array.
281 __cxa_vec_delete(void *array_address,
282 std::size_t element_size,
283 std::size_t padding_size,
284 __cxa_cdtor_type destructor)
286 __cxa_vec_delete2(array_address, element_size, padding_size,
288 &operator delete []);
292 __cxa_vec_delete2(void *array_address,
293 std::size_t element_size,
294 std::size_t padding_size,
295 __cxa_cdtor_type destructor,
296 void (*dealloc) (void *))
301 char* base = static_cast<char *>(array_address);
305 std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
306 base -= padding_size;
309 __cxa_vec_dtor(array_address, element_count, element_size,
315 uncatch_exception ue;
318 __throw_exception_again;
325 __cxa_vec_delete3(void *array_address,
326 std::size_t element_size,
327 std::size_t padding_size,
328 __cxa_cdtor_type destructor,
329 void (*dealloc) (void *, std::size_t))
334 char* base = static_cast <char *> (array_address);
335 std::size_t size = 0;
339 std::size_t element_count = reinterpret_cast<std::size_t *> (base)[-1];
340 base -= padding_size;
341 size = element_count * element_size + padding_size;
344 __cxa_vec_dtor(array_address, element_count, element_size,
350 uncatch_exception ue;
353 __throw_exception_again;
358 } // namespace __cxxabiv1
360 #if defined(__arm__) && defined(__ARM_EABI__)
362 // The ARM C++ ABI requires that the library provide these additional
363 // helper functions. There are placed in this file, despite being
364 // architecture-specifier, so that the compiler can inline the __cxa
365 // functions into these functions as appropriate.
370 __aeabi_vec_ctor_nocookie_nodtor (void *array_address,
371 abi::__cxa_cdtor_type constructor,
372 std::size_t element_size,
373 std::size_t element_count)
375 return abi::__cxa_vec_ctor (array_address, element_count, element_size,
376 constructor, /*destructor=*/NULL);
380 __aeabi_vec_ctor_cookie_nodtor (void *array_address,
381 abi::__cxa_cdtor_type constructor,
382 std::size_t element_size,
383 std::size_t element_count)
385 if (array_address == NULL)
388 array_address = reinterpret_cast<std::size_t *>(array_address) + 2;
389 reinterpret_cast<std::size_t *>(array_address)[-2] = element_size;
390 reinterpret_cast<std::size_t *>(array_address)[-1] = element_count;
391 return abi::__cxa_vec_ctor (array_address,
392 element_count, element_size,
393 constructor, /*destructor=*/NULL);
397 __aeabi_vec_cctor_nocookie_nodtor (void *dest_array,
399 std::size_t element_size,
400 std::size_t element_count,
401 void *(*constructor) (void *, void *))
403 return abi::__cxa_vec_cctor (dest_array, src_array,
404 element_count, element_size,
409 __aeabi_vec_new_cookie_noctor (std::size_t element_size,
410 std::size_t element_count)
412 return abi::__cxa_vec_new(element_count, element_size,
413 2 * sizeof (std::size_t),
414 /*constructor=*/NULL, /*destructor=*/NULL);
418 __aeabi_vec_new_nocookie (std::size_t element_size,
419 std::size_t element_count,
420 abi::__cxa_cdtor_type constructor)
422 return abi::__cxa_vec_new (element_count, element_size, 0, constructor,
427 __aeabi_vec_new_cookie_nodtor (std::size_t element_size,
428 std::size_t element_count,
429 abi::__cxa_cdtor_type constructor)
431 return abi::__cxa_vec_new(element_count, element_size,
432 2 * sizeof (std::size_t),
437 __aeabi_vec_new_cookie(std::size_t element_size,
438 std::size_t element_count,
439 abi::__cxa_cdtor_type constructor,
440 abi::__cxa_cdtor_type destructor)
442 return abi::__cxa_vec_new (element_count, element_size,
443 2 * sizeof (std::size_t),
444 constructor, destructor);
449 __aeabi_vec_dtor (void *array_address,
450 abi::__cxa_cdtor_type destructor,
451 std::size_t element_size,
452 std::size_t element_count)
454 abi::__cxa_vec_dtor (array_address, element_count, element_size,
456 return reinterpret_cast<std::size_t*> (array_address) - 2;
460 __aeabi_vec_dtor_cookie (void *array_address,
461 abi::__cxa_cdtor_type destructor)
466 abi::__cxa_vec_dtor (array_address,
467 reinterpret_cast<std::size_t *>(array_address)[-1],
468 reinterpret_cast<std::size_t *>(array_address)[-2],
470 return reinterpret_cast<std::size_t*> (array_address) - 2;
475 __aeabi_vec_delete (void *array_address,
476 abi::__cxa_cdtor_type destructor)
481 abi::__cxa_vec_delete (array_address,
482 reinterpret_cast<std::size_t *>(array_address)[-2],
483 2 * sizeof (std::size_t),
488 __aeabi_vec_delete3 (void *array_address,
489 abi::__cxa_cdtor_type destructor,
490 void (*dealloc) (void *, std::size_t))
495 abi::__cxa_vec_delete3 (array_address,
496 reinterpret_cast<std::size_t *>(array_address)[-2],
497 2 * sizeof (std::size_t),
498 destructor, dealloc);
502 __aeabi_vec_delete3_nodtor (void *array_address,
503 void (*dealloc) (void *, std::size_t))
508 abi::__cxa_vec_delete3 (array_address,
509 reinterpret_cast<std::size_t *>(array_address)[-2],
510 2 * sizeof (std::size_t),
511 /*destructor=*/NULL, dealloc);
513 } // namespace __aeabiv1
515 #endif // defined(__arm__) && defined(__ARM_EABI__)