//===-------------------------- cxa_vector.cpp ---------------------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is dual licensed under the MIT and the University of Illinois Open5// Source Licenses. See LICENSE.TXT for details.6//7//8// This file implements the "Array Construction and Destruction APIs"9// http://mentorembedded.github.io/cxx-abi/abi.html#array-ctor10//11//===----------------------------------------------------------------------===//1213#include "cxxabi.h"1415#include <exception> // for std::terminate1617namespace __cxxabiv1 {1819#if 020#pragma mark --Helper routines and classes --21#endif2223namespace {24inline static size_t __get_element_count ( void *p ) {25return static_cast <size_t *> (p)[-1];26}2728inline static void __set_element_count ( void *p, size_t element_count ) {29static_cast <size_t *> (p)[-1] = element_count;30}313233// A pair of classes to simplify exception handling and control flow.34// They get passed a block of memory in the constructor, and unless the35// 'release' method is called, they deallocate the memory in the destructor.36// Preferred usage is to allocate some memory, attach it to one of these objects,37// and then, when all the operations to set up the memory block have succeeded,38// call 'release'. If any of the setup operations fail, or an exception is39// thrown, then the block is automatically deallocated.40//41// The only difference between these two classes is the signature for the42// deallocation function (to match new2/new3 and delete2/delete3.43class st_heap_block2 {44public:45typedef void (*dealloc_f)(void *);4647st_heap_block2 ( dealloc_f dealloc, void *ptr )48: dealloc_ ( dealloc ), ptr_ ( ptr ), enabled_ ( true ) {}49~st_heap_block2 () { if ( enabled_ ) dealloc_ ( ptr_ ) ; }50void release () { enabled_ = false; }5152private:53dealloc_f dealloc_;54void *ptr_;55bool enabled_;56};5758class st_heap_block3 {59public:60typedef void (*dealloc_f)(void *, size_t);6162st_heap_block3 ( dealloc_f dealloc, void *ptr, size_t size )63: dealloc_ ( dealloc ), ptr_ ( ptr ), size_ ( size ), enabled_ ( true ) {}64~st_heap_block3 () { if ( enabled_ ) dealloc_ ( ptr_, size_ ) ; }65void release () { enabled_ = false; }6667private:68dealloc_f dealloc_;69void *ptr_;70size_t size_;71bool enabled_;72};7374class st_cxa_cleanup {75public:76typedef void (*destruct_f)(void *);7778st_cxa_cleanup ( void *ptr, size_t &idx, size_t element_size, destruct_f destructor )79: ptr_ ( ptr ), idx_ ( idx ), element_size_ ( element_size ),80destructor_ ( destructor ), enabled_ ( true ) {}81~st_cxa_cleanup () {82if ( enabled_ )83__cxa_vec_cleanup ( ptr_, idx_, element_size_, destructor_ );84}8586void release () { enabled_ = false; }8788private:89void *ptr_;90size_t &idx_;91size_t element_size_;92destruct_f destructor_;93bool enabled_;94};9596class st_terminate {97public:98st_terminate ( bool enabled = true ) : enabled_ ( enabled ) {}99~st_terminate () { if ( enabled_ ) std::terminate (); }100void release () { enabled_ = false; }101private:102bool enabled_ ;103};104}105106#if 0107#pragma mark --Externally visible routines--108#endif109110extern "C" {111112// Equivalent to113//114// __cxa_vec_new2(element_count, element_size, padding_size, constructor,115// destructor, &::operator new[], &::operator delete[])116_LIBCXXABI_FUNC_VIS void *117__cxa_vec_new(size_t element_count, size_t element_size, size_t padding_size,118void (*constructor)(void *), void (*destructor)(void *)) {119return __cxa_vec_new2 ( element_count, element_size, padding_size,120constructor, destructor, &::operator new [], &::operator delete [] );121}122123124125// Given the number and size of elements for an array and the non-negative126// size of prefix padding for a cookie, allocate space (using alloc) for127// the array preceded by the specified padding, initialize the cookie if128// the padding is non-zero, and call the given constructor on each element.129// Return the address of the array proper, after the padding.130//131// If alloc throws an exception, rethrow the exception. If alloc returns132// NULL, return NULL. If the constructor throws an exception, call133// destructor for any already constructed elements, and rethrow the134// exception. If the destructor throws an exception, call std::terminate.135//136// The constructor may be NULL, in which case it must not be called. If the137// padding_size is zero, the destructor may be NULL; in that case it must138// not be called.139//140// Neither alloc nor dealloc may be NULL.141_LIBCXXABI_FUNC_VIS void *142__cxa_vec_new2(size_t element_count, size_t element_size, size_t padding_size,143void (*constructor)(void *), void (*destructor)(void *),144void *(*alloc)(size_t), void (*dealloc)(void *)) {145const size_t heap_size = element_count * element_size + padding_size;146char * const heap_block = static_cast<char *> ( alloc ( heap_size ));147char *vec_base = heap_block;148149if ( NULL != vec_base ) {150st_heap_block2 heap ( dealloc, heap_block );151152// put the padding before the array elements153if ( 0 != padding_size ) {154vec_base += padding_size;155__set_element_count ( vec_base, element_count );156}157158// Construct the elements159__cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor );160heap.release (); // We're good!161}162163return vec_base;164}165166167// Same as __cxa_vec_new2 except that the deallocation function takes both168// the object address and its size.169_LIBCXXABI_FUNC_VIS void *170__cxa_vec_new3(size_t element_count, size_t element_size, size_t padding_size,171void (*constructor)(void *), void (*destructor)(void *),172void *(*alloc)(size_t), void (*dealloc)(void *, size_t)) {173const size_t heap_size = element_count * element_size + padding_size;174char * const heap_block = static_cast<char *> ( alloc ( heap_size ));175char *vec_base = heap_block;176177if ( NULL != vec_base ) {178st_heap_block3 heap ( dealloc, heap_block, heap_size );179180// put the padding before the array elements181if ( 0 != padding_size ) {182vec_base += padding_size;183__set_element_count ( vec_base, element_count );184}185186// Construct the elements187__cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor );188heap.release (); // We're good!189}190191return vec_base;192}193194195// Given the (data) addresses of a destination and a source array, an196// element count and an element size, call the given copy constructor to197// copy each element from the source array to the destination array. The198// copy constructor's arguments are the destination address and source199// address, respectively. If an exception occurs, call the given destructor200// (if non-NULL) on each copied element and rethrow. If the destructor201// throws an exception, call terminate(). The constructor and or destructor202// pointers may be NULL. If either is NULL, no action is taken when it203// would have been called.204205_LIBCXXABI_FUNC_VIS void __cxa_vec_cctor(void *dest_array, void *src_array,206size_t element_count,207size_t element_size,208void (*constructor)(void *, void *),209void (*destructor)(void *)) {210if ( NULL != constructor ) {211size_t idx = 0;212char *src_ptr = static_cast<char *>(src_array);213char *dest_ptr = static_cast<char *>(dest_array);214st_cxa_cleanup cleanup ( dest_array, idx, element_size, destructor );215216for ( idx = 0; idx < element_count;217++idx, src_ptr += element_size, dest_ptr += element_size )218constructor ( dest_ptr, src_ptr );219cleanup.release (); // We're good!220}221}222223224// Given the (data) address of an array, not including any cookie padding,225// and the number and size of its elements, call the given constructor on226// each element. If the constructor throws an exception, call the given227// destructor for any already-constructed elements, and rethrow the228// exception. If the destructor throws an exception, call terminate(). The229// constructor and/or destructor pointers may be NULL. If either is NULL,230// no action is taken when it would have been called.231_LIBCXXABI_FUNC_VIS void232__cxa_vec_ctor(void *array_address, size_t element_count, size_t element_size,233void (*constructor)(void *), void (*destructor)(void *)) {234if ( NULL != constructor ) {235size_t idx;236char *ptr = static_cast <char *> ( array_address );237st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor );238239// Construct the elements240for ( idx = 0; idx < element_count; ++idx, ptr += element_size )241constructor ( ptr );242cleanup.release (); // We're good!243}244}245246// Given the (data) address of an array, the number of elements, and the247// size of its elements, call the given destructor on each element. If the248// destructor throws an exception, rethrow after destroying the remaining249// elements if possible. If the destructor throws a second exception, call250// terminate(). The destructor pointer may be NULL, in which case this251// routine does nothing.252_LIBCXXABI_FUNC_VIS void __cxa_vec_dtor(void *array_address,253size_t element_count,254size_t element_size,255void (*destructor)(void *)) {256if ( NULL != destructor ) {257char *ptr = static_cast <char *> (array_address);258size_t idx = element_count;259st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor );260{261st_terminate exception_guard (__cxa_uncaught_exception ());262ptr += element_count * element_size; // one past the last element263264while ( idx-- > 0 ) {265ptr -= element_size;266destructor ( ptr );267}268exception_guard.release (); // We're good !269}270cleanup.release (); // We're still good!271}272}273274// Given the (data) address of an array, the number of elements, and the275// size of its elements, call the given destructor on each element. If the276// destructor throws an exception, call terminate(). The destructor pointer277// may be NULL, in which case this routine does nothing.278_LIBCXXABI_FUNC_VIS void __cxa_vec_cleanup(void *array_address,279size_t element_count,280size_t element_size,281void (*destructor)(void *)) {282if ( NULL != destructor ) {283char *ptr = static_cast <char *> (array_address);284size_t idx = element_count;285st_terminate exception_guard;286287ptr += element_count * element_size; // one past the last element288while ( idx-- > 0 ) {289ptr -= element_size;290destructor ( ptr );291}292exception_guard.release (); // We're done!293}294}295296297// If the array_address is NULL, return immediately. Otherwise, given the298// (data) address of an array, the non-negative size of prefix padding for299// the cookie, and the size of its elements, call the given destructor on300// each element, using the cookie to determine the number of elements, and301// then delete the space by calling ::operator delete[](void *). If the302// destructor throws an exception, rethrow after (a) destroying the303// remaining elements, and (b) deallocating the storage. If the destructor304// throws a second exception, call terminate(). If padding_size is 0, the305// destructor pointer must be NULL. If the destructor pointer is NULL, no306// destructor call is to be made.307//308// The intent of this function is to permit an implementation to call this309// function when confronted with an expression of the form delete[] p in310// the source code, provided that the default deallocation function can be311// used. Therefore, the semantics of this function are consistent with312// those required by the standard. The requirement that the deallocation313// function be called even if the destructor throws an exception derives314// from the resolution to DR 353 to the C++ standard, which was adopted in315// April, 2003.316_LIBCXXABI_FUNC_VIS void __cxa_vec_delete(void *array_address,317size_t element_size,318size_t padding_size,319void (*destructor)(void *)) {320__cxa_vec_delete2 ( array_address, element_size, padding_size,321destructor, &::operator delete [] );322}323324// Same as __cxa_vec_delete, except that the given function is used for325// deallocation instead of the default delete function. If dealloc throws326// an exception, the result is undefined. The dealloc pointer may not be327// NULL.328_LIBCXXABI_FUNC_VIS void329__cxa_vec_delete2(void *array_address, size_t element_size, size_t padding_size,330void (*destructor)(void *), void (*dealloc)(void *)) {331if ( NULL != array_address ) {332char *vec_base = static_cast <char *> (array_address);333char *heap_block = vec_base - padding_size;334st_heap_block2 heap ( dealloc, heap_block );335336if ( 0 != padding_size && NULL != destructor ) // call the destructors337__cxa_vec_dtor ( array_address, __get_element_count ( vec_base ),338element_size, destructor );339}340}341342343// Same as __cxa_vec_delete, except that the given function is used for344// deallocation instead of the default delete function. The deallocation345// function takes both the object address and its size. If dealloc throws346// an exception, the result is undefined. The dealloc pointer may not be347// NULL.348_LIBCXXABI_FUNC_VIS void349__cxa_vec_delete3(void *array_address, size_t element_size, size_t padding_size,350void (*destructor)(void *), void (*dealloc)(void *, size_t)) {351if ( NULL != array_address ) {352char *vec_base = static_cast <char *> (array_address);353char *heap_block = vec_base - padding_size;354const size_t element_count = padding_size ? __get_element_count ( vec_base ) : 0;355const size_t heap_block_size = element_size * element_count + padding_size;356st_heap_block3 heap ( dealloc, heap_block, heap_block_size );357358if ( 0 != padding_size && NULL != destructor ) // call the destructors359__cxa_vec_dtor ( array_address, element_count, element_size, destructor );360}361}362363364} // extern "C"365366} // abi367368369