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