1 //===-------------------------- cxa_vector.cpp ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 // 9 // This file implements the "Array Construction and Destruction APIs" 10 // http://mentorembedded.github.io/cxx-abi/abi.html#array-ctor 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "cxxabi.h" 15 16 #include <exception> // for std::terminate 17 18 namespace __cxxabiv1 { 19 20 #if 0 21 #pragma mark --Helper routines and classes -- 22 #endif 23 24 namespace { 25 inline static size_t __get_element_count ( void *p ) { 26 return static_cast <size_t *> (p)[-1]; 27 } 28 29 inline static void __set_element_count ( void *p, size_t element_count ) { 30 static_cast <size_t *> (p)[-1] = element_count; 31 } 32 33 34 // A pair of classes to simplify exception handling and control flow. 35 // They get passed a block of memory in the constructor, and unless the 36 // 'release' method is called, they deallocate the memory in the destructor. 37 // Preferred usage is to allocate some memory, attach it to one of these objects, 38 // and then, when all the operations to set up the memory block have succeeded, 39 // call 'release'. If any of the setup operations fail, or an exception is 40 // thrown, then the block is automatically deallocated. 41 // 42 // The only difference between these two classes is the signature for the 43 // deallocation function (to match new2/new3 and delete2/delete3. 44 class st_heap_block2 { 45 public: 46 typedef void (*dealloc_f)(void *); 47 48 st_heap_block2 ( dealloc_f dealloc, void *ptr ) 49 : dealloc_ ( dealloc ), ptr_ ( ptr ), enabled_ ( true ) {} 50 ~st_heap_block2 () { if ( enabled_ ) dealloc_ ( ptr_ ) ; } 51 void release () { enabled_ = false; } 52 53 private: 54 dealloc_f dealloc_; 55 void *ptr_; 56 bool enabled_; 57 }; 58 59 class st_heap_block3 { 60 public: 61 typedef void (*dealloc_f)(void *, size_t); 62 63 st_heap_block3 ( dealloc_f dealloc, void *ptr, size_t size ) 64 : dealloc_ ( dealloc ), ptr_ ( ptr ), size_ ( size ), enabled_ ( true ) {} 65 ~st_heap_block3 () { if ( enabled_ ) dealloc_ ( ptr_, size_ ) ; } 66 void release () { enabled_ = false; } 67 68 private: 69 dealloc_f dealloc_; 70 void *ptr_; 71 size_t size_; 72 bool enabled_; 73 }; 74 75 class st_cxa_cleanup { 76 public: 77 typedef void (*destruct_f)(void *); 78 79 st_cxa_cleanup ( void *ptr, size_t &idx, size_t element_size, destruct_f destructor ) 80 : ptr_ ( ptr ), idx_ ( idx ), element_size_ ( element_size ), 81 destructor_ ( destructor ), enabled_ ( true ) {} 82 ~st_cxa_cleanup () { 83 if ( enabled_ ) 84 __cxa_vec_cleanup ( ptr_, idx_, element_size_, destructor_ ); 85 } 86 87 void release () { enabled_ = false; } 88 89 private: 90 void *ptr_; 91 size_t &idx_; 92 size_t element_size_; 93 destruct_f destructor_; 94 bool enabled_; 95 }; 96 97 class st_terminate { 98 public: 99 st_terminate ( bool enabled = true ) : enabled_ ( enabled ) {} 100 ~st_terminate () { if ( enabled_ ) std::terminate (); } 101 void release () { enabled_ = false; } 102 private: 103 bool enabled_ ; 104 }; 105 } 106 107 #if 0 108 #pragma mark --Externally visible routines-- 109 #endif 110 111 extern "C" { 112 113 // Equivalent to 114 // 115 // __cxa_vec_new2(element_count, element_size, padding_size, constructor, 116 // destructor, &::operator new[], &::operator delete[]) 117 void* __cxa_vec_new( 118 size_t element_count, size_t element_size, size_t padding_size, 119 void (*constructor)(void*), void (*destructor)(void*) ) { 120 121 return __cxa_vec_new2 ( element_count, element_size, padding_size, 122 constructor, destructor, &::operator new [], &::operator delete [] ); 123 } 124 125 126 127 // Given the number and size of elements for an array and the non-negative 128 // size of prefix padding for a cookie, allocate space (using alloc) for 129 // the array preceded by the specified padding, initialize the cookie if 130 // the padding is non-zero, and call the given constructor on each element. 131 // Return the address of the array proper, after the padding. 132 // 133 // If alloc throws an exception, rethrow the exception. If alloc returns 134 // NULL, return NULL. If the constructor throws an exception, call 135 // destructor for any already constructed elements, and rethrow the 136 // exception. If the destructor throws an exception, call std::terminate. 137 // 138 // The constructor may be NULL, in which case it must not be called. If the 139 // padding_size is zero, the destructor may be NULL; in that case it must 140 // not be called. 141 // 142 // Neither alloc nor dealloc may be NULL. 143 void* __cxa_vec_new2( 144 size_t element_count, size_t element_size, size_t padding_size, 145 void (*constructor)(void*), void (*destructor)(void*), 146 void* (*alloc)(size_t), void (*dealloc)(void*) ) { 147 148 const size_t heap_size = element_count * element_size + padding_size; 149 char * const heap_block = static_cast<char *> ( alloc ( heap_size )); 150 char *vec_base = heap_block; 151 152 if ( NULL != vec_base ) { 153 st_heap_block2 heap ( dealloc, heap_block ); 154 155 // put the padding before the array elements 156 if ( 0 != padding_size ) { 157 vec_base += padding_size; 158 __set_element_count ( vec_base, element_count ); 159 } 160 161 // Construct the elements 162 __cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor ); 163 heap.release (); // We're good! 164 } 165 166 return vec_base; 167 } 168 169 170 // Same as __cxa_vec_new2 except that the deallocation function takes both 171 // the object address and its size. 172 void* __cxa_vec_new3( 173 size_t element_count, size_t element_size, size_t padding_size, 174 void (*constructor)(void*), void (*destructor)(void*), 175 void* (*alloc)(size_t), void (*dealloc)(void*, size_t) ) { 176 177 const size_t heap_size = element_count * element_size + padding_size; 178 char * const heap_block = static_cast<char *> ( alloc ( heap_size )); 179 char *vec_base = heap_block; 180 181 if ( NULL != vec_base ) { 182 st_heap_block3 heap ( dealloc, heap_block, heap_size ); 183 184 // put the padding before the array elements 185 if ( 0 != padding_size ) { 186 vec_base += padding_size; 187 __set_element_count ( vec_base, element_count ); 188 } 189 190 // Construct the elements 191 __cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor ); 192 heap.release (); // We're good! 193 } 194 195 return vec_base; 196 } 197 198 199 // Given the (data) addresses of a destination and a source array, an 200 // element count and an element size, call the given copy constructor to 201 // copy each element from the source array to the destination array. The 202 // copy constructor's arguments are the destination address and source 203 // address, respectively. If an exception occurs, call the given destructor 204 // (if non-NULL) on each copied element and rethrow. If the destructor 205 // throws an exception, call terminate(). The constructor and or destructor 206 // pointers may be NULL. If either is NULL, no action is taken when it 207 // would have been called. 208 209 void __cxa_vec_cctor( void* dest_array, void* src_array, 210 size_t element_count, size_t element_size, 211 void (*constructor) (void*, void*), void (*destructor)(void*) ) { 212 213 if ( NULL != constructor ) { 214 size_t idx = 0; 215 char *src_ptr = static_cast<char *>(src_array); 216 char *dest_ptr = static_cast<char *>(dest_array); 217 st_cxa_cleanup cleanup ( dest_array, idx, element_size, destructor ); 218 219 for ( idx = 0; idx < element_count; 220 ++idx, src_ptr += element_size, dest_ptr += element_size ) 221 constructor ( dest_ptr, src_ptr ); 222 cleanup.release (); // We're good! 223 } 224 } 225 226 227 // Given the (data) address of an array, not including any cookie padding, 228 // and the number and size of its elements, call the given constructor on 229 // each element. If the constructor throws an exception, call the given 230 // destructor for any already-constructed elements, and rethrow the 231 // exception. If the destructor throws an exception, call terminate(). The 232 // constructor and/or destructor pointers may be NULL. If either is NULL, 233 // no action is taken when it would have been called. 234 void __cxa_vec_ctor( 235 void* array_address, size_t element_count, size_t element_size, 236 void (*constructor)(void*), void (*destructor)(void*) ) { 237 238 if ( NULL != constructor ) { 239 size_t idx; 240 char *ptr = static_cast <char *> ( array_address ); 241 st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor ); 242 243 // Construct the elements 244 for ( idx = 0; idx < element_count; ++idx, ptr += element_size ) 245 constructor ( ptr ); 246 cleanup.release (); // We're good! 247 } 248 } 249 250 // Given the (data) address of an array, the number of elements, and the 251 // size of its elements, call the given destructor on each element. If the 252 // destructor throws an exception, rethrow after destroying the remaining 253 // elements if possible. If the destructor throws a second exception, call 254 // terminate(). The destructor pointer may be NULL, in which case this 255 // routine does nothing. 256 void __cxa_vec_dtor( 257 void* array_address, size_t element_count, size_t element_size, 258 void (*destructor)(void*) ) { 259 260 if ( NULL != destructor ) { 261 char *ptr = static_cast <char *> (array_address); 262 size_t idx = element_count; 263 st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor ); 264 { 265 st_terminate exception_guard (__cxa_uncaught_exception ()); 266 ptr += element_count * element_size; // one past the last element 267 268 while ( idx-- > 0 ) { 269 ptr -= element_size; 270 destructor ( ptr ); 271 } 272 exception_guard.release (); // We're good ! 273 } 274 cleanup.release (); // We're still good! 275 } 276 } 277 278 // Given the (data) address of an array, the number of elements, and the 279 // size of its elements, call the given destructor on each element. If the 280 // destructor throws an exception, call terminate(). The destructor pointer 281 // may be NULL, in which case this routine does nothing. 282 void __cxa_vec_cleanup( void* array_address, size_t element_count, 283 size_t element_size, void (*destructor)(void*) ) { 284 285 if ( NULL != destructor ) { 286 char *ptr = static_cast <char *> (array_address); 287 size_t idx = element_count; 288 st_terminate exception_guard; 289 290 ptr += element_count * element_size; // one past the last element 291 while ( idx-- > 0 ) { 292 ptr -= element_size; 293 destructor ( ptr ); 294 } 295 exception_guard.release (); // We're done! 296 } 297 } 298 299 300 // If the array_address is NULL, return immediately. Otherwise, given the 301 // (data) address of an array, the non-negative size of prefix padding for 302 // the cookie, and the size of its elements, call the given destructor on 303 // each element, using the cookie to determine the number of elements, and 304 // then delete the space by calling ::operator delete[](void *). If the 305 // destructor throws an exception, rethrow after (a) destroying the 306 // remaining elements, and (b) deallocating the storage. If the destructor 307 // throws a second exception, call terminate(). If padding_size is 0, the 308 // destructor pointer must be NULL. If the destructor pointer is NULL, no 309 // destructor call is to be made. 310 // 311 // The intent of this function is to permit an implementation to call this 312 // function when confronted with an expression of the form delete[] p in 313 // the source code, provided that the default deallocation function can be 314 // used. Therefore, the semantics of this function are consistent with 315 // those required by the standard. The requirement that the deallocation 316 // function be called even if the destructor throws an exception derives 317 // from the resolution to DR 353 to the C++ standard, which was adopted in 318 // April, 2003. 319 void __cxa_vec_delete( void* array_address, 320 size_t element_size, size_t padding_size, void (*destructor)(void*) ) { 321 322 __cxa_vec_delete2 ( array_address, element_size, padding_size, 323 destructor, &::operator delete [] ); 324 } 325 326 327 // Same as __cxa_vec_delete, except that the given function is used for 328 // deallocation instead of the default delete function. If dealloc throws 329 // an exception, the result is undefined. The dealloc pointer may not be 330 // NULL. 331 void __cxa_vec_delete2( void* array_address, 332 size_t element_size, size_t padding_size, 333 void (*destructor)(void*), void (*dealloc)(void*) ) { 334 335 if ( NULL != array_address ) { 336 char *vec_base = static_cast <char *> (array_address); 337 char *heap_block = vec_base - padding_size; 338 st_heap_block2 heap ( dealloc, heap_block ); 339 340 if ( 0 != padding_size && NULL != destructor ) // call the destructors 341 __cxa_vec_dtor ( array_address, __get_element_count ( vec_base ), 342 element_size, destructor ); 343 } 344 } 345 346 347 // Same as __cxa_vec_delete, except that the given function is used for 348 // deallocation instead of the default delete function. The deallocation 349 // function takes both the object address and its size. If dealloc throws 350 // an exception, the result is undefined. The dealloc pointer may not be 351 // NULL. 352 void __cxa_vec_delete3( void* array_address, 353 size_t element_size, size_t padding_size, 354 void (*destructor)(void*), void (*dealloc) (void*, size_t)) { 355 356 if ( NULL != array_address ) { 357 char *vec_base = static_cast <char *> (array_address); 358 char *heap_block = vec_base - padding_size; 359 const size_t element_count = padding_size ? __get_element_count ( vec_base ) : 0; 360 const size_t heap_block_size = element_size * element_count + padding_size; 361 st_heap_block3 heap ( dealloc, heap_block, heap_block_size ); 362 363 if ( 0 != padding_size && NULL != destructor ) // call the destructors 364 __cxa_vec_dtor ( array_address, element_count, element_size, destructor ); 365 } 366 } 367 368 369 } // extern "C" 370 371 } // abi 372