]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/libsupc++/vec.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / vec.cc
CommitLineData
85d54afa 1// New abi Support -*- C++ -*-
2
f1717362 3// Copyright (C) 2000-2016 Free Software Foundation, Inc.
85d54afa 4//
908dad4c 5// This file is part of GCC.
85d54afa 6//
908dad4c 7// GCC is free software; you can redistribute it and/or modify
458f31b3 8// it under the terms of the GNU General Public License as published by
6bc9506f 9// the Free Software Foundation; either version 3, or (at your option)
458f31b3 10// any later version.
11
908dad4c 12// GCC is distributed in the hope that it will be useful,
458f31b3 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.
16
6bc9506f 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.
20
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/>.
458f31b3 25
85d54afa 26// Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
27
458f31b3 28#include <cxxabi.h>
29#include <new>
30#include <exception>
dee74d2b 31#include <bits/exception_defines.h>
df4b504c 32#include "unwind-cxx.h"
458f31b3 33
34namespace __cxxabiv1
35{
85d54afa 36 namespace
37 {
38 struct uncatch_exception
39 {
0a421700 40 uncatch_exception();
df4b504c 41 ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
85d54afa 42
0a421700 43 __cxa_exception* p;
44
45 private:
46 uncatch_exception&
47 operator=(const uncatch_exception&);
48
49 uncatch_exception(const uncatch_exception&);
85d54afa 50 };
df4b504c 51
0a421700 52 uncatch_exception::uncatch_exception() : p(0)
df4b504c 53 {
54 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
55
56 p = globals->caughtExceptions;
57 p->handlerCount -= 1;
58 globals->caughtExceptions = p->nextException;
59 globals->uncaughtExceptions += 1;
60 }
5fd168b6 61
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)
66 {
67 if (element_size && element_count > std::size_t(-1) / element_size)
1590c7d4 68 _GLIBCXX_THROW_OR_ABORT(std::bad_alloc());
5fd168b6 69 std::size_t size = element_count * element_size;
70 if (size + padding_size < size)
1590c7d4 71 _GLIBCXX_THROW_OR_ABORT(std::bad_alloc());
5fd168b6 72 return size + padding_size;
73 }
85d54afa 74 }
458f31b3 75
85d54afa 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,
921d9bf3 81 __cxa_cdtor_type constructor,
82 __cxa_cdtor_type destructor)
85d54afa 83 {
84 return __cxa_vec_new2(element_count, element_size, padding_size,
85 constructor, destructor,
86 &operator new[], &operator delete []);
87 }
458f31b3 88
85d54afa 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,
921d9bf3 93 __cxa_cdtor_type constructor,
94 __cxa_cdtor_type destructor,
72b8dc4b 95 void *(*alloc) (std::size_t),
85d54afa 96 void (*dealloc) (void *))
97 {
5fd168b6 98 std::size_t size
99 = compute_size(element_count, element_size, padding_size);
85d54afa 100 char *base = static_cast <char *> (alloc (size));
6dd0bb13 101 if (!base)
102 return base;
103
85d54afa 104 if (padding_size)
105 {
106 base += padding_size;
107 reinterpret_cast <std::size_t *> (base)[-1] = element_count;
479a838e 108#ifdef _GLIBCXX_ELTSIZE_IN_COOKIE
600f4be7 109 reinterpret_cast <std::size_t *> (base)[-2] = element_size;
110#endif
85d54afa 111 }
b66f4546 112 __try
85d54afa 113 {
114 __cxa_vec_ctor(base, element_count, element_size,
115 constructor, destructor);
116 }
b66f4546 117 __catch(...)
85d54afa 118 {
119 {
120 uncatch_exception ue;
57811700 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);
85d54afa 125 }
126 __throw_exception_again;
127 }
128 return base;
129 }
458f31b3 130
85d54afa 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,
921d9bf3 135 __cxa_cdtor_type constructor,
136 __cxa_cdtor_type destructor,
85d54afa 137 void *(*alloc) (std::size_t),
138 void (*dealloc) (void *, std::size_t))
139 {
5fd168b6 140 std::size_t size
141 = compute_size(element_count, element_size, padding_size);
85d54afa 142 char *base = static_cast<char *>(alloc (size));
6dd0bb13 143 if (!base)
144 return base;
85d54afa 145
146 if (padding_size)
655b9c51 147 {
85d54afa 148 base += padding_size;
149 reinterpret_cast<std::size_t *>(base)[-1] = element_count;
479a838e 150#ifdef _GLIBCXX_ELTSIZE_IN_COOKIE
600f4be7 151 reinterpret_cast <std::size_t *> (base)[-2] = element_size;
152#endif
655b9c51 153 }
b66f4546 154 __try
85d54afa 155 {
156 __cxa_vec_ctor(base, element_count, element_size,
157 constructor, destructor);
158 }
b66f4546 159 __catch(...)
85d54afa 160 {
161 {
162 uncatch_exception ue;
57811700 163 if (dealloc)
164 dealloc(base - padding_size, size);
85d54afa 165 }
166 __throw_exception_again;
167 }
168 return base;
169 }
458f31b3 170
85d54afa 171 // Construct array.
b5a97e54 172 extern "C" __cxa_vec_ctor_return_type
85d54afa 173 __cxa_vec_ctor(void *array_address,
174 std::size_t element_count,
175 std::size_t element_size,
921d9bf3 176 __cxa_cdtor_type constructor,
177 __cxa_cdtor_type destructor)
85d54afa 178 {
179 std::size_t ix = 0;
180 char *ptr = static_cast<char *>(array_address);
181
b66f4546 182 __try
655b9c51 183 {
85d54afa 184 if (constructor)
185 for (; ix != element_count; ix++, ptr += element_size)
186 constructor(ptr);
655b9c51 187 }
b66f4546 188 __catch(...)
85d54afa 189 {
190 {
191 uncatch_exception ue;
5549e2e9 192 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
85d54afa 193 }
194 __throw_exception_again;
195 }
b5a97e54 196 _GLIBCXX_CXA_VEC_CTOR_RETURN (array_address);
85d54afa 197 }
458f31b3 198
85d54afa 199 // Construct an array by copying.
b5a97e54 200 extern "C" __cxa_vec_ctor_return_type
85d54afa 201 __cxa_vec_cctor(void *dest_array,
202 void *src_array,
203 std::size_t element_count,
204 std::size_t element_size,
921d9bf3 205 __cxa_cdtor_return_type (*constructor) (void *, void *),
206 __cxa_cdtor_type destructor)
85d54afa 207 {
208 std::size_t ix = 0;
209 char *dest_ptr = static_cast<char *>(dest_array);
210 char *src_ptr = static_cast<char *>(src_array);
211
b66f4546 212 __try
655b9c51 213 {
85d54afa 214 if (constructor)
215 for (; ix != element_count;
216 ix++, src_ptr += element_size, dest_ptr += element_size)
217 constructor(dest_ptr, src_ptr);
655b9c51 218 }
b66f4546 219 __catch(...)
85d54afa 220 {
221 {
222 uncatch_exception ue;
5549e2e9 223 __cxa_vec_cleanup(dest_array, ix, element_size, destructor);
85d54afa 224 }
225 __throw_exception_again;
226 }
b5a97e54 227 _GLIBCXX_CXA_VEC_CTOR_RETURN (dest_array);
85d54afa 228 }
229
230 // Destruct array.
231 extern "C" void
232 __cxa_vec_dtor(void *array_address,
270d165e 233 std::size_t element_count,
234 std::size_t element_size,
921d9bf3 235 __cxa_cdtor_type destructor)
85d54afa 236 {
237 if (destructor)
655b9c51 238 {
85d54afa 239 char *ptr = static_cast<char *>(array_address);
240 std::size_t ix = element_count;
5549e2e9 241
85d54afa 242 ptr += element_count * element_size;
5549e2e9 243
b66f4546 244 __try
85d54afa 245 {
246 while (ix--)
247 {
248 ptr -= element_size;
249 destructor(ptr);
250 }
251 }
b66f4546 252 __catch(...)
655b9c51 253 {
85d54afa 254 {
255 uncatch_exception ue;
5549e2e9 256 __cxa_vec_cleanup(array_address, ix, element_size, destructor);
85d54afa 257 }
258 __throw_exception_again;
655b9c51 259 }
85d54afa 260 }
261 }
458f31b3 262
5549e2e9 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,
85522548 270 __cxa_cdtor_type destructor) throw()
5549e2e9 271 {
272 if (destructor)
273 {
274 char *ptr = static_cast <char *> (array_address);
275 std::size_t ix = element_count;
276
277 ptr += element_count * element_size;
278
b66f4546 279 __try
5549e2e9 280 {
281 while (ix--)
282 {
283 ptr -= element_size;
284 destructor(ptr);
285 }
286 }
b66f4546 287 __catch(...)
5549e2e9 288 {
289 std::terminate();
290 }
291 }
292 }
293
85d54afa 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,
921d9bf3 299 __cxa_cdtor_type destructor)
85d54afa 300 {
301 __cxa_vec_delete2(array_address, element_size, padding_size,
302 destructor,
303 &operator delete []);
304 }
458f31b3 305
85d54afa 306 extern "C" void
307 __cxa_vec_delete2(void *array_address,
308 std::size_t element_size,
309 std::size_t padding_size,
921d9bf3 310 __cxa_cdtor_type destructor,
85d54afa 311 void (*dealloc) (void *))
312 {
1dc58c21 313 if (!array_address)
314 return;
315
68667c0a 316 char* base = static_cast<char *>(array_address);
458f31b3 317
85d54afa 318 if (padding_size)
319 {
320 std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
321 base -= padding_size;
b66f4546 322 __try
655b9c51 323 {
85d54afa 324 __cxa_vec_dtor(array_address, element_count, element_size,
325 destructor);
655b9c51 326 }
b66f4546 327 __catch(...)
655b9c51 328 {
85d54afa 329 {
330 uncatch_exception ue;
331 dealloc(base);
332 }
333 __throw_exception_again;
655b9c51 334 }
85d54afa 335 }
336 dealloc(base);
337 }
458f31b3 338
85d54afa 339 extern "C" void
340 __cxa_vec_delete3(void *array_address,
341 std::size_t element_size,
342 std::size_t padding_size,
921d9bf3 343 __cxa_cdtor_type destructor,
85d54afa 344 void (*dealloc) (void *, std::size_t))
345 {
1dc58c21 346 if (!array_address)
347 return;
348
68667c0a 349 char* base = static_cast <char *> (array_address);
350 std::size_t size = 0;
351
85d54afa 352 if (padding_size)
353 {
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;
b66f4546 357 __try
85d54afa 358 {
359 __cxa_vec_dtor(array_address, element_count, element_size,
360 destructor);
361 }
b66f4546 362 __catch(...)
85d54afa 363 {
364 {
365 uncatch_exception ue;
366 dealloc(base, size);
367 }
368 __throw_exception_again;
369 }
370 }
371 dealloc(base, size);
372 }
458f31b3 373} // namespace __cxxabiv1
aa078d95 374
921d9bf3 375#if defined(__arm__) && defined(__ARM_EABI__)
376
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.
381
382namespace __aeabiv1
383{
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)
389 {
390 return abi::__cxa_vec_ctor (array_address, element_count, element_size,
391 constructor, /*destructor=*/NULL);
392 }
393
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)
399 {
400 if (array_address == NULL)
401 return NULL;
402
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);
409 }
410
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 *))
417 {
418 return abi::__cxa_vec_cctor (dest_array, src_array,
419 element_count, element_size,
420 constructor, NULL);
421 }
422
423 extern "C" void *
424 __aeabi_vec_new_cookie_noctor (std::size_t element_size,
425 std::size_t element_count)
426 {
427 return abi::__cxa_vec_new(element_count, element_size,
428 2 * sizeof (std::size_t),
429 /*constructor=*/NULL, /*destructor=*/NULL);
430 }
431
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)
436 {
437 return abi::__cxa_vec_new (element_count, element_size, 0, constructor,
438 NULL);
439 }
440
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)
445 {
446 return abi::__cxa_vec_new(element_count, element_size,
447 2 * sizeof (std::size_t),
448 constructor, NULL);
449 }
450
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)
456 {
457 return abi::__cxa_vec_new (element_count, element_size,
458 2 * sizeof (std::size_t),
459 constructor, destructor);
460 }
461
462
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)
468 {
469 abi::__cxa_vec_dtor (array_address, element_count, element_size,
470 destructor);
471 return reinterpret_cast<std::size_t*> (array_address) - 2;
472 }
473
474 extern "C" void *
475 __aeabi_vec_dtor_cookie (void *array_address,
476 abi::__cxa_cdtor_type destructor)
477 {
abee15ec 478 if (!array_address)
479 return NULL;
480
921d9bf3 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;
486 }
487
488
489 extern "C" void
490 __aeabi_vec_delete (void *array_address,
491 abi::__cxa_cdtor_type destructor)
492 {
abee15ec 493 if (!array_address)
494 return;
495
921d9bf3 496 abi::__cxa_vec_delete (array_address,
497 reinterpret_cast<std::size_t *>(array_address)[-2],
498 2 * sizeof (std::size_t),
499 destructor);
500 }
501
502 extern "C" void
503 __aeabi_vec_delete3 (void *array_address,
504 abi::__cxa_cdtor_type destructor,
505 void (*dealloc) (void *, std::size_t))
506 {
abee15ec 507 if (!array_address)
508 return;
509
921d9bf3 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);
514 }
515
516 extern "C" void
517 __aeabi_vec_delete3_nodtor (void *array_address,
518 void (*dealloc) (void *, std::size_t))
519 {
abee15ec 520 if (!array_address)
521 return;
522
921d9bf3 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);
527 }
921d9bf3 528} // namespace __aeabiv1
529
530#endif // defined(__arm__) && defined(__ARM_EABI__)