Reset branch to trunk.
[official-gcc.git] / trunk / libstdc++-v3 / libsupc++ / vec.cc
blob286401cdf1050f5d9193f92d766a91a71a377ebb
1 // New abi Support -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2003, 2004, 2009 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 3, 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 // 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>
28 #include <cxxabi.h>
29 #include <new>
30 #include <exception>
31 #include <exception_defines.h>
32 #include "unwind-cxx.h"
34 namespace __cxxabiv1
36 namespace
38 struct uncatch_exception
40 uncatch_exception();
41 ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
43 __cxa_exception* p;
45 private:
46 uncatch_exception&
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;
57 p->handlerCount -= 1;
58 globals->caughtExceptions = p->nextException;
59 globals->uncaughtExceptions += 1;
63 // Allocate and construct array.
64 extern "C" void *
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 []);
76 extern "C" void *
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));
87 if (!base)
88 return base;
90 if (padding_size)
92 base += padding_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;
96 #endif
98 __try
100 __cxa_vec_ctor(base, element_count, element_size,
101 constructor, destructor);
103 __catch(...)
106 uncatch_exception ue;
107 // Core issue 901 will probably be resolved such that a
108 // deleted operator delete means not freeing memory here.
109 if (dealloc)
110 dealloc(base - padding_size);
112 __throw_exception_again;
114 return base;
117 extern "C" void *
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));
128 if (!base)
129 return base;
131 if (padding_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;
137 #endif
139 __try
141 __cxa_vec_ctor(base, element_count, element_size,
142 constructor, destructor);
144 __catch(...)
147 uncatch_exception ue;
148 if (dealloc)
149 dealloc(base - padding_size, size);
151 __throw_exception_again;
153 return base;
156 // Construct array.
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)
164 std::size_t ix = 0;
165 char *ptr = static_cast<char *>(array_address);
167 __try
169 if (constructor)
170 for (; ix != element_count; ix++, ptr += element_size)
171 constructor(ptr);
173 __catch(...)
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,
187 void *src_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)
193 std::size_t ix = 0;
194 char *dest_ptr = static_cast<char *>(dest_array);
195 char *src_ptr = static_cast<char *>(src_array);
197 __try
199 if (constructor)
200 for (; ix != element_count;
201 ix++, src_ptr += element_size, dest_ptr += element_size)
202 constructor(dest_ptr, src_ptr);
204 __catch(...)
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);
215 // Destruct array.
216 extern "C" void
217 __cxa_vec_dtor(void *array_address,
218 std::size_t element_count,
219 std::size_t element_size,
220 __cxa_cdtor_type destructor)
222 if (destructor)
224 char *ptr = static_cast<char *>(array_address);
225 std::size_t ix = element_count;
227 ptr += element_count * element_size;
229 __try
231 while (ix--)
233 ptr -= element_size;
234 destructor(ptr);
237 __catch(...)
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.
251 extern "C" void
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()
257 if (destructor)
259 char *ptr = static_cast <char *> (array_address);
260 std::size_t ix = element_count;
262 ptr += element_count * element_size;
264 __try
266 while (ix--)
268 ptr -= element_size;
269 destructor(ptr);
272 __catch(...)
274 std::terminate();
279 // Destruct and release array.
280 extern "C" void
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,
287 destructor,
288 &operator delete []);
291 extern "C" void
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 *))
298 if (!array_address)
299 return;
301 char* base = static_cast<char *>(array_address);
303 if (padding_size)
305 std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
306 base -= padding_size;
307 __try
309 __cxa_vec_dtor(array_address, element_count, element_size,
310 destructor);
312 __catch(...)
315 uncatch_exception ue;
316 dealloc(base);
318 __throw_exception_again;
321 dealloc(base);
324 extern "C" void
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))
331 if (!array_address)
332 return;
334 char* base = static_cast <char *> (array_address);
335 std::size_t size = 0;
337 if (padding_size)
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;
342 __try
344 __cxa_vec_dtor(array_address, element_count, element_size,
345 destructor);
347 __catch(...)
350 uncatch_exception ue;
351 dealloc(base, size);
353 __throw_exception_again;
356 dealloc(base, size);
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.
367 namespace __aeabiv1
369 extern "C" void *
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);
379 extern "C" void *
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)
386 return 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);
396 extern "C" void *
397 __aeabi_vec_cctor_nocookie_nodtor (void *dest_array,
398 void *src_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,
405 constructor, NULL);
408 extern "C" void *
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);
417 extern "C" void *
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,
423 NULL);
426 extern "C" void *
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),
433 constructor, NULL);
436 extern "C" void *
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);
448 extern "C" void *
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,
455 destructor);
456 return reinterpret_cast<std::size_t*> (array_address) - 2;
459 extern "C" void *
460 __aeabi_vec_dtor_cookie (void *array_address,
461 abi::__cxa_cdtor_type destructor)
463 if (!array_address)
464 return NULL;
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],
469 destructor);
470 return reinterpret_cast<std::size_t*> (array_address) - 2;
474 extern "C" void
475 __aeabi_vec_delete (void *array_address,
476 abi::__cxa_cdtor_type destructor)
478 if (!array_address)
479 return;
481 abi::__cxa_vec_delete (array_address,
482 reinterpret_cast<std::size_t *>(array_address)[-2],
483 2 * sizeof (std::size_t),
484 destructor);
487 extern "C" void
488 __aeabi_vec_delete3 (void *array_address,
489 abi::__cxa_cdtor_type destructor,
490 void (*dealloc) (void *, std::size_t))
492 if (!array_address)
493 return;
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);
501 extern "C" void
502 __aeabi_vec_delete3_nodtor (void *array_address,
503 void (*dealloc) (void *, std::size_t))
505 if (!array_address)
506 return;
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__)