MCPcopy Create free account
hub / github.com/PX4/eigen / construct_elements_of_array

Function construct_elements_of_array

Eigen/src/Core/util/Memory.h:265–335  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

263 * The \a size parameter tells on how many objects to call the constructor of T.
264 */
265template<typename T> EIGEN_DEVICE_FUNC inline T* construct_elements_of_array(T *ptr, std::size_t size)
266{
267 std::size_t i;
268 EIGEN_TRY
269 {
270 for (i = 0; i < size; ++i) ::new (ptr + i) T;
271 return ptr;
272 }
273 EIGEN_CATCH(...)
274 {
275 destruct_elements_of_array(ptr, i);
276 EIGEN_THROW;
277 }
278 return NULL;
279}
280
281/*****************************************************************************
282*** Implementation of aligned new/delete-like functions ***
283*****************************************************************************/
284
285template<typename T>
286EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size)
287{
288 if(size > std::size_t(-1) / sizeof(T))
289 throw_std_bad_alloc();
290}
291
292/** \internal Allocates \a size objects of type T. The returned pointer is guaranteed to have 16 bytes alignment.
293 * On allocation error, the returned pointer is undefined, but a std::bad_alloc is thrown.
294 * The default constructor of T is called.
295 */
296template<typename T> EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size)
297{
298 check_size_for_overflow<T>(size);
299 T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
300 EIGEN_TRY
301 {
302 return construct_elements_of_array(result, size);
303 }
304 EIGEN_CATCH(...)
305 {
306 aligned_free(result);
307 EIGEN_THROW;
308 }
309 return result;
310}
311
312template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(std::size_t size)
313{
314 check_size_for_overflow<T>(size);
315 T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
316 EIGEN_TRY
317 {
318 return construct_elements_of_array(result, size);
319 }
320 EIGEN_CATCH(...)
321 {
322 conditional_aligned_free<Align>(result);

Calls 4

throw_std_bad_allocFunction · 0.85
aligned_mallocFunction · 0.85
aligned_freeFunction · 0.85

Tested by

no test coverage detected