| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "cxxabi.h" |
| | #include "__cxxabi_config.h" |
| |
|
| | #include <exception> |
| | #include <new> |
| |
|
| | #include "abort_message.h" |
| |
|
| | #ifndef __has_builtin |
| | #define __has_builtin(x) 0 |
| | #endif |
| |
|
| | namespace __cxxabiv1 { |
| |
|
| | |
| | |
| | |
| |
|
| | namespace { |
| | inline static size_t __get_element_count ( void *p ) { |
| | return static_cast <size_t *> (p)[-1]; |
| | } |
| |
|
| | inline static void __set_element_count ( void *p, size_t element_count ) { |
| | static_cast <size_t *> (p)[-1] = element_count; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | class st_heap_block2 { |
| | public: |
| | typedef void (*dealloc_f)(void *); |
| |
|
| | st_heap_block2 ( dealloc_f dealloc, void *ptr ) |
| | : dealloc_ ( dealloc ), ptr_ ( ptr ), enabled_ ( true ) {} |
| | ~st_heap_block2 () { if ( enabled_ ) dealloc_ ( ptr_ ) ; } |
| | void release () { enabled_ = false; } |
| |
|
| | private: |
| | dealloc_f dealloc_; |
| | void *ptr_; |
| | bool enabled_; |
| | }; |
| |
|
| | class st_heap_block3 { |
| | public: |
| | typedef void (*dealloc_f)(void *, size_t); |
| |
|
| | st_heap_block3 ( dealloc_f dealloc, void *ptr, size_t size ) |
| | : dealloc_ ( dealloc ), ptr_ ( ptr ), size_ ( size ), enabled_ ( true ) {} |
| | ~st_heap_block3 () { if ( enabled_ ) dealloc_ ( ptr_, size_ ) ; } |
| | void release () { enabled_ = false; } |
| |
|
| | private: |
| | dealloc_f dealloc_; |
| | void *ptr_; |
| | size_t size_; |
| | bool enabled_; |
| | }; |
| |
|
| | class st_cxa_cleanup { |
| | public: |
| | typedef void (*destruct_f)(void *); |
| |
|
| | st_cxa_cleanup ( void *ptr, size_t &idx, size_t element_size, destruct_f destructor ) |
| | : ptr_ ( ptr ), idx_ ( idx ), element_size_ ( element_size ), |
| | destructor_ ( destructor ), enabled_ ( true ) {} |
| | ~st_cxa_cleanup () { |
| | if ( enabled_ ) |
| | __cxa_vec_cleanup ( ptr_, idx_, element_size_, destructor_ ); |
| | } |
| |
|
| | void release () { enabled_ = false; } |
| |
|
| | private: |
| | void *ptr_; |
| | size_t &idx_; |
| | size_t element_size_; |
| | destruct_f destructor_; |
| | bool enabled_; |
| | }; |
| |
|
| | class st_terminate { |
| | public: |
| | st_terminate ( bool enabled = true ) : enabled_ ( enabled ) {} |
| | ~st_terminate () { if ( enabled_ ) std::terminate (); } |
| | void release () { enabled_ = false; } |
| | private: |
| | bool enabled_ ; |
| | }; |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | namespace { |
| | _LIBCXXABI_NORETURN |
| | void throw_bad_array_new_length() { |
| | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
| | throw std::bad_array_new_length(); |
| | #else |
| | abort_message("__cxa_vec_new failed to allocate memory"); |
| | #endif |
| | } |
| |
|
| | bool mul_overflow(size_t x, size_t y, size_t *res) { |
| | #if (defined(_LIBCXXABI_COMPILER_CLANG) && __has_builtin(__builtin_mul_overflow)) \ |
| | || defined(_LIBCXXABI_COMPILER_GCC) |
| | return __builtin_mul_overflow(x, y, res); |
| | #else |
| | *res = x * y; |
| | return x && ((*res / x) != y); |
| | #endif |
| | } |
| |
|
| | bool add_overflow(size_t x, size_t y, size_t *res) { |
| | #if (defined(_LIBCXXABI_COMPILER_CLANG) && __has_builtin(__builtin_add_overflow)) \ |
| | || defined(_LIBCXXABI_COMPILER_GCC) |
| | return __builtin_add_overflow(x, y, res); |
| | #else |
| | *res = x + y; |
| | return *res < y; |
| | #endif |
| | } |
| |
|
| | size_t calculate_allocation_size_or_throw(size_t element_count, |
| | size_t element_size, |
| | size_t padding_size) { |
| | size_t element_heap_size; |
| | if (mul_overflow(element_count, element_size, &element_heap_size)) |
| | throw_bad_array_new_length(); |
| |
|
| | size_t allocation_size; |
| | if (add_overflow(element_heap_size, padding_size, &allocation_size)) |
| | throw_bad_array_new_length(); |
| |
|
| | return allocation_size; |
| | } |
| |
|
| | } |
| |
|
| | extern "C" { |
| |
|
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void * |
| | __cxa_vec_new(size_t element_count, size_t element_size, size_t padding_size, |
| | void (*constructor)(void *), void (*destructor)(void *)) { |
| | return __cxa_vec_new2 ( element_count, element_size, padding_size, |
| | constructor, destructor, &::operator new [], &::operator delete [] ); |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void * |
| | __cxa_vec_new2(size_t element_count, size_t element_size, size_t padding_size, |
| | void (*constructor)(void *), void (*destructor)(void *), |
| | void *(*alloc)(size_t), void (*dealloc)(void *)) { |
| | const size_t heap_size = calculate_allocation_size_or_throw( |
| | element_count, element_size, padding_size); |
| | char* const heap_block = static_cast<char*>(alloc(heap_size)); |
| | char* vec_base = heap_block; |
| |
|
| | if (NULL != vec_base) { |
| | st_heap_block2 heap(dealloc, heap_block); |
| |
|
| | |
| | if ( 0 != padding_size ) { |
| | vec_base += padding_size; |
| | __set_element_count ( vec_base, element_count ); |
| | } |
| |
|
| | |
| | __cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor ); |
| | heap.release (); |
| | } |
| |
|
| | return vec_base; |
| | } |
| |
|
| |
|
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void * |
| | __cxa_vec_new3(size_t element_count, size_t element_size, size_t padding_size, |
| | void (*constructor)(void *), void (*destructor)(void *), |
| | void *(*alloc)(size_t), void (*dealloc)(void *, size_t)) { |
| | const size_t heap_size = calculate_allocation_size_or_throw( |
| | element_count, element_size, padding_size); |
| | char* const heap_block = static_cast<char*>(alloc(heap_size)); |
| | char* vec_base = heap_block; |
| |
|
| | if (NULL != vec_base) { |
| | st_heap_block3 heap(dealloc, heap_block, heap_size); |
| |
|
| | |
| | if ( 0 != padding_size ) { |
| | vec_base += padding_size; |
| | __set_element_count ( vec_base, element_count ); |
| | } |
| |
|
| | |
| | __cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor ); |
| | heap.release (); |
| | } |
| |
|
| | return vec_base; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | _LIBCXXABI_FUNC_VIS void __cxa_vec_cctor(void *dest_array, void *src_array, |
| | size_t element_count, |
| | size_t element_size, |
| | void (*constructor)(void *, void *), |
| | void (*destructor)(void *)) { |
| | if ( NULL != constructor ) { |
| | size_t idx = 0; |
| | char *src_ptr = static_cast<char *>(src_array); |
| | char *dest_ptr = static_cast<char *>(dest_array); |
| | st_cxa_cleanup cleanup ( dest_array, idx, element_size, destructor ); |
| |
|
| | for ( idx = 0; idx < element_count; |
| | ++idx, src_ptr += element_size, dest_ptr += element_size ) |
| | constructor ( dest_ptr, src_ptr ); |
| | cleanup.release (); |
| | } |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void |
| | __cxa_vec_ctor(void *array_address, size_t element_count, size_t element_size, |
| | void (*constructor)(void *), void (*destructor)(void *)) { |
| | if ( NULL != constructor ) { |
| | size_t idx; |
| | char *ptr = static_cast <char *> ( array_address ); |
| | st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor ); |
| |
|
| | |
| | for ( idx = 0; idx < element_count; ++idx, ptr += element_size ) |
| | constructor ( ptr ); |
| | cleanup.release (); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void __cxa_vec_dtor(void *array_address, |
| | size_t element_count, |
| | size_t element_size, |
| | void (*destructor)(void *)) { |
| | if ( NULL != destructor ) { |
| | char *ptr = static_cast <char *> (array_address); |
| | size_t idx = element_count; |
| | st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor ); |
| | { |
| | st_terminate exception_guard (__cxa_uncaught_exception ()); |
| | ptr += element_count * element_size; |
| |
|
| | while ( idx-- > 0 ) { |
| | ptr -= element_size; |
| | destructor ( ptr ); |
| | } |
| | exception_guard.release (); |
| | } |
| | cleanup.release (); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void __cxa_vec_cleanup(void *array_address, |
| | size_t element_count, |
| | size_t element_size, |
| | void (*destructor)(void *)) { |
| | if ( NULL != destructor ) { |
| | char *ptr = static_cast <char *> (array_address); |
| | size_t idx = element_count; |
| | st_terminate exception_guard; |
| |
|
| | ptr += element_count * element_size; |
| | while ( idx-- > 0 ) { |
| | ptr -= element_size; |
| | destructor ( ptr ); |
| | } |
| | exception_guard.release (); |
| | } |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void __cxa_vec_delete(void *array_address, |
| | size_t element_size, |
| | size_t padding_size, |
| | void (*destructor)(void *)) { |
| | __cxa_vec_delete2 ( array_address, element_size, padding_size, |
| | destructor, &::operator delete [] ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void |
| | __cxa_vec_delete2(void *array_address, size_t element_size, size_t padding_size, |
| | void (*destructor)(void *), void (*dealloc)(void *)) { |
| | if ( NULL != array_address ) { |
| | char *vec_base = static_cast <char *> (array_address); |
| | char *heap_block = vec_base - padding_size; |
| | st_heap_block2 heap ( dealloc, heap_block ); |
| |
|
| | if ( 0 != padding_size && NULL != destructor ) |
| | __cxa_vec_dtor ( array_address, __get_element_count ( vec_base ), |
| | element_size, destructor ); |
| | } |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | _LIBCXXABI_FUNC_VIS void |
| | __cxa_vec_delete3(void *array_address, size_t element_size, size_t padding_size, |
| | void (*destructor)(void *), void (*dealloc)(void *, size_t)) { |
| | if ( NULL != array_address ) { |
| | char *vec_base = static_cast <char *> (array_address); |
| | char *heap_block = vec_base - padding_size; |
| | const size_t element_count = padding_size ? __get_element_count ( vec_base ) : 0; |
| | const size_t heap_block_size = element_size * element_count + padding_size; |
| | st_heap_block3 heap ( dealloc, heap_block, heap_block_size ); |
| |
|
| | if ( 0 != padding_size && NULL != destructor ) |
| | __cxa_vec_dtor ( array_address, element_count, element_size, destructor ); |
| | } |
| | } |
| |
|
| |
|
| | } |
| |
|
| | } |
| |
|