19 #ifndef EIGEN_MEMORY_H 20 #define EIGEN_MEMORY_H 22 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED 33 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \ 34 && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) 35 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1 37 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0 44 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__) 45 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1 47 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0 50 #if defined(__APPLE__) \ 52 || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \ 53 || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 54 #define EIGEN_MALLOC_ALREADY_ALIGNED 1 56 #define EIGEN_MALLOC_ALREADY_ALIGNED 0 64 #if defined(__unix__) || defined(__unix) 66 #if ((defined __QNXNTO__) || (defined _GNU_SOURCE) || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))) && (defined _POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0) 67 #define EIGEN_HAS_POSIX_MEMALIGN 1 71 #ifndef EIGEN_HAS_POSIX_MEMALIGN 72 #define EIGEN_HAS_POSIX_MEMALIGN 0 75 #ifdef EIGEN_VECTORIZE_SSE 76 #define EIGEN_HAS_MM_MALLOC 1 78 #define EIGEN_HAS_MM_MALLOC 0 87 #ifdef EIGEN_EXCEPTIONS 88 throw std::bad_alloc();
90 std::size_t huge = -1;
106 void *original = std::malloc(size+16);
107 if (original == 0)
return 0;
108 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(15))) + 16);
109 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
116 if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
127 void *original = *(
reinterpret_cast<void**
>(ptr) - 1);
128 std::ptrdiff_t previous_offset =
static_cast<char *
>(ptr)-static_cast<char *>(original);
129 original = std::realloc(original,size+16);
130 if (original == 0)
return 0;
131 void *aligned =
reinterpret_cast<void*
>((
reinterpret_cast<std::size_t
>(original) & ~(std::size_t(15))) + 16);
132 void *previous_aligned =
static_cast<char *
>(original)+previous_offset;
133 if(aligned!=previous_aligned)
134 std::memmove(aligned, previous_aligned, size);
136 *(
reinterpret_cast<void**
>(aligned) - 1) = original;
166 #ifdef EIGEN_HAS_ERRNO 174 std::memcpy(newptr, ptr, (std::min)(size,old_size));
185 #ifdef EIGEN_NO_MALLOC 188 eigen_assert(
false &&
"heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
190 #elif defined EIGEN_RUNTIME_NO_MALLOC 191 inline bool is_malloc_allowed_impl(
bool update,
bool new_value =
false)
193 static bool value =
true;
198 inline bool is_malloc_allowed() {
return is_malloc_allowed_impl(
false); }
199 inline bool set_is_malloc_allowed(
bool new_value) {
return is_malloc_allowed_impl(
true, new_value); }
202 eigen_assert(is_malloc_allowed() &&
"heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
218 result = std::malloc(size);
219 #elif EIGEN_MALLOC_ALREADY_ALIGNED 220 result = std::malloc(size);
221 #elif EIGEN_HAS_POSIX_MEMALIGN 222 if(posix_memalign(&result, 16, size)) result = 0;
223 #elif EIGEN_HAS_MM_MALLOC 224 result = _mm_malloc(size, 16);
225 #elif defined(_MSC_VER) && (!defined(_WIN32_WCE)) 226 result = _aligned_malloc(size, 16);
242 #elif EIGEN_MALLOC_ALREADY_ALIGNED 244 #elif EIGEN_HAS_POSIX_MEMALIGN 246 #elif EIGEN_HAS_MM_MALLOC 248 #elif defined(_MSC_VER) && (!defined(_WIN32_WCE)) 266 result = std::realloc(ptr,new_size);
267 #elif EIGEN_MALLOC_ALREADY_ALIGNED 268 result = std::realloc(ptr,new_size);
269 #elif EIGEN_HAS_POSIX_MEMALIGN 271 #elif EIGEN_HAS_MM_MALLOC 275 #if defined(_MSC_VER) && defined(_mm_free) 276 result = _aligned_realloc(ptr,new_size,16);
280 #elif defined(_MSC_VER) 281 result = _aligned_realloc(ptr,new_size,16);
286 if (!result && new_size)
308 void *result = std::malloc(size);
332 return std::realloc(ptr, new_size);
344 for (
size_t i=0; i < size; ++i) ::
new (ptr + i)
T;
355 while(size) ptr[--size].~T();
365 if(size >
size_t(-1) /
sizeof(
T))
375 check_size_for_overflow<T>(size);
382 check_size_for_overflow<T>(size);
383 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(
T)*size));
392 destruct_elements_of_array<T>(ptr, size);
401 destruct_elements_of_array<T>(ptr, size);
402 conditional_aligned_free<Align>(ptr);
407 check_size_for_overflow<T>(new_size);
408 check_size_for_overflow<T>(old_size);
409 if(new_size < old_size)
411 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(
T)*new_size,
sizeof(
T)*old_size));
412 if(new_size > old_size)
420 check_size_for_overflow<T>(size);
421 T *result =
reinterpret_cast<T*
>(conditional_aligned_malloc<Align>(
sizeof(
T)*size));
429 check_size_for_overflow<T>(new_size);
430 check_size_for_overflow<T>(old_size);
433 T *result =
reinterpret_cast<T*
>(conditional_aligned_realloc<Align>(
reinterpret_cast<void*
>(pts),
sizeof(
T)*new_size,
sizeof(
T)*old_size));
442 destruct_elements_of_array<T>(ptr, size);
443 conditional_aligned_free<Align>(ptr);
464 template<
typename Scalar,
typename Index>
468 PacketAlignedMask = PacketSize-1
477 else if(
size_t(array) & (
sizeof(Scalar)-1))
485 return std::min<Index>( (PacketSize - (Index((
size_t(array)/
sizeof(Scalar))) & PacketAlignedMask))
486 & PacketAlignedMask, size);
492 template<
typename Index>
495 return ((size+base-1)/base)*base;
502 template<
typename T>
void smart_copy(
const T* start,
const T* end,
T* target)
508 static inline void run(
const T* start,
const T* end,
T* target)
509 { memcpy(target, start, std::ptrdiff_t(end)-std::ptrdiff_t(start)); }
513 static inline void run(
const T* start,
const T* end,
T* target)
514 { std::copy(start, end, target); }
525 #if (defined __linux__) 526 #define EIGEN_ALLOCA alloca 527 #elif defined(_MSC_VER) 528 #define EIGEN_ALLOCA _alloca 544 : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
552 Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
582 #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<size_t>(EIGEN_ALLOCA(SIZE+16)) & ~(size_t(15))) + 16) 584 #define EIGEN_ALIGNED_ALLOCA EIGEN_ALLOCA 587 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \ 588 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 589 TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \ 590 : reinterpret_cast<TYPE*>( \ 591 (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \ 592 : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \ 593 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,sizeof(TYPE)*SIZE>EIGEN_STACK_ALLOCATION_LIMIT) 597 #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \ 598 Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \ 599 TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \ 600 Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true) 610 #ifdef EIGEN_EXCEPTIONS 611 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 612 void* operator new(size_t size, const std::nothrow_t&) throw() { \ 613 try { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \ 614 catch (...) { return 0; } \ 618 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 619 void* operator new(size_t size, const std::nothrow_t&) throw() { \ 620 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 624 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \ 625 void *operator new(size_t size) { \ 626 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 628 void *operator new[](size_t size) { \ 629 return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \ 631 void operator delete(void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 632 void operator delete[](void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \ 636 static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \ 637 void operator delete(void * memory, void *ptr) throw() { return ::operator delete(memory,ptr); } \ 639 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \ 640 void operator delete(void *ptr, const std::nothrow_t&) throw() { \ 641 Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \ 643 typedef void eigen_aligned_operator_new_marker_type; 645 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) 648 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true) 649 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \ 650 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%16==0))) 693 const_pointer
address( const_reference value )
const 717 return (std::numeric_limits<size_type>::max)();
720 pointer
allocate( size_type num,
const void* hint = 0 )
723 internal::check_size_for_overflow<T>(num);
729 ::new( p )
T( value );
733 #if (__cplusplus >= 201103L) 734 template<
typename... Args>
735 void construct(pointer p, Args&&... args)
737 ::new(p)
T(std::forward<Args>(args)...);
760 #if !defined(EIGEN_NO_CPUID) 761 # if defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) ) 762 # if defined(__PIC__) && defined(__i386__) 764 # define EIGEN_CPUID(abcd,func,id) \ 765 __asm__ __volatile__ ("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id)); 766 # elif defined(__PIC__) && defined(__x86_64__) 769 # define EIGEN_CPUID(abcd,func,id) \ 770 __asm__ __volatile__ ("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1": "=a" (abcd[0]), "=&r" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id)); 773 # define EIGEN_CPUID(abcd,func,id) \ 774 __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) ); 776 # elif defined(_MSC_VER) 777 # if (_MSC_VER > 1500) && ( defined(_M_IX86) || defined(_M_X64) ) 778 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id) 787 inline bool cpuid_is_vendor(
int abcd[4],
const char* vendor)
789 return abcd[1]==(
reinterpret_cast<const int*
>(vendor))[0] && abcd[3]==(
reinterpret_cast<const int*
>(vendor))[1] && abcd[2]==(
reinterpret_cast<const int*
>(vendor))[2];
792 inline void queryCacheSizes_intel_direct(
int& l1,
int& l2,
int& l3)
799 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
800 EIGEN_CPUID(abcd,0x4,cache_id);
801 cache_type = (abcd[0] & 0x0F) >> 0;
802 if(cache_type==1||cache_type==3)
804 int cache_level = (abcd[0] & 0xE0) >> 5;
805 int ways = (abcd[1] & 0xFFC00000) >> 22;
806 int partitions = (abcd[1] & 0x003FF000) >> 12;
807 int line_size = (abcd[1] & 0x00000FFF) >> 0;
808 int sets = (abcd[2]);
810 int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
814 case 1: l1 = cache_size;
break;
815 case 2: l2 = cache_size;
break;
816 case 3: l3 = cache_size;
break;
821 }
while(cache_type>0 && cache_id<16);
824 inline void queryCacheSizes_intel_codes(
int& l1,
int& l2,
int& l3)
827 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
829 EIGEN_CPUID(abcd,0x00000002,0);
830 unsigned char * bytes =
reinterpret_cast<unsigned char *
>(abcd)+2;
831 bool check_for_p2_core2 =
false;
832 for(
int i=0; i<14; ++i)
836 case 0x0A: l1 = 8;
break;
837 case 0x0C: l1 = 16;
break;
838 case 0x0E: l1 = 24;
break;
839 case 0x10: l1 = 16;
break;
840 case 0x15: l1 = 16;
break;
841 case 0x2C: l1 = 32;
break;
842 case 0x30: l1 = 32;
break;
843 case 0x60: l1 = 16;
break;
844 case 0x66: l1 = 8;
break;
845 case 0x67: l1 = 16;
break;
846 case 0x68: l1 = 32;
break;
847 case 0x1A: l2 = 96;
break;
848 case 0x22: l3 = 512;
break;
849 case 0x23: l3 = 1024;
break;
850 case 0x25: l3 = 2048;
break;
851 case 0x29: l3 = 4096;
break;
852 case 0x39: l2 = 128;
break;
853 case 0x3A: l2 = 192;
break;
854 case 0x3B: l2 = 128;
break;
855 case 0x3C: l2 = 256;
break;
856 case 0x3D: l2 = 384;
break;
857 case 0x3E: l2 = 512;
break;
858 case 0x40: l2 = 0;
break;
859 case 0x41: l2 = 128;
break;
860 case 0x42: l2 = 256;
break;
861 case 0x43: l2 = 512;
break;
862 case 0x44: l2 = 1024;
break;
863 case 0x45: l2 = 2048;
break;
864 case 0x46: l3 = 4096;
break;
865 case 0x47: l3 = 8192;
break;
866 case 0x48: l2 = 3072;
break;
867 case 0x49:
if(l2!=0) l3 = 4096;
else {check_for_p2_core2=
true; l3 = l2 = 4096;}
break;
868 case 0x4A: l3 = 6144;
break;
869 case 0x4B: l3 = 8192;
break;
870 case 0x4C: l3 = 12288;
break;
871 case 0x4D: l3 = 16384;
break;
872 case 0x4E: l2 = 6144;
break;
873 case 0x78: l2 = 1024;
break;
874 case 0x79: l2 = 128;
break;
875 case 0x7A: l2 = 256;
break;
876 case 0x7B: l2 = 512;
break;
877 case 0x7C: l2 = 1024;
break;
878 case 0x7D: l2 = 2048;
break;
879 case 0x7E: l2 = 256;
break;
880 case 0x7F: l2 = 512;
break;
881 case 0x80: l2 = 512;
break;
882 case 0x81: l2 = 128;
break;
883 case 0x82: l2 = 256;
break;
884 case 0x83: l2 = 512;
break;
885 case 0x84: l2 = 1024;
break;
886 case 0x85: l2 = 2048;
break;
887 case 0x86: l2 = 512;
break;
888 case 0x87: l2 = 1024;
break;
889 case 0x88: l3 = 2048;
break;
890 case 0x89: l3 = 4096;
break;
891 case 0x8A: l3 = 8192;
break;
892 case 0x8D: l3 = 3072;
break;
897 if(check_for_p2_core2 && l2 == l3)
904 inline void queryCacheSizes_intel(
int& l1,
int& l2,
int& l3,
int max_std_funcs)
907 queryCacheSizes_intel_direct(l1,l2,l3);
909 queryCacheSizes_intel_codes(l1,l2,l3);
912 inline void queryCacheSizes_amd(
int& l1,
int& l2,
int& l3)
915 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
916 EIGEN_CPUID(abcd,0x80000005,0);
917 l1 = (abcd[2] >> 24) * 1024;
918 abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
919 EIGEN_CPUID(abcd,0x80000006,0);
920 l2 = (abcd[2] >> 16) * 1024;
921 l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024;
933 EIGEN_CPUID(abcd,0x0,0);
934 int max_std_funcs = abcd[1];
935 if(cpuid_is_vendor(abcd,
"GenuineIntel"))
936 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
937 else if(cpuid_is_vendor(abcd,
"AuthenticAMD") || cpuid_is_vendor(abcd,
"AMDisbetter!"))
938 queryCacheSizes_amd(l1,l2,l3);
941 queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
972 int l1, l2(-1), l3(-1);
974 return (std::max)(l2,l3);
981 #endif // EIGEN_MEMORY_H
static void run(const T *start, const T *end, T *target)
void * conditional_aligned_malloc< false >(size_t size)
void * handmade_aligned_realloc(void *ptr, std::size_t size, std::size_t=0)
bool operator!=(const aligned_allocator< T > &) const
static Index first_multiple(Index size, Index base)
void conditional_aligned_free(void *ptr)
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
T * conditional_aligned_realloc_new_auto(T *pts, size_t new_size, size_t old_size)
void * aligned_realloc(void *ptr, size_t new_size, size_t old_size)
#define EIGEN_UNUSED_VARIABLE(var)
iterative scaling algorithm to equilibrate rows and column norms in matrices
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
void * generic_aligned_realloc(void *ptr, size_t size, size_t old_size)
pointer address(reference value) const
void aligned_free(void *ptr)
T * conditional_aligned_new_auto(size_t size)
aligned_allocator(const aligned_allocator &)
aligned_allocator< U > other
const_pointer address(const_reference value) const
void conditional_aligned_free< false >(void *ptr)
void * handmade_aligned_malloc(std::size_t size)
const T & const_reference
T * conditional_aligned_realloc_new(T *pts, size_t new_size, size_t old_size)
T * conditional_aligned_new(size_t size)
size_type max_size() const
void throw_std_bad_alloc()
~aligned_stack_memory_handler()
STL compatible allocator to use with with 16 byte aligned types.
void construct(pointer p, const T &value)
void * conditional_aligned_realloc< false >(void *ptr, size_t new_size, size_t)
void queryCacheSizes(int &l1, int &l2, int &l3)
pointer allocate(size_type num, const void *hint=0)
void smart_copy(const T *start, const T *end, T *target)
T * construct_elements_of_array(T *ptr, size_t size)
void conditional_aligned_delete_auto(T *ptr, size_t size)
void aligned_delete(T *ptr, size_t size)
void deallocate(pointer p, size_type)
void conditional_aligned_delete(T *ptr, size_t size)
T * aligned_new(size_t size)
std::ptrdiff_t difference_type
void check_that_malloc_is_allowed()
bool operator==(const aligned_allocator< T > &) const
void * conditional_aligned_malloc(size_t size)
#define EIGEN_ALWAYS_INLINE
static void run(const T *start, const T *end, T *target)
void handmade_aligned_free(void *ptr)
aligned_stack_memory_handler(T *ptr, size_t size, bool dealloc)
static Derived::Index first_aligned(const Derived &m)
void * conditional_aligned_realloc(void *ptr, size_t new_size, size_t old_size)
void destruct_elements_of_array(T *ptr, size_t size)
int queryTopLevelCacheSize()
aligned_allocator(const aligned_allocator< U > &)
void * aligned_malloc(std::size_t size)
EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)