* gnat.dg/gcov: New directory.
[official-gcc.git] / libstdc++-v3 / libsupc++ / vec.cc
blob0490e304d4718e4fa61a2a212f27eee82733ab0c
1 // New abi Support -*- C++ -*-
3 // Copyright (C) 2000-2017 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 <bits/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;
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.
77 extern "C" void *
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 []);
89 extern "C" void *
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 *))
98 std::size_t size
99 = compute_size(element_count, element_size, padding_size);
100 char *base = static_cast <char *> (alloc (size));
101 if (!base)
102 return base;
104 if (padding_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;
110 #endif
112 __try
114 __cxa_vec_ctor(base, element_count, element_size,
115 constructor, destructor);
117 __catch(...)
120 uncatch_exception ue;
121 // Core issue 901 will probably be resolved such that a
122 // deleted operator delete means not freeing memory here.
123 if (dealloc)
124 dealloc(base - padding_size);
126 __throw_exception_again;
128 return base;
131 extern "C" void *
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))
140 std::size_t size
141 = compute_size(element_count, element_size, padding_size);
142 char *base = static_cast<char *>(alloc (size));
143 if (!base)
144 return base;
146 if (padding_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;
152 #endif
154 __try
156 __cxa_vec_ctor(base, element_count, element_size,
157 constructor, destructor);
159 __catch(...)
162 uncatch_exception ue;
163 if (dealloc)
164 dealloc(base - padding_size, size);
166 __throw_exception_again;
168 return base;
171 // Construct array.
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)
179 std::size_t ix = 0;
180 char *ptr = static_cast<char *>(array_address);
182 __try
184 if (constructor)
185 for (; ix != element_count; ix++, ptr += element_size)
186 constructor(ptr);
188 __catch(...)
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,
202 void *src_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)
208 std::size_t ix = 0;
209 char *dest_ptr = static_cast<char *>(dest_array);
210 char *src_ptr = static_cast<char *>(src_array);
212 __try
214 if (constructor)
215 for (; ix != element_count;
216 ix++, src_ptr += element_size, dest_ptr += element_size)
217 constructor(dest_ptr, src_ptr);
219 __catch(...)
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);
230 // Destruct array.
231 extern "C" void
232 __cxa_vec_dtor(void *array_address,
233 std::size_t element_count,
234 std::size_t element_size,
235 __cxa_cdtor_type destructor)
237 if (destructor)
239 char *ptr = static_cast<char *>(array_address);
240 std::size_t ix = element_count;
242 ptr += element_count * element_size;
244 __try
246 while (ix--)
248 ptr -= element_size;
249 destructor(ptr);
252 __catch(...)
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.
266 extern "C" void
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()
272 if (destructor)
274 char *ptr = static_cast <char *> (array_address);
275 std::size_t ix = element_count;
277 ptr += element_count * element_size;
279 __try
281 while (ix--)
283 ptr -= element_size;
284 destructor(ptr);
287 __catch(...)
289 std::terminate();
294 // Destruct and release array.
295 extern "C" void
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,
302 destructor,
303 &operator delete []);
306 extern "C" void
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 *))
313 if (!array_address)
314 return;
316 char* base = static_cast<char *>(array_address);
318 if (padding_size)
320 std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
321 base -= padding_size;
322 __try
324 __cxa_vec_dtor(array_address, element_count, element_size,
325 destructor);
327 __catch(...)
330 uncatch_exception ue;
331 dealloc(base);
333 __throw_exception_again;
336 dealloc(base);
339 extern "C" void
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))
346 if (!array_address)
347 return;
349 char* base = static_cast <char *> (array_address);
350 std::size_t size = 0;
352 if (padding_size)
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;
357 __try
359 __cxa_vec_dtor(array_address, element_count, element_size,
360 destructor);
362 __catch(...)
365 uncatch_exception ue;
366 dealloc(base, size);
368 __throw_exception_again;
371 dealloc(base, size);
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.
382 namespace __aeabiv1
384 extern "C" void *
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);
394 extern "C" void *
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)
401 return 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);
411 extern "C" void *
412 __aeabi_vec_cctor_nocookie_nodtor (void *dest_array,
413 void *src_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,
420 constructor, NULL);
423 extern "C" void *
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);
432 extern "C" void *
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,
438 NULL);
441 extern "C" void *
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),
448 constructor, NULL);
451 extern "C" void *
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);
463 extern "C" void *
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,
470 destructor);
471 return reinterpret_cast<std::size_t*> (array_address) - 2;
474 extern "C" void *
475 __aeabi_vec_dtor_cookie (void *array_address,
476 abi::__cxa_cdtor_type destructor)
478 if (!array_address)
479 return NULL;
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],
484 destructor);
485 return reinterpret_cast<std::size_t*> (array_address) - 2;
489 extern "C" void
490 __aeabi_vec_delete (void *array_address,
491 abi::__cxa_cdtor_type destructor)
493 if (!array_address)
494 return;
496 abi::__cxa_vec_delete (array_address,
497 reinterpret_cast<std::size_t *>(array_address)[-2],
498 2 * sizeof (std::size_t),
499 destructor);
502 extern "C" void
503 __aeabi_vec_delete3 (void *array_address,
504 abi::__cxa_cdtor_type destructor,
505 void (*dealloc) (void *, std::size_t))
507 if (!array_address)
508 return;
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);
516 extern "C" void
517 __aeabi_vec_delete3_nodtor (void *array_address,
518 void (*dealloc) (void *, std::size_t))
520 if (!array_address)
521 return;
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__)