fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libstdc++-v3 / libsupc++ / vec.cc
blob86d41d9ea98b4a49dc868b6898d1cfc86b7a9043
1 // New abi Support -*- C++ -*-
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.
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>
33 #include <cxxabi.h>
34 #include <new>
35 #include <exception>
36 #include <exception_defines.h>
37 #include "unwind-cxx.h"
39 namespace __cxxabiv1
41 namespace
43 struct uncatch_exception
45 uncatch_exception();
46 ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
48 __cxa_exception* p;
50 private:
51 uncatch_exception&
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;
62 p->handlerCount -= 1;
63 globals->caughtExceptions = p->nextException;
64 globals->uncaughtExceptions += 1;
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 *))
76 return __cxa_vec_new2(element_count, element_size, padding_size,
77 constructor, destructor,
78 &operator new[], &operator delete []);
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 *))
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;
95 if (padding_size)
97 base += padding_size;
98 reinterpret_cast <std::size_t *> (base)[-1] = element_count;
102 __cxa_vec_ctor(base, element_count, element_size,
103 constructor, destructor);
105 catch (...)
108 uncatch_exception ue;
109 dealloc(base - padding_size);
111 __throw_exception_again;
113 return base;
116 extern "C" void *
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));
127 if (!base)
128 return base;
130 if (padding_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);
140 catch (...)
143 uncatch_exception ue;
144 dealloc(base - padding_size, size);
146 __throw_exception_again;
148 return base;
151 // Construct array.
152 extern "C" void
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 *))
159 std::size_t ix = 0;
160 char *ptr = static_cast<char *>(array_address);
164 if (constructor)
165 for (; ix != element_count; ix++, ptr += element_size)
166 constructor(ptr);
168 catch (...)
171 uncatch_exception ue;
172 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
174 __throw_exception_again;
178 // Construct an array by copying.
179 extern "C" void
180 __cxa_vec_cctor(void *dest_array,
181 void *src_array,
182 std::size_t element_count,
183 std::size_t element_size,
184 void (*constructor) (void *, void *),
185 void (*destructor) (void *))
187 std::size_t ix = 0;
188 char *dest_ptr = static_cast<char *>(dest_array);
189 char *src_ptr = static_cast<char *>(src_array);
193 if (constructor)
194 for (; ix != element_count;
195 ix++, src_ptr += element_size, dest_ptr += element_size)
196 constructor(dest_ptr, src_ptr);
198 catch (...)
201 uncatch_exception ue;
202 __cxa_vec_cleanup(dest_array, ix, element_size, destructor);
204 __throw_exception_again;
208 // Destruct array.
209 extern "C" void
210 __cxa_vec_dtor(void *array_address,
211 std::size_t element_count,
212 std::size_t element_size,
213 void (*destructor) (void *))
215 if (destructor)
217 char *ptr = static_cast<char *>(array_address);
218 std::size_t ix = element_count;
220 ptr += element_count * element_size;
224 while (ix--)
226 ptr -= element_size;
227 destructor(ptr);
230 catch (...)
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.
244 extern "C" void
245 __cxa_vec_cleanup(void *array_address,
246 std::size_t element_count,
247 std::size_t element_size,
248 void (*destructor) (void *))
250 if (destructor)
252 char *ptr = static_cast <char *> (array_address);
253 std::size_t ix = element_count;
255 ptr += element_count * element_size;
259 while (ix--)
261 ptr -= element_size;
262 destructor(ptr);
265 catch (...)
267 std::terminate();
272 // Destruct and release array.
273 extern "C" void
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,
280 destructor,
281 &operator delete []);
284 extern "C" void
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 *))
291 if (!array_address)
292 return;
294 char* base = static_cast<char *>(array_address);
296 if (padding_size)
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,
303 destructor);
305 catch (...)
308 uncatch_exception ue;
309 dealloc(base);
311 __throw_exception_again;
314 dealloc(base);
317 extern "C" void
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))
324 if (!array_address)
325 return;
327 char* base = static_cast <char *> (array_address);
328 std::size_t size = 0;
330 if (padding_size)
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,
338 destructor);
340 catch (...)
343 uncatch_exception ue;
344 dealloc(base, size);
346 __throw_exception_again;
349 dealloc(base, size);
351 } // namespace __cxxabiv1