1 // New abi Support -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2003, 2004 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 2, 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 // 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.
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.
31 // Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
36 #include <exception_defines.h>
37 #include "unwind-cxx.h"
43 struct uncatch_exception
46 ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
52 operator=(const uncatch_exception&);
54 uncatch_exception(const uncatch_exception&);
57 uncatch_exception::uncatch_exception() : p(0)
59 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
61 p = globals->caughtExceptions;
63 globals->caughtExceptions = p->nextException;
64 globals->uncaughtExceptions += 1;
68 // Allocate and construct array.
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 *))
76 return __cxa_vec_new2(element_count, element_size, padding_size,
77 constructor, destructor,
78 &operator new[], &operator delete []);
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 *))
90 std::size_t size = element_count * element_size + padding_size;
91 char *base = static_cast <char *> (alloc (size));
98 reinterpret_cast <std::size_t *> (base)[-1] = element_count;
102 __cxa_vec_ctor(base, element_count, element_size,
103 constructor, destructor);
108 uncatch_exception ue;
109 dealloc(base - padding_size);
111 __throw_exception_again;
117 __cxa_vec_new3(std::size_t element_count,
118 std::size_t element_size,
119 std::size_t padding_size,
120 void (*constructor) (void *),
121 void (*destructor) (void *),
122 void *(*alloc) (std::size_t),
123 void (*dealloc) (void *, std::size_t))
125 std::size_t size = element_count * element_size + padding_size;
126 char *base = static_cast<char *>(alloc (size));
132 base += padding_size;
133 reinterpret_cast<std::size_t *>(base)[-1] = element_count;
137 __cxa_vec_ctor(base, element_count, element_size,
138 constructor, destructor);
143 uncatch_exception ue;
144 dealloc(base - padding_size, size);
146 __throw_exception_again;
153 __cxa_vec_ctor(void *array_address,
154 std::size_t element_count,
155 std::size_t element_size,
156 void (*constructor) (void *),
157 void (*destructor) (void *))
160 char *ptr = static_cast<char *>(array_address);
165 for (; ix != element_count; ix++, ptr += element_size)
171 uncatch_exception ue;
172 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
174 __throw_exception_again;
178 // Construct an array by copying.
180 __cxa_vec_cctor(void *dest_array,
182 std::size_t element_count,
183 std::size_t element_size,
184 void (*constructor) (void *, void *),
185 void (*destructor) (void *))
188 char *dest_ptr = static_cast<char *>(dest_array);
189 char *src_ptr = static_cast<char *>(src_array);
194 for (; ix != element_count;
195 ix++, src_ptr += element_size, dest_ptr += element_size)
196 constructor(dest_ptr, src_ptr);
201 uncatch_exception ue;
202 __cxa_vec_cleanup(dest_array, ix, element_size, destructor);
204 __throw_exception_again;
210 __cxa_vec_dtor(void *array_address,
211 std::size_t element_count,
212 std::size_t element_size,
213 void (*destructor) (void *))
217 char *ptr = static_cast<char *>(array_address);
218 std::size_t ix = element_count;
220 ptr += element_count * element_size;
233 uncatch_exception ue;
234 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
236 __throw_exception_again;
241 // Destruct array as a result of throwing an exception.
242 // [except.ctor]/3 If a destructor called during stack unwinding
243 // exits with an exception, terminate is called.
245 __cxa_vec_cleanup(void *array_address,
246 std::size_t element_count,
247 std::size_t element_size,
248 void (*destructor) (void *))
252 char *ptr = static_cast <char *> (array_address);
253 std::size_t ix = element_count;
255 ptr += element_count * element_size;
272 // Destruct and release array.
274 __cxa_vec_delete(void *array_address,
275 std::size_t element_size,
276 std::size_t padding_size,
277 void (*destructor) (void *))
279 __cxa_vec_delete2(array_address, element_size, padding_size,
281 &operator delete []);
285 __cxa_vec_delete2(void *array_address,
286 std::size_t element_size,
287 std::size_t padding_size,
288 void (*destructor) (void *),
289 void (*dealloc) (void *))
294 char* base = static_cast<char *>(array_address);
298 std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
299 base -= padding_size;
302 __cxa_vec_dtor(array_address, element_count, element_size,
308 uncatch_exception ue;
311 __throw_exception_again;
318 __cxa_vec_delete3(void *array_address,
319 std::size_t element_size,
320 std::size_t padding_size,
321 void (*destructor) (void *),
322 void (*dealloc) (void *, std::size_t))
327 char* base = static_cast <char *> (array_address);
328 std::size_t size = 0;
332 std::size_t element_count = reinterpret_cast<std::size_t *> (base)[-1];
333 base -= padding_size;
334 size = element_count * element_size + padding_size;
337 __cxa_vec_dtor(array_address, element_count, element_size,
343 uncatch_exception ue;
346 __throw_exception_again;
351 } // namespace __cxxabiv1