1 // New abi Support -*- C++ -*-
3 // Copyright (C) 2000-2017 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 <bits/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;
62 // Compute the total size with overflow checking.
63 std::size_t compute_size(std::size_t element_count
,
64 std::size_t element_size
,
65 std::size_t padding_size
)
67 if (element_size
&& element_count
> std::size_t(-1) / element_size
)
68 _GLIBCXX_THROW_OR_ABORT(std::bad_alloc());
69 std::size_t size
= element_count
* element_size
;
70 if (size
+ padding_size
< size
)
71 _GLIBCXX_THROW_OR_ABORT(std::bad_alloc());
72 return size
+ padding_size
;
76 // Allocate and construct array.
78 __cxa_vec_new(std::size_t element_count
,
79 std::size_t element_size
,
80 std::size_t padding_size
,
81 __cxa_cdtor_type constructor
,
82 __cxa_cdtor_type destructor
)
84 return __cxa_vec_new2(element_count
, element_size
, padding_size
,
85 constructor
, destructor
,
86 &operator new[], &operator delete []);
90 __cxa_vec_new2(std::size_t element_count
,
91 std::size_t element_size
,
92 std::size_t padding_size
,
93 __cxa_cdtor_type constructor
,
94 __cxa_cdtor_type destructor
,
95 void *(*alloc
) (std::size_t),
96 void (*dealloc
) (void *))
99 = compute_size(element_count
, element_size
, padding_size
);
100 char *base
= static_cast <char *> (alloc (size
));
106 base
+= padding_size
;
107 reinterpret_cast <std::size_t *> (base
)[-1] = element_count
;
108 #ifdef _GLIBCXX_ELTSIZE_IN_COOKIE
109 reinterpret_cast <std::size_t *> (base
)[-2] = element_size
;
114 __cxa_vec_ctor(base
, element_count
, element_size
,
115 constructor
, destructor
);
120 uncatch_exception ue
;
121 // Core issue 901 will probably be resolved such that a
122 // deleted operator delete means not freeing memory here.
124 dealloc(base
- padding_size
);
126 __throw_exception_again
;
132 __cxa_vec_new3(std::size_t element_count
,
133 std::size_t element_size
,
134 std::size_t padding_size
,
135 __cxa_cdtor_type constructor
,
136 __cxa_cdtor_type destructor
,
137 void *(*alloc
) (std::size_t),
138 void (*dealloc
) (void *, std::size_t))
141 = compute_size(element_count
, element_size
, padding_size
);
142 char *base
= static_cast<char *>(alloc (size
));
148 base
+= padding_size
;
149 reinterpret_cast<std::size_t *>(base
)[-1] = element_count
;
150 #ifdef _GLIBCXX_ELTSIZE_IN_COOKIE
151 reinterpret_cast <std::size_t *> (base
)[-2] = element_size
;
156 __cxa_vec_ctor(base
, element_count
, element_size
,
157 constructor
, destructor
);
162 uncatch_exception ue
;
164 dealloc(base
- padding_size
, size
);
166 __throw_exception_again
;
172 extern "C" __cxa_vec_ctor_return_type
173 __cxa_vec_ctor(void *array_address
,
174 std::size_t element_count
,
175 std::size_t element_size
,
176 __cxa_cdtor_type constructor
,
177 __cxa_cdtor_type destructor
)
180 char *ptr
= static_cast<char *>(array_address
);
185 for (; ix
!= element_count
; ix
++, ptr
+= element_size
)
191 uncatch_exception ue
;
192 __cxa_vec_cleanup(array_address
, ix
, element_size
, destructor
);
194 __throw_exception_again
;
196 _GLIBCXX_CXA_VEC_CTOR_RETURN (array_address
);
199 // Construct an array by copying.
200 extern "C" __cxa_vec_ctor_return_type
201 __cxa_vec_cctor(void *dest_array
,
203 std::size_t element_count
,
204 std::size_t element_size
,
205 __cxa_cdtor_return_type (*constructor
) (void *, void *),
206 __cxa_cdtor_type destructor
)
209 char *dest_ptr
= static_cast<char *>(dest_array
);
210 char *src_ptr
= static_cast<char *>(src_array
);
215 for (; ix
!= element_count
;
216 ix
++, src_ptr
+= element_size
, dest_ptr
+= element_size
)
217 constructor(dest_ptr
, src_ptr
);
222 uncatch_exception ue
;
223 __cxa_vec_cleanup(dest_array
, ix
, element_size
, destructor
);
225 __throw_exception_again
;
227 _GLIBCXX_CXA_VEC_CTOR_RETURN (dest_array
);
232 __cxa_vec_dtor(void *array_address
,
233 std::size_t element_count
,
234 std::size_t element_size
,
235 __cxa_cdtor_type destructor
)
239 char *ptr
= static_cast<char *>(array_address
);
240 std::size_t ix
= element_count
;
242 ptr
+= element_count
* element_size
;
255 uncatch_exception ue
;
256 __cxa_vec_cleanup(array_address
, ix
, element_size
, destructor
);
258 __throw_exception_again
;
263 // Destruct array as a result of throwing an exception.
264 // [except.ctor]/3 If a destructor called during stack unwinding
265 // exits with an exception, terminate is called.
267 __cxa_vec_cleanup(void *array_address
,
268 std::size_t element_count
,
269 std::size_t element_size
,
270 __cxa_cdtor_type destructor
) throw()
274 char *ptr
= static_cast <char *> (array_address
);
275 std::size_t ix
= element_count
;
277 ptr
+= element_count
* element_size
;
294 // Destruct and release array.
296 __cxa_vec_delete(void *array_address
,
297 std::size_t element_size
,
298 std::size_t padding_size
,
299 __cxa_cdtor_type destructor
)
301 __cxa_vec_delete2(array_address
, element_size
, padding_size
,
303 &operator delete []);
307 __cxa_vec_delete2(void *array_address
,
308 std::size_t element_size
,
309 std::size_t padding_size
,
310 __cxa_cdtor_type destructor
,
311 void (*dealloc
) (void *))
316 char* base
= static_cast<char *>(array_address
);
320 std::size_t element_count
= reinterpret_cast<std::size_t *>(base
)[-1];
321 base
-= padding_size
;
324 __cxa_vec_dtor(array_address
, element_count
, element_size
,
330 uncatch_exception ue
;
333 __throw_exception_again
;
340 __cxa_vec_delete3(void *array_address
,
341 std::size_t element_size
,
342 std::size_t padding_size
,
343 __cxa_cdtor_type destructor
,
344 void (*dealloc
) (void *, std::size_t))
349 char* base
= static_cast <char *> (array_address
);
350 std::size_t size
= 0;
354 std::size_t element_count
= reinterpret_cast<std::size_t *> (base
)[-1];
355 base
-= padding_size
;
356 size
= element_count
* element_size
+ padding_size
;
359 __cxa_vec_dtor(array_address
, element_count
, element_size
,
365 uncatch_exception ue
;
368 __throw_exception_again
;
373 } // namespace __cxxabiv1
375 #if defined(__arm__) && defined(__ARM_EABI__)
377 // The ARM C++ ABI requires that the library provide these additional
378 // helper functions. There are placed in this file, despite being
379 // architecture-specifier, so that the compiler can inline the __cxa
380 // functions into these functions as appropriate.
385 __aeabi_vec_ctor_nocookie_nodtor (void *array_address
,
386 abi::__cxa_cdtor_type constructor
,
387 std::size_t element_size
,
388 std::size_t element_count
)
390 return abi::__cxa_vec_ctor (array_address
, element_count
, element_size
,
391 constructor
, /*destructor=*/NULL
);
395 __aeabi_vec_ctor_cookie_nodtor (void *array_address
,
396 abi::__cxa_cdtor_type constructor
,
397 std::size_t element_size
,
398 std::size_t element_count
)
400 if (array_address
== NULL
)
403 array_address
= reinterpret_cast<std::size_t *>(array_address
) + 2;
404 reinterpret_cast<std::size_t *>(array_address
)[-2] = element_size
;
405 reinterpret_cast<std::size_t *>(array_address
)[-1] = element_count
;
406 return abi::__cxa_vec_ctor (array_address
,
407 element_count
, element_size
,
408 constructor
, /*destructor=*/NULL
);
412 __aeabi_vec_cctor_nocookie_nodtor (void *dest_array
,
414 std::size_t element_size
,
415 std::size_t element_count
,
416 void *(*constructor
) (void *, void *))
418 return abi::__cxa_vec_cctor (dest_array
, src_array
,
419 element_count
, element_size
,
424 __aeabi_vec_new_cookie_noctor (std::size_t element_size
,
425 std::size_t element_count
)
427 return abi::__cxa_vec_new(element_count
, element_size
,
428 2 * sizeof (std::size_t),
429 /*constructor=*/NULL
, /*destructor=*/NULL
);
433 __aeabi_vec_new_nocookie (std::size_t element_size
,
434 std::size_t element_count
,
435 abi::__cxa_cdtor_type constructor
)
437 return abi::__cxa_vec_new (element_count
, element_size
, 0, constructor
,
442 __aeabi_vec_new_cookie_nodtor (std::size_t element_size
,
443 std::size_t element_count
,
444 abi::__cxa_cdtor_type constructor
)
446 return abi::__cxa_vec_new(element_count
, element_size
,
447 2 * sizeof (std::size_t),
452 __aeabi_vec_new_cookie(std::size_t element_size
,
453 std::size_t element_count
,
454 abi::__cxa_cdtor_type constructor
,
455 abi::__cxa_cdtor_type destructor
)
457 return abi::__cxa_vec_new (element_count
, element_size
,
458 2 * sizeof (std::size_t),
459 constructor
, destructor
);
464 __aeabi_vec_dtor (void *array_address
,
465 abi::__cxa_cdtor_type destructor
,
466 std::size_t element_size
,
467 std::size_t element_count
)
469 abi::__cxa_vec_dtor (array_address
, element_count
, element_size
,
471 return reinterpret_cast<std::size_t*> (array_address
) - 2;
475 __aeabi_vec_dtor_cookie (void *array_address
,
476 abi::__cxa_cdtor_type destructor
)
481 abi::__cxa_vec_dtor (array_address
,
482 reinterpret_cast<std::size_t *>(array_address
)[-1],
483 reinterpret_cast<std::size_t *>(array_address
)[-2],
485 return reinterpret_cast<std::size_t*> (array_address
) - 2;
490 __aeabi_vec_delete (void *array_address
,
491 abi::__cxa_cdtor_type destructor
)
496 abi::__cxa_vec_delete (array_address
,
497 reinterpret_cast<std::size_t *>(array_address
)[-2],
498 2 * sizeof (std::size_t),
503 __aeabi_vec_delete3 (void *array_address
,
504 abi::__cxa_cdtor_type destructor
,
505 void (*dealloc
) (void *, std::size_t))
510 abi::__cxa_vec_delete3 (array_address
,
511 reinterpret_cast<std::size_t *>(array_address
)[-2],
512 2 * sizeof (std::size_t),
513 destructor
, dealloc
);
517 __aeabi_vec_delete3_nodtor (void *array_address
,
518 void (*dealloc
) (void *, std::size_t))
523 abi::__cxa_vec_delete3 (array_address
,
524 reinterpret_cast<std::size_t *>(array_address
)[-2],
525 2 * sizeof (std::size_t),
526 /*destructor=*/NULL
, dealloc
);
528 } // namespace __aeabiv1
530 #endif // defined(__arm__) && defined(__ARM_EABI__)