2008-08-26 19:12:23 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-08-26 19:12:23 +00:00
|
|
|
//
|
|
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
2009-02-02 13:22:19 +00:00
|
|
|
// Copyright (C) 2008-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2009-01-09 00:55:53 +00:00
|
|
|
// Copyright (C) 2009 Kenneth Riddile <kfriddile@yahoo.com>
|
2010-02-27 17:25:07 +01:00
|
|
|
// Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
|
2008-08-26 19:12:23 +00:00
|
|
|
//
|
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_MEMORY_H
|
|
|
|
|
#define EIGEN_MEMORY_H
|
|
|
|
|
|
2009-06-29 00:08:34 +02:00
|
|
|
// FreeBSD 6 seems to have 16-byte aligned malloc
|
|
|
|
|
// See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
|
|
|
|
|
// FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
|
|
|
|
|
// See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
|
|
|
|
|
#if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
|
|
|
|
|
#define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
|
|
|
|
|
#else
|
|
|
|
|
#define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(__APPLE__) || defined(_WIN64) || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
|
2009-01-09 14:56:44 +00:00
|
|
|
#define EIGEN_MALLOC_ALREADY_ALIGNED 1
|
|
|
|
|
#else
|
|
|
|
|
#define EIGEN_MALLOC_ALREADY_ALIGNED 0
|
2008-08-26 19:12:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
2009-04-24 13:26:36 +00:00
|
|
|
#if ((defined _GNU_SOURCE) || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))) && (defined _POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0)
|
2009-01-09 14:56:44 +00:00
|
|
|
#define EIGEN_HAS_POSIX_MEMALIGN 1
|
|
|
|
|
#else
|
|
|
|
|
#define EIGEN_HAS_POSIX_MEMALIGN 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef EIGEN_VECTORIZE_SSE
|
|
|
|
|
#define EIGEN_HAS_MM_MALLOC 1
|
|
|
|
|
#else
|
|
|
|
|
#define EIGEN_HAS_MM_MALLOC 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/** \internal like malloc, but the returned pointer is guaranteed to be 16-byte aligned.
|
|
|
|
|
* Fast, but wastes 16 additional bytes of memory.
|
|
|
|
|
* Does not throw any exception.
|
|
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
inline void* ei_handmade_aligned_malloc(size_t size)
|
2009-01-09 14:56:44 +00:00
|
|
|
{
|
2010-02-10 13:24:47 +01:00
|
|
|
void *original = std::malloc(size+16);
|
2010-02-12 08:58:29 -05:00
|
|
|
void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
|
2009-01-09 14:56:44 +00:00
|
|
|
*(reinterpret_cast<void**>(aligned) - 1) = original;
|
|
|
|
|
return aligned;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \internal frees memory allocated with ei_handmade_aligned_malloc */
|
|
|
|
|
inline void ei_handmade_aligned_free(void *ptr)
|
|
|
|
|
{
|
2009-01-12 12:54:32 +00:00
|
|
|
if(ptr)
|
2010-02-10 13:24:47 +01:00
|
|
|
std::free(*(reinterpret_cast<void**>(ptr) - 1));
|
2009-01-09 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-27 17:25:07 +01:00
|
|
|
inline void* ei_handmade_aligned_realloc(void* ptr, size_t size)
|
|
|
|
|
{
|
|
|
|
|
// 0. Handle corner cases according to the standard
|
|
|
|
|
if (ptr!=0 && size==0)
|
|
|
|
|
{
|
|
|
|
|
ei_handmade_aligned_free(ptr);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ptr==0) return ei_handmade_aligned_malloc(size);
|
|
|
|
|
|
|
|
|
|
// 1. compute the original base address
|
|
|
|
|
// 2. compute the new reallocated address
|
|
|
|
|
// 3. compute the aligned address and store the original one
|
|
|
|
|
void *base = *(reinterpret_cast<void**>(ptr) - 1);
|
|
|
|
|
void *original = std::realloc(base, size+16);
|
|
|
|
|
void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
|
|
|
|
|
*(reinterpret_cast<void**>(aligned) - 1) = original;
|
|
|
|
|
return aligned;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if EIGEN_HAS_MM_MALLOC
|
2010-02-27 17:51:48 -05:00
|
|
|
inline void* ei_mm_realloc(void *ptr, size_t size, size_t old_size)
|
2010-02-27 17:25:07 +01:00
|
|
|
{
|
2010-02-27 18:57:07 -05:00
|
|
|
// 0. Check if size==0 and act according to the standard, which says that
|
|
|
|
|
// for size==0, the object pointer (i.e. ptr) should be freed.
|
|
|
|
|
if (size==0)
|
2010-02-27 17:25:07 +01:00
|
|
|
{
|
|
|
|
|
_mm_free(ptr);
|
2010-02-27 18:57:07 -05:00
|
|
|
return 0;
|
2010-02-27 17:25:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. Allocate new memory
|
|
|
|
|
void* newptr = _mm_malloc(size,16);
|
|
|
|
|
|
|
|
|
|
// 2. Verify the allocation success
|
2010-02-27 18:57:07 -05:00
|
|
|
if (newptr == 0)
|
2010-02-27 17:25:07 +01:00
|
|
|
{
|
|
|
|
|
/*errno = ENOMEM;*/ // according to the standard we should set errno = ENOMEM
|
2010-02-27 18:57:07 -05:00
|
|
|
return 0;
|
2010-02-27 17:25:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Copy the overlapping data and free the old data
|
2010-02-27 18:57:07 -05:00
|
|
|
if (ptr != 0)
|
2010-02-27 17:25:07 +01:00
|
|
|
{
|
|
|
|
|
std::memcpy(newptr, ptr, std::min(size,old_size));
|
|
|
|
|
_mm_free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newptr;
|
|
|
|
|
}
|
2010-02-27 18:57:07 -05:00
|
|
|
#endif // EIGEN_HAS_MM_MALLOC
|
|
|
|
|
|
|
|
|
|
#if EIGEN_HAS_POSIX_MEMALIGN
|
|
|
|
|
inline void* ei_posix_memalign_realloc(void *ptr, size_t size, size_t old_size)
|
|
|
|
|
{
|
|
|
|
|
// 0. Check if size==0 and act according to the standard, which says that
|
|
|
|
|
// for size==0, the object pointer (i.e. ptr) should be freed.
|
|
|
|
|
if (size==0)
|
|
|
|
|
{
|
|
|
|
|
free(ptr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. Allocate new memory and verify the allocation success
|
|
|
|
|
void *newptr;
|
|
|
|
|
if(posix_memalign(&newptr, 16, size))
|
|
|
|
|
{
|
|
|
|
|
/*errno = ENOMEM;*/ // according to the standard we should set errno = ENOMEM
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Copy the overlapping data and free the old data
|
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
std::memcpy(newptr, ptr, std::min(size,old_size));
|
|
|
|
|
free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newptr;
|
|
|
|
|
}
|
|
|
|
|
#endif // EIGEN_HAS_POSIX_MEMALIGN
|
2010-02-27 17:25:07 +01:00
|
|
|
|
2009-01-08 15:20:21 +00:00
|
|
|
/** \internal allocates \a size bytes. The returned pointer is guaranteed to have 16 bytes alignment.
|
2009-04-06 13:33:42 +00:00
|
|
|
* On allocation error, the returned pointer is null, and if exceptions are enabled then a std::bad_alloc is thrown.
|
2008-12-16 15:17:29 +00:00
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
inline void* ei_aligned_malloc(size_t size)
|
2008-08-26 19:12:23 +00:00
|
|
|
{
|
2009-01-08 15:20:21 +00:00
|
|
|
#ifdef EIGEN_NO_MALLOC
|
|
|
|
|
ei_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
void *result;
|
2009-05-15 16:05:45 +00:00
|
|
|
#if !EIGEN_ALIGN
|
|
|
|
|
result = malloc(size);
|
|
|
|
|
#elif EIGEN_MALLOC_ALREADY_ALIGNED
|
|
|
|
|
result = malloc(size);
|
|
|
|
|
#elif EIGEN_HAS_POSIX_MEMALIGN
|
|
|
|
|
if(posix_memalign(&result, 16, size)) result = 0;
|
|
|
|
|
#elif EIGEN_HAS_MM_MALLOC
|
|
|
|
|
result = _mm_malloc(size, 16);
|
|
|
|
|
#elif (defined _MSC_VER)
|
|
|
|
|
result = _aligned_malloc(size, 16);
|
2009-01-08 15:20:21 +00:00
|
|
|
#else
|
2009-05-15 16:05:45 +00:00
|
|
|
result = ei_handmade_aligned_malloc(size);
|
2009-01-08 15:20:21 +00:00
|
|
|
#endif
|
2009-11-07 09:07:23 +01:00
|
|
|
|
2009-01-08 15:20:21 +00:00
|
|
|
#ifdef EIGEN_EXCEPTIONS
|
2009-02-21 16:35:57 +00:00
|
|
|
if(result == 0)
|
2009-01-08 15:20:21 +00:00
|
|
|
throw std::bad_alloc();
|
|
|
|
|
#endif
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** allocates \a size bytes. If Align is true, then the returned ptr is 16-byte-aligned.
|
2009-04-06 13:33:42 +00:00
|
|
|
* On allocation error, the returned pointer is null, and if exceptions are enabled then a std::bad_alloc is thrown.
|
2009-01-08 15:20:21 +00:00
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
template<bool Align> inline void* ei_conditional_aligned_malloc(size_t size)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
|
|
|
|
return ei_aligned_malloc(size);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-12 08:58:29 -05:00
|
|
|
template<> inline void* ei_conditional_aligned_malloc<false>(size_t size)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
2009-01-10 14:24:55 +00:00
|
|
|
#ifdef EIGEN_NO_MALLOC
|
|
|
|
|
ei_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-02-10 13:24:47 +01:00
|
|
|
void *result = std::malloc(size);
|
2009-01-08 15:20:21 +00:00
|
|
|
#ifdef EIGEN_EXCEPTIONS
|
2009-01-21 17:10:23 +00:00
|
|
|
if(!result) throw std::bad_alloc();
|
2009-01-08 15:20:21 +00:00
|
|
|
#endif
|
2009-01-21 17:10:23 +00:00
|
|
|
return result;
|
2009-01-08 15:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
2009-06-04 17:25:15 +02:00
|
|
|
/** \internal construct the elements of an array.
|
2009-06-04 09:11:35 +02:00
|
|
|
* The \a size parameter tells on how many objects to call the constructor of T.
|
|
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
template<typename T> inline T* ei_construct_elements_of_array(T *ptr, size_t size)
|
2009-06-04 09:11:35 +02:00
|
|
|
{
|
2010-02-12 08:58:29 -05:00
|
|
|
for (size_t i=0; i < size; ++i) ::new (ptr + i) T;
|
2009-06-04 09:11:35 +02:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-08 15:20:21 +00:00
|
|
|
/** allocates \a size objects of type T. The returned pointer is guaranteed to have 16 bytes alignment.
|
|
|
|
|
* On allocation error, the returned pointer is undefined, but if exceptions are enabled then a std::bad_alloc is thrown.
|
|
|
|
|
* The default constructor of T is called.
|
|
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
template<typename T> inline T* ei_aligned_new(size_t size)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
2009-06-04 09:11:35 +02:00
|
|
|
T *result = reinterpret_cast<T*>(ei_aligned_malloc(sizeof(T)*size));
|
2009-06-04 17:25:15 +02:00
|
|
|
return ei_construct_elements_of_array(result, size);
|
2009-01-08 15:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-12 08:58:29 -05:00
|
|
|
template<typename T, bool Align> inline T* ei_conditional_aligned_new(size_t size)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
2009-06-04 09:11:35 +02:00
|
|
|
T *result = reinterpret_cast<T*>(ei_conditional_aligned_malloc<Align>(sizeof(T)*size));
|
2009-06-04 17:25:15 +02:00
|
|
|
return ei_construct_elements_of_array(result, size);
|
2008-08-26 19:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-30 14:11:35 +00:00
|
|
|
/** \internal free memory allocated with ei_aligned_malloc
|
|
|
|
|
*/
|
2009-01-08 15:20:21 +00:00
|
|
|
inline void ei_aligned_free(void *ptr)
|
|
|
|
|
{
|
2009-05-03 13:50:56 +00:00
|
|
|
#if !EIGEN_ALIGN
|
2009-02-04 16:53:03 +00:00
|
|
|
free(ptr);
|
|
|
|
|
#elif EIGEN_MALLOC_ALREADY_ALIGNED
|
2009-01-08 15:20:21 +00:00
|
|
|
free(ptr);
|
2009-01-09 20:57:06 +00:00
|
|
|
#elif EIGEN_HAS_POSIX_MEMALIGN
|
2009-01-08 15:20:21 +00:00
|
|
|
free(ptr);
|
2009-01-09 14:56:44 +00:00
|
|
|
#elif EIGEN_HAS_MM_MALLOC
|
2009-01-08 15:20:21 +00:00
|
|
|
_mm_free(ptr);
|
2009-01-09 20:57:06 +00:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
_aligned_free(ptr);
|
2009-01-09 14:56:44 +00:00
|
|
|
#else
|
|
|
|
|
ei_handmade_aligned_free(ptr);
|
2009-01-08 15:20:21 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \internal free memory allocated with ei_conditional_aligned_malloc
|
|
|
|
|
*/
|
|
|
|
|
template<bool Align> inline void ei_conditional_aligned_free(void *ptr)
|
|
|
|
|
{
|
|
|
|
|
ei_aligned_free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-08 20:20:11 +00:00
|
|
|
template<> inline void ei_conditional_aligned_free<false>(void *ptr)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
2010-02-10 13:24:47 +01:00
|
|
|
std::free(ptr);
|
2009-01-08 15:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-27 17:25:07 +01:00
|
|
|
inline void* ei_aligned_realloc(void *ptr, size_t new_size, size_t old_size)
|
|
|
|
|
{
|
|
|
|
|
(void)old_size; // Suppress 'unused variable' warning. Seen in boost tee.
|
|
|
|
|
|
|
|
|
|
void *result;
|
|
|
|
|
#if !EIGEN_ALIGN
|
|
|
|
|
result = realloc(ptr,new_size);
|
|
|
|
|
#elif EIGEN_MALLOC_ALREADY_ALIGNED
|
2010-02-27 18:57:07 -05:00
|
|
|
result = realloc(ptr,new_size);
|
2010-02-27 17:25:07 +01:00
|
|
|
#elif EIGEN_HAS_POSIX_MEMALIGN
|
2010-02-27 18:57:07 -05:00
|
|
|
result = ei_posix_memalign_realloc(ptr,new_size,old_size);
|
2010-02-27 17:25:07 +01:00
|
|
|
#elif EIGEN_HAS_MM_MALLOC
|
2010-02-27 18:57:07 -05:00
|
|
|
#if defined(_MSC_VER) && defined(_mm_free)
|
|
|
|
|
result = _aligned_realloc(ptr,new_size,16);
|
|
|
|
|
#else
|
|
|
|
|
result = ei_mm_realloc(ptr,new_size,old_size);
|
|
|
|
|
#endif
|
2010-02-27 17:25:07 +01:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
result = _aligned_realloc(ptr,new_size,16);
|
|
|
|
|
#else
|
|
|
|
|
result = ei_handmade_aligned_realloc(ptr,new_size);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef EIGEN_EXCEPTIONS
|
|
|
|
|
if (result==0 && new_size!=0)
|
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
#endif
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<bool Align> inline void* ei_conditional_aligned_realloc(void* ptr, size_t new_size, size_t old_size)
|
|
|
|
|
{
|
|
|
|
|
return ei_aligned_realloc(ptr, new_size, old_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<> inline void* ei_conditional_aligned_realloc<false>(void* ptr, size_t new_size, size_t)
|
|
|
|
|
{
|
|
|
|
|
return std::realloc(ptr, new_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T, bool Align> inline T* ei_conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
|
|
|
|
|
{
|
|
|
|
|
T *result = reinterpret_cast<T*>(ei_conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
|
|
|
|
|
if (new_size > old_size)
|
|
|
|
|
ei_construct_elements_of_array(result+old_size, new_size-old_size);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-04 17:25:15 +02:00
|
|
|
/** \internal destruct the elements of an array.
|
2009-01-08 15:20:21 +00:00
|
|
|
* The \a size parameters tells on how many objects to call the destructor of T.
|
|
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
template<typename T> inline void ei_destruct_elements_of_array(T *ptr, size_t size)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
|
|
|
|
// always destruct an array starting from the end.
|
|
|
|
|
while(size) ptr[--size].~T();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \internal delete objects constructed with ei_aligned_new
|
|
|
|
|
* The \a size parameters tells on how many objects to call the destructor of T.
|
|
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
template<typename T> inline void ei_aligned_delete(T *ptr, size_t size)
|
2008-08-26 19:12:23 +00:00
|
|
|
{
|
2009-06-04 17:25:15 +02:00
|
|
|
ei_destruct_elements_of_array<T>(ptr, size);
|
2009-01-08 15:20:21 +00:00
|
|
|
ei_aligned_free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \internal delete objects constructed with ei_conditional_aligned_new
|
|
|
|
|
* The \a size parameters tells on how many objects to call the destructor of T.
|
|
|
|
|
*/
|
2010-02-12 08:58:29 -05:00
|
|
|
template<typename T, bool Align> inline void ei_conditional_aligned_delete(T *ptr, size_t size)
|
2009-01-08 15:20:21 +00:00
|
|
|
{
|
2009-06-04 17:25:15 +02:00
|
|
|
ei_destruct_elements_of_array<T>(ptr, size);
|
2009-01-08 15:20:21 +00:00
|
|
|
ei_conditional_aligned_free<Align>(ptr);
|
2008-08-26 19:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-02 12:38:16 -05:00
|
|
|
/** \internal \returns the index of the first element of the array that is well aligned for vectorization.
|
2009-12-16 08:53:14 -05:00
|
|
|
*
|
2010-01-02 12:38:16 -05:00
|
|
|
* \param array the address of the start of the array
|
|
|
|
|
* \param size the size of the array
|
|
|
|
|
*
|
|
|
|
|
* \note If no element of the array is well aligned, the size of the array is returned. Typically,
|
|
|
|
|
* for example with SSE, "well aligned" means 16-byte-aligned. If vectorization is disabled or if the
|
|
|
|
|
* packet size for the given scalar type is 1, then everything is considered well-aligned.
|
|
|
|
|
*
|
|
|
|
|
* \note If the scalar type is vectorizable, we rely on the following assumptions: sizeof(Scalar) is a
|
|
|
|
|
* power of 2, the packet size in bytes is also a power of 2, and is a multiple of sizeof(Scalar). On the
|
|
|
|
|
* other hand, we do not assume that the array address is a multiple of sizeof(Scalar), as that fails for
|
|
|
|
|
* example with Scalar=double on certain 32-bit platforms, see bug #79.
|
|
|
|
|
*
|
|
|
|
|
* There is also the variant ei_first_aligned(const MatrixBase&, Integer) defined in Coeffs.h.
|
2009-12-16 08:53:14 -05:00
|
|
|
*/
|
2009-12-12 11:39:07 +01:00
|
|
|
template<typename Scalar, typename Integer>
|
2010-01-02 12:38:16 -05:00
|
|
|
inline static Integer ei_first_aligned(const Scalar* array, Integer size)
|
2008-08-26 19:12:23 +00:00
|
|
|
{
|
|
|
|
|
typedef typename ei_packet_traits<Scalar>::type Packet;
|
2010-01-02 12:38:16 -05:00
|
|
|
enum { PacketSize = ei_packet_traits<Scalar>::size,
|
|
|
|
|
PacketAlignedMask = PacketSize-1
|
|
|
|
|
};
|
2010-02-25 21:01:52 -05:00
|
|
|
|
2010-01-02 12:38:16 -05:00
|
|
|
if(PacketSize==1)
|
|
|
|
|
{
|
|
|
|
|
// Either there is no vectorization, or a packet consists of exactly 1 scalar so that all elements
|
2010-02-27 17:25:07 +01:00
|
|
|
// of the array have the same alignment.
|
2010-01-02 12:38:16 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if(size_t(array) & (sizeof(Scalar)-1))
|
|
|
|
|
{
|
|
|
|
|
// There is vectorization for this scalar type, but the array is not aligned to the size of a single scalar.
|
|
|
|
|
// Consequently, no element of the array is well aligned.
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return std::min<Integer>( (PacketSize - (Integer((size_t(array)/sizeof(Scalar))) & PacketAlignedMask))
|
|
|
|
|
& PacketAlignedMask, size);
|
|
|
|
|
}
|
2008-08-26 19:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \internal
|
2009-01-08 15:20:21 +00:00
|
|
|
* ei_aligned_stack_alloc(SIZE) allocates an aligned buffer of SIZE bytes
|
2010-01-02 12:38:16 -05:00
|
|
|
* on the stack if SIZE is smaller than EIGEN_STACK_ALLOCATION_LIMIT, and
|
|
|
|
|
* if stack allocation is supported by the platform (currently, this is linux only).
|
2008-12-24 13:59:24 +00:00
|
|
|
* Otherwise the memory is allocated on the heap.
|
2009-01-08 15:20:21 +00:00
|
|
|
* Data allocated with ei_aligned_stack_alloc \b must be freed by calling ei_aligned_stack_free(PTR,SIZE).
|
2008-08-26 19:12:23 +00:00
|
|
|
* \code
|
2009-01-03 22:33:08 +00:00
|
|
|
* float * data = ei_aligned_stack_alloc(float,array.size());
|
2008-08-26 19:12:23 +00:00
|
|
|
* // ...
|
2009-01-03 22:33:08 +00:00
|
|
|
* ei_aligned_stack_free(data,float,array.size());
|
2008-08-26 19:12:23 +00:00
|
|
|
* \endcode
|
|
|
|
|
*/
|
|
|
|
|
#ifdef __linux__
|
2009-01-08 15:20:21 +00:00
|
|
|
#define ei_aligned_stack_alloc(SIZE) (SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) \
|
|
|
|
|
? alloca(SIZE) \
|
|
|
|
|
: ei_aligned_malloc(SIZE)
|
|
|
|
|
#define ei_aligned_stack_free(PTR,SIZE) if(SIZE>EIGEN_STACK_ALLOCATION_LIMIT) ei_aligned_free(PTR)
|
2008-08-26 19:12:23 +00:00
|
|
|
#else
|
2009-01-08 15:20:21 +00:00
|
|
|
#define ei_aligned_stack_alloc(SIZE) ei_aligned_malloc(SIZE)
|
|
|
|
|
#define ei_aligned_stack_free(PTR,SIZE) ei_aligned_free(PTR)
|
2008-08-26 19:12:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
2009-06-04 17:25:15 +02:00
|
|
|
#define ei_aligned_stack_new(TYPE,SIZE) ei_construct_elements_of_array(reinterpret_cast<TYPE*>(ei_aligned_stack_alloc(sizeof(TYPE)*SIZE)), SIZE)
|
|
|
|
|
#define ei_aligned_stack_delete(TYPE,PTR,SIZE) do {ei_destruct_elements_of_array<TYPE>(PTR, SIZE); \
|
2009-01-12 14:20:21 +00:00
|
|
|
ei_aligned_stack_free(PTR,sizeof(TYPE)*SIZE);} while(0)
|
2009-01-06 03:16:50 +00:00
|
|
|
|
2009-02-04 16:53:03 +00:00
|
|
|
|
2009-05-03 13:50:56 +00:00
|
|
|
#if EIGEN_ALIGN
|
2009-05-15 15:53:26 +00:00
|
|
|
#ifdef EIGEN_EXCEPTIONS
|
|
|
|
|
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
|
2010-02-12 08:58:29 -05:00
|
|
|
void* operator new(size_t size, const std::nothrow_t&) throw() { \
|
2009-05-15 15:53:26 +00:00
|
|
|
try { return Eigen::ei_conditional_aligned_malloc<NeedsToAlign>(size); } \
|
|
|
|
|
catch (...) { return 0; } \
|
|
|
|
|
return 0; \
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
|
2010-02-12 08:58:29 -05:00
|
|
|
void* operator new(size_t size, const std::nothrow_t&) throw() { \
|
2009-05-15 15:53:26 +00:00
|
|
|
return Eigen::ei_conditional_aligned_malloc<NeedsToAlign>(size); \
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-02-04 16:53:03 +00:00
|
|
|
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
|
2010-02-12 08:58:29 -05:00
|
|
|
void *operator new(size_t size) { \
|
2009-02-04 16:53:03 +00:00
|
|
|
return Eigen::ei_conditional_aligned_malloc<NeedsToAlign>(size); \
|
|
|
|
|
} \
|
2010-02-12 08:58:29 -05:00
|
|
|
void *operator new[](size_t size) { \
|
2009-02-04 16:53:03 +00:00
|
|
|
return Eigen::ei_conditional_aligned_malloc<NeedsToAlign>(size); \
|
|
|
|
|
} \
|
2009-05-07 20:33:48 +00:00
|
|
|
void operator delete(void * ptr) throw() { Eigen::ei_conditional_aligned_free<NeedsToAlign>(ptr); } \
|
|
|
|
|
void operator delete[](void * ptr) throw() { Eigen::ei_conditional_aligned_free<NeedsToAlign>(ptr); } \
|
|
|
|
|
/* in-place new and delete. since (at least afaik) there is no actual */ \
|
|
|
|
|
/* memory allocated we can safely let the default implementation handle */ \
|
|
|
|
|
/* this particular case. */ \
|
2010-02-12 08:58:29 -05:00
|
|
|
static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \
|
2009-05-07 20:33:48 +00:00
|
|
|
void operator delete(void * memory, void *ptr) throw() { return ::operator delete(memory,ptr); } \
|
|
|
|
|
/* nothrow-new (returns zero instead of std::bad_alloc) */ \
|
2009-05-15 15:53:26 +00:00
|
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
|
2009-05-07 20:33:48 +00:00
|
|
|
void operator delete(void *ptr, const std::nothrow_t&) throw() { \
|
|
|
|
|
Eigen::ei_conditional_aligned_free<NeedsToAlign>(ptr); \
|
|
|
|
|
} \
|
2009-02-07 11:16:15 +00:00
|
|
|
typedef void ei_operator_new_marker_type;
|
2009-02-04 16:53:03 +00:00
|
|
|
#else
|
|
|
|
|
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
|
|
|
|
|
#endif
|
2009-01-08 15:20:21 +00:00
|
|
|
|
2009-01-08 15:37:13 +00:00
|
|
|
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
|
2009-01-12 16:06:04 +00:00
|
|
|
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
|
2009-01-08 15:37:13 +00:00
|
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%16==0))
|
2009-01-08 15:20:21 +00:00
|
|
|
|
2009-02-04 16:53:03 +00:00
|
|
|
|
2009-01-09 00:55:53 +00:00
|
|
|
/** \class aligned_allocator
|
|
|
|
|
*
|
|
|
|
|
* \brief stl compatible allocator to use with with 16 byte aligned types
|
|
|
|
|
*
|
|
|
|
|
* Example:
|
|
|
|
|
* \code
|
2009-01-10 02:50:09 +00:00
|
|
|
* // Matrix4f requires 16 bytes alignment:
|
|
|
|
|
* std::map< int, Matrix4f, std::less<int>, aligned_allocator<Matrix4f> > my_map_mat4;
|
2009-01-09 00:55:53 +00:00
|
|
|
* // Vector3f does not require 16 bytes alignment, no need to use Eigen's allocator:
|
2009-01-10 02:50:09 +00:00
|
|
|
* std::map< int, Vector3f > my_map_vec3;
|
2009-01-09 00:55:53 +00:00
|
|
|
* \endcode
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
template<class T>
|
|
|
|
|
class aligned_allocator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2010-02-12 08:58:29 -05:00
|
|
|
typedef size_t size_type;
|
2010-02-10 13:24:47 +01:00
|
|
|
typedef std::ptrdiff_t difference_type;
|
2009-01-09 00:55:53 +00:00
|
|
|
typedef T* pointer;
|
|
|
|
|
typedef const T* const_pointer;
|
|
|
|
|
typedef T& reference;
|
|
|
|
|
typedef const T& const_reference;
|
|
|
|
|
typedef T value_type;
|
|
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
|
struct rebind
|
|
|
|
|
{
|
|
|
|
|
typedef aligned_allocator<U> other;
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
pointer address( reference value ) const
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
return &value;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
const_pointer address( const_reference value ) const
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
return &value;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
aligned_allocator() throw()
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
aligned_allocator( const aligned_allocator& ) throw()
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class U>
|
2009-11-07 09:07:23 +01:00
|
|
|
aligned_allocator( const aligned_allocator<U>& ) throw()
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
~aligned_allocator() throw()
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
size_type max_size() const throw()
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
return std::numeric_limits<size_type>::max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pointer allocate( size_type num, const_pointer* hint = 0 )
|
|
|
|
|
{
|
|
|
|
|
static_cast<void>( hint ); // suppress unused variable warning
|
|
|
|
|
return static_cast<pointer>( ei_aligned_malloc( num * sizeof(T) ) );
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
void construct( pointer p, const T& value )
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
::new( p ) T( value );
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
void destroy( pointer p )
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
p->~T();
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-07 09:07:23 +01:00
|
|
|
void deallocate( pointer p, size_type /*num*/ )
|
2009-01-09 00:55:53 +00:00
|
|
|
{
|
|
|
|
|
ei_aligned_free( p );
|
|
|
|
|
}
|
2009-11-07 09:07:23 +01:00
|
|
|
|
2010-01-04 23:21:04 +01:00
|
|
|
bool operator!=(const aligned_allocator<T>& ) const
|
2009-04-09 21:22:02 +00:00
|
|
|
{ return false; }
|
2009-11-07 09:07:23 +01:00
|
|
|
|
2010-01-04 23:21:04 +01:00
|
|
|
bool operator==(const aligned_allocator<T>& ) const
|
2009-04-09 21:22:02 +00:00
|
|
|
{ return true; }
|
2009-01-09 00:55:53 +00:00
|
|
|
};
|
|
|
|
|
|
2008-08-26 19:12:23 +00:00
|
|
|
#endif // EIGEN_MEMORY_H
|