Eigen  3.2.93
Memory.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 // Copyright (C) 2009 Kenneth Riddile <kfriddile@yahoo.com>
7 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
8 // Copyright (C) 2010 Thomas Capricelli <orzel@freehackers.org>
9 // Copyright (C) 2013 Pavel Holoborodko <pavel@holoborodko.com>
10 //
11 // This Source Code Form is subject to the terms of the Mozilla
12 // Public License v. 2.0. If a copy of the MPL was not distributed
13 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 
15 
16 /*****************************************************************************
17 *** Platform checks for aligned malloc functions ***
18 *****************************************************************************/
19 
20 #ifndef EIGEN_MEMORY_H
21 #define EIGEN_MEMORY_H
22 
23 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
24 
25 // Try to determine automatically if malloc is already aligned.
26 
27 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
28 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
29 // This is true at least since glibc 2.8.
30 // This leaves the question how to detect 64-bit. According to this document,
31 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
32 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
33 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
34 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
35  && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
36  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
37 #else
38  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
39 #endif
40 
41 // FreeBSD 6 seems to have 16-byte aligned malloc
42 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
43 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
44 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
45 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
46  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
47 #else
48  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
49 #endif
50 
51 #if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
52  || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) \
53  || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
54  || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
55  #define EIGEN_MALLOC_ALREADY_ALIGNED 1
56 #else
57  #define EIGEN_MALLOC_ALREADY_ALIGNED 0
58 #endif
59 
60 #endif
61 
62 namespace Eigen {
63 
64 namespace internal {
65 
66 EIGEN_DEVICE_FUNC
67 inline void throw_std_bad_alloc()
68 {
69  #ifdef EIGEN_EXCEPTIONS
70  throw std::bad_alloc();
71  #else
72  std::size_t huge = static_cast<std::size_t>(-1);
73  new int[huge];
74  #endif
75 }
76 
77 /*****************************************************************************
78 *** Implementation of handmade aligned functions ***
79 *****************************************************************************/
80 
81 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
82 
86 inline void* handmade_aligned_malloc(std::size_t size)
87 {
88  void *original = std::malloc(size+EIGEN_DEFAULT_ALIGN_BYTES);
89  if (original == 0) return 0;
90  void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
91  *(reinterpret_cast<void**>(aligned) - 1) = original;
92  return aligned;
93 }
94 
96 inline void handmade_aligned_free(void *ptr)
97 {
98  if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
99 }
100 
106 inline void* handmade_aligned_realloc(void* ptr, std::size_t size, std::size_t = 0)
107 {
108  if (ptr == 0) return handmade_aligned_malloc(size);
109  void *original = *(reinterpret_cast<void**>(ptr) - 1);
110  std::ptrdiff_t previous_offset = static_cast<char *>(ptr)-static_cast<char *>(original);
111  original = std::realloc(original,size+EIGEN_DEFAULT_ALIGN_BYTES);
112  if (original == 0) return 0;
113  void *aligned = reinterpret_cast<void*>((reinterpret_cast<std::size_t>(original) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1))) + EIGEN_DEFAULT_ALIGN_BYTES);
114  void *previous_aligned = static_cast<char *>(original)+previous_offset;
115  if(aligned!=previous_aligned)
116  std::memmove(aligned, previous_aligned, size);
117 
118  *(reinterpret_cast<void**>(aligned) - 1) = original;
119  return aligned;
120 }
121 
122 /*****************************************************************************
123 *** Implementation of portable aligned versions of malloc/free/realloc ***
124 *****************************************************************************/
125 
126 #ifdef EIGEN_NO_MALLOC
127 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
128 {
129  eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
130 }
131 #elif defined EIGEN_RUNTIME_NO_MALLOC
132 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
133 {
134  static bool value = true;
135  if (update == 1)
136  value = new_value;
137  return value;
138 }
139 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
140 EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
141 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
142 {
143  eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
144 }
145 #else
146 EIGEN_DEVICE_FUNC inline void check_that_malloc_is_allowed()
147 {}
148 #endif
149 
153 EIGEN_DEVICE_FUNC inline void* aligned_malloc(size_t size)
154 {
155  check_that_malloc_is_allowed();
156 
157  void *result;
158  #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
159  result = std::malloc(size);
160  #if EIGEN_DEFAULT_ALIGN_BYTES==16
161  eigen_assert((size<16 || (std::size_t(result)%16)==0) && "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback to handmade alignd memory allocator.");
162  #endif
163  #else
164  result = handmade_aligned_malloc(size);
165  #endif
166 
167  if(!result && size)
168  throw_std_bad_alloc();
169 
170  return result;
171 }
172 
174 EIGEN_DEVICE_FUNC inline void aligned_free(void *ptr)
175 {
176  #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
177  std::free(ptr);
178  #else
179  handmade_aligned_free(ptr);
180  #endif
181 }
182 
188 inline void* aligned_realloc(void *ptr, size_t new_size, size_t old_size)
189 {
190  EIGEN_UNUSED_VARIABLE(old_size);
191 
192  void *result;
193 #if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
194  result = std::realloc(ptr,new_size);
195 #else
196  result = handmade_aligned_realloc(ptr,new_size,old_size);
197 #endif
198 
199  if (!result && new_size)
200  throw_std_bad_alloc();
201 
202  return result;
203 }
204 
205 /*****************************************************************************
206 *** Implementation of conditionally aligned functions ***
207 *****************************************************************************/
208 
212 template<bool Align> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc(size_t size)
213 {
214  return aligned_malloc(size);
215 }
216 
217 template<> EIGEN_DEVICE_FUNC inline void* conditional_aligned_malloc<false>(size_t size)
218 {
219  check_that_malloc_is_allowed();
220 
221  void *result = std::malloc(size);
222  if(!result && size)
223  throw_std_bad_alloc();
224  return result;
225 }
226 
228 template<bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_free(void *ptr)
229 {
230  aligned_free(ptr);
231 }
232 
233 template<> EIGEN_DEVICE_FUNC inline void conditional_aligned_free<false>(void *ptr)
234 {
235  std::free(ptr);
236 }
237 
238 template<bool Align> inline void* conditional_aligned_realloc(void* ptr, size_t new_size, size_t old_size)
239 {
240  return aligned_realloc(ptr, new_size, old_size);
241 }
242 
243 template<> inline void* conditional_aligned_realloc<false>(void* ptr, size_t new_size, size_t)
244 {
245  return std::realloc(ptr, new_size);
246 }
247 
248 /*****************************************************************************
249 *** Construction/destruction of array elements ***
250 *****************************************************************************/
251 
255 template<typename T> EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T *ptr, size_t size)
256 {
257  // always destruct an array starting from the end.
258  if(ptr)
259  while(size) ptr[--size].~T();
260 }
261 
265 template<typename T> EIGEN_DEVICE_FUNC inline T* construct_elements_of_array(T *ptr, size_t size)
266 {
267  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 }
279 
280 /*****************************************************************************
281 *** Implementation of aligned new/delete-like functions ***
282 *****************************************************************************/
283 
284 template<typename T>
285 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(size_t size)
286 {
287  if(size > size_t(-1) / sizeof(T))
288  throw_std_bad_alloc();
289 }
290 
295 template<typename T> EIGEN_DEVICE_FUNC inline T* aligned_new(size_t size)
296 {
297  check_size_for_overflow<T>(size);
298  T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
299  EIGEN_TRY
300  {
301  return construct_elements_of_array(result, size);
302  }
303  EIGEN_CATCH(...)
304  {
305  aligned_free(result);
306  EIGEN_THROW;
307  }
308 }
309 
310 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new(size_t size)
311 {
312  check_size_for_overflow<T>(size);
313  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
314  EIGEN_TRY
315  {
316  return construct_elements_of_array(result, size);
317  }
318  EIGEN_CATCH(...)
319  {
320  conditional_aligned_free<Align>(result);
321  EIGEN_THROW;
322  }
323 }
324 
328 template<typename T> EIGEN_DEVICE_FUNC inline void aligned_delete(T *ptr, size_t size)
329 {
330  destruct_elements_of_array<T>(ptr, size);
331  aligned_free(ptr);
332 }
333 
337 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T *ptr, size_t size)
338 {
339  destruct_elements_of_array<T>(ptr, size);
340  conditional_aligned_free<Align>(ptr);
341 }
342 
343 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
344 {
345  check_size_for_overflow<T>(new_size);
346  check_size_for_overflow<T>(old_size);
347  if(new_size < old_size)
348  destruct_elements_of_array(pts+new_size, old_size-new_size);
349  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
350  if(new_size > old_size)
351  {
352  EIGEN_TRY
353  {
354  construct_elements_of_array(result+old_size, new_size-old_size);
355  }
356  EIGEN_CATCH(...)
357  {
358  conditional_aligned_free<Align>(result);
359  EIGEN_THROW;
360  }
361  }
362  return result;
363 }
364 
365 
366 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline T* conditional_aligned_new_auto(size_t size)
367 {
368  if(size==0)
369  return 0; // short-cut. Also fixes Bug 884
370  check_size_for_overflow<T>(size);
371  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
372  if(NumTraits<T>::RequireInitialization)
373  {
374  EIGEN_TRY
375  {
376  construct_elements_of_array(result, size);
377  }
378  EIGEN_CATCH(...)
379  {
380  conditional_aligned_free<Align>(result);
381  EIGEN_THROW;
382  }
383  }
384  return result;
385 }
386 
387 template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(T* pts, size_t new_size, size_t old_size)
388 {
389  check_size_for_overflow<T>(new_size);
390  check_size_for_overflow<T>(old_size);
391  if(NumTraits<T>::RequireInitialization && (new_size < old_size))
392  destruct_elements_of_array(pts+new_size, old_size-new_size);
393  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
394  if(NumTraits<T>::RequireInitialization && (new_size > old_size))
395  {
396  EIGEN_TRY
397  {
398  construct_elements_of_array(result+old_size, new_size-old_size);
399  }
400  EIGEN_CATCH(...)
401  {
402  conditional_aligned_free<Align>(result);
403  EIGEN_THROW;
404  }
405  }
406  return result;
407 }
408 
409 template<typename T, bool Align> EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T *ptr, size_t size)
410 {
411  if(NumTraits<T>::RequireInitialization)
412  destruct_elements_of_array<T>(ptr, size);
413  conditional_aligned_free<Align>(ptr);
414 }
415 
416 /****************************************************************************/
417 
435 template<int Alignment, typename Scalar, typename Index>
436 EIGEN_DEVICE_FUNC inline Index first_aligned(const Scalar* array, Index size)
437 {
438  const Index ScalarSize = sizeof(Scalar);
439  const Index AlignmentSize = Alignment / ScalarSize;
440  const Index AlignmentMask = AlignmentSize-1;
441 
442  if(AlignmentSize<=1)
443  {
444  // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
445  // so that all elements of the array have the same alignment.
446  return 0;
447  }
448  else if( (UIntPtr(array) & (sizeof(Scalar)-1)) || (Alignment%ScalarSize)!=0)
449  {
450  // The array is not aligned to the size of a single scalar, or the requested alignment is not a multiple of the scalar size.
451  // Consequently, no element of the array is well aligned.
452  return size;
453  }
454  else
455  {
456  Index first = (AlignmentSize - (Index((UIntPtr(array)/sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
457  return (first < size) ? first : size;
458  }
459 }
460 
463 template<typename Scalar, typename Index>
464 EIGEN_DEVICE_FUNC inline Index first_default_aligned(const Scalar* array, Index size)
465 {
466  typedef typename packet_traits<Scalar>::type DefaultPacketType;
467  return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
468 }
469 
472 template<typename Index>
473 inline Index first_multiple(Index size, Index base)
474 {
475  return ((size+base-1)/base)*base;
476 }
477 
478 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
479 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
480 template<typename T, bool UseMemcpy> struct smart_copy_helper;
481 
482 template<typename T> EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target)
483 {
484  smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
485 }
486 
487 template<typename T> struct smart_copy_helper<T,true> {
488  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
489  {
490  IntPtr size = IntPtr(end)-IntPtr(start);
491  if(size==0) return;
492  eigen_internal_assert(start!=0 && end!=0 && target!=0);
493  memcpy(target, start, size);
494  }
495 };
496 
497 template<typename T> struct smart_copy_helper<T,false> {
498  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target)
499  { std::copy(start, end, target); }
500 };
501 
502 // intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
503 template<typename T, bool UseMemmove> struct smart_memmove_helper;
504 
505 template<typename T> void smart_memmove(const T* start, const T* end, T* target)
506 {
507  smart_memmove_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
508 }
509 
510 template<typename T> struct smart_memmove_helper<T,true> {
511  static inline void run(const T* start, const T* end, T* target)
512  {
513  IntPtr size = IntPtr(end)-IntPtr(start);
514  if(size==0) return;
515  eigen_internal_assert(start!=0 && end!=0 && target!=0);
516  std::memmove(target, start, size);
517  }
518 };
519 
520 template<typename T> struct smart_memmove_helper<T,false> {
521  static inline void run(const T* start, const T* end, T* target)
522  {
523  if (uintptr_t(target) < uintptr_t(start))
524  {
525  std::copy(start, end, target);
526  }
527  else
528  {
529  std::ptrdiff_t count = (std::ptrdiff_t(end)-std::ptrdiff_t(start)) / sizeof(T);
530  std::copy_backward(start, end, target + count);
531  }
532  }
533 };
534 
535 
536 /*****************************************************************************
537 *** Implementation of runtime stack allocation (falling back to malloc) ***
538 *****************************************************************************/
539 
540 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
541 // to the appropriate stack allocation function
542 #ifndef EIGEN_ALLOCA
543  #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
544  #define EIGEN_ALLOCA alloca
545  #elif EIGEN_COMP_MSVC
546  #define EIGEN_ALLOCA _alloca
547  #endif
548 #endif
549 
550 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
551 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
552 template<typename T> class aligned_stack_memory_handler : noncopyable
553 {
554  public:
555  /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
556  * Note that \a ptr can be 0 regardless of the other parameters.
557  * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type T (see NumTraits<T>::RequireInitialization).
558  * In this case, the buffer elements will also be destructed when this handler will be destructed.
559  * Finally, if \a dealloc is true, then the pointer \a ptr is freed.
560  **/
561  aligned_stack_memory_handler(T* ptr, size_t size, bool dealloc)
562  : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
563  {
564  if(NumTraits<T>::RequireInitialization && m_ptr)
565  Eigen::internal::construct_elements_of_array(m_ptr, size);
566  }
567  ~aligned_stack_memory_handler()
568  {
569  if(NumTraits<T>::RequireInitialization && m_ptr)
570  Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
571  if(m_deallocate)
572  Eigen::internal::aligned_free(m_ptr);
573  }
574  protected:
575  T* m_ptr;
576  size_t m_size;
577  bool m_deallocate;
578 };
579 
580 template<typename T> class scoped_array : noncopyable
581 {
582  T* m_ptr;
583 public:
584  explicit scoped_array(std::ptrdiff_t size)
585  {
586  m_ptr = new T[size];
587  }
588  ~scoped_array()
589  {
590  delete[] m_ptr;
591  }
592  T& operator[](std::ptrdiff_t i) { return m_ptr[i]; }
593  const T& operator[](std::ptrdiff_t i) const { return m_ptr[i]; }
594  T* &ptr() { return m_ptr; }
595  const T* ptr() const { return m_ptr; }
596  operator const T*() const { return m_ptr; }
597 };
598 
599 template<typename T> void swap(scoped_array<T> &a,scoped_array<T> &b)
600 {
601  std::swap(a.ptr(),b.ptr());
602 }
603 
604 } // end namespace internal
605 
621 #ifdef EIGEN_ALLOCA
622 
623  #if EIGEN_DEFAULT_ALIGN_BYTES>0
624  // We always manually re-align the result of EIGEN_ALLOCA.
625  // If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
626  #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((internal::UIntPtr(EIGEN_ALLOCA(SIZE+EIGEN_DEFAULT_ALIGN_BYTES-1)) + EIGEN_DEFAULT_ALIGN_BYTES-1) & ~(std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1)))
627  #else
628  #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
629  #endif
630 
631  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
632  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
633  TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
634  : reinterpret_cast<TYPE*>( \
635  (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
636  : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
637  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)
638 
639 #else
640 
641  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
642  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
643  TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
644  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
645 
646 #endif
647 
648 
649 /*****************************************************************************
650 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
651 *****************************************************************************/
652 
653 #if EIGEN_MAX_ALIGN_BYTES!=0
654  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
655  void* operator new(size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \
656  EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
657  EIGEN_CATCH (...) { return 0; } \
658  }
659  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
660  void *operator new(size_t size) { \
661  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
662  } \
663  void *operator new[](size_t size) { \
664  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
665  } \
666  void operator delete(void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
667  void operator delete[](void * ptr) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
668  void operator delete(void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
669  void operator delete[](void * ptr, std::size_t /* sz */) EIGEN_NO_THROW { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
670  /* in-place new and delete. since (at least afaik) there is no actual */ \
671  /* memory allocated we can safely let the default implementation handle */ \
672  /* this particular case. */ \
673  static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \
674  static void *operator new[](size_t size, void* ptr) { return ::operator new[](size,ptr); } \
675  void operator delete(void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete(memory,ptr); } \
676  void operator delete[](void * memory, void *ptr) EIGEN_NO_THROW { return ::operator delete[](memory,ptr); } \
677  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
678  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
679  void operator delete(void *ptr, const std::nothrow_t&) EIGEN_NO_THROW { \
680  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
681  } \
682  typedef void eigen_aligned_operator_new_marker_type;
683 #else
684  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
685 #endif
686 
687 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
688 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
689  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%EIGEN_MAX_ALIGN_BYTES==0)))
690 
691 /****************************************************************************/
692 
709 template<class T>
710 class aligned_allocator : public std::allocator<T>
711 {
712 public:
713  typedef size_t size_type;
714  typedef std::ptrdiff_t difference_type;
715  typedef T* pointer;
716  typedef const T* const_pointer;
717  typedef T& reference;
718  typedef const T& const_reference;
719  typedef T value_type;
720 
721  template<class U>
722  struct rebind
723  {
724  typedef aligned_allocator<U> other;
725  };
726 
727  aligned_allocator() : std::allocator<T>() {}
728 
729  aligned_allocator(const aligned_allocator& other) : std::allocator<T>(other) {}
730 
731  template<class U>
732  aligned_allocator(const aligned_allocator<U>& other) : std::allocator<T>(other) {}
733 
734  ~aligned_allocator() {}
735 
736  pointer allocate(size_type num, const void* /*hint*/ = 0)
737  {
738  internal::check_size_for_overflow<T>(num);
739  return static_cast<pointer>( internal::aligned_malloc(num * sizeof(T)) );
740  }
741 
742  void deallocate(pointer p, size_type /*num*/)
743  {
744  internal::aligned_free(p);
745  }
746 };
747 
748 //---------- Cache sizes ----------
749 
750 #if !defined(EIGEN_NO_CPUID)
751 # if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
752 # if defined(__PIC__) && EIGEN_ARCH_i386
753  // Case for x86 with PIC
754 # define EIGEN_CPUID(abcd,func,id) \
755  __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));
756 # elif defined(__PIC__) && EIGEN_ARCH_x86_64
757  // Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with the default small code model.
758  // However, we cannot detect which code model is used, and the xchg overhead is negligible anyway.
759 # define EIGEN_CPUID(abcd,func,id) \
760  __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));
761 # else
762  // Case for x86_64 or x86 w/o PIC
763 # define EIGEN_CPUID(abcd,func,id) \
764  __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "0" (func), "2" (id) );
765 # endif
766 # elif EIGEN_COMP_MSVC
767 # if (EIGEN_COMP_MSVC > 1500) && EIGEN_ARCH_i386_OR_x86_64
768 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
769 # endif
770 # endif
771 #endif
772 
773 namespace internal {
774 
775 #ifdef EIGEN_CPUID
776 
777 inline bool cpuid_is_vendor(int abcd[4], const int vendor[3])
778 {
779  return abcd[1]==vendor[0] && abcd[3]==vendor[1] && abcd[2]==vendor[2];
780 }
781 
782 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
783 {
784  int abcd[4];
785  l1 = l2 = l3 = 0;
786  int cache_id = 0;
787  int cache_type = 0;
788  do {
789  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
790  EIGEN_CPUID(abcd,0x4,cache_id);
791  cache_type = (abcd[0] & 0x0F) >> 0;
792  if(cache_type==1||cache_type==3) // data or unified cache
793  {
794  int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
795  int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
796  int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
797  int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
798  int sets = (abcd[2]); // C[31:0]
799 
800  int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
801 
802  switch(cache_level)
803  {
804  case 1: l1 = cache_size; break;
805  case 2: l2 = cache_size; break;
806  case 3: l3 = cache_size; break;
807  default: break;
808  }
809  }
810  cache_id++;
811  } while(cache_type>0 && cache_id<16);
812 }
813 
814 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
815 {
816  int abcd[4];
817  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
818  l1 = l2 = l3 = 0;
819  EIGEN_CPUID(abcd,0x00000002,0);
820  unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
821  bool check_for_p2_core2 = false;
822  for(int i=0; i<14; ++i)
823  {
824  switch(bytes[i])
825  {
826  case 0x0A: l1 = 8; break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
827  case 0x0C: l1 = 16; break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
828  case 0x0E: l1 = 24; break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
829  case 0x10: l1 = 16; break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
830  case 0x15: l1 = 16; break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
831  case 0x2C: l1 = 32; break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
832  case 0x30: l1 = 32; break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
833  case 0x60: l1 = 16; break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
834  case 0x66: l1 = 8; break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
835  case 0x67: l1 = 16; break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
836  case 0x68: l1 = 32; break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
837  case 0x1A: l2 = 96; break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
838  case 0x22: l3 = 512; break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
839  case 0x23: l3 = 1024; break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
840  case 0x25: l3 = 2048; break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
841  case 0x29: l3 = 4096; break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
842  case 0x39: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
843  case 0x3A: l2 = 192; break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
844  case 0x3B: l2 = 128; break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
845  case 0x3C: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
846  case 0x3D: l2 = 384; break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
847  case 0x3E: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
848  case 0x40: l2 = 0; break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
849  case 0x41: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
850  case 0x42: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
851  case 0x43: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
852  case 0x44: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
853  case 0x45: l2 = 2048; break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
854  case 0x46: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
855  case 0x47: l3 = 8192; break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
856  case 0x48: l2 = 3072; break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
857  case 0x49: if(l2!=0) l3 = 4096; else {check_for_p2_core2=true; l3 = l2 = 4096;} break;// code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
858  case 0x4A: l3 = 6144; break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
859  case 0x4B: l3 = 8192; break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
860  case 0x4C: l3 = 12288; break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
861  case 0x4D: l3 = 16384; break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
862  case 0x4E: l2 = 6144; break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
863  case 0x78: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
864  case 0x79: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
865  case 0x7A: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
866  case 0x7B: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
867  case 0x7C: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
868  case 0x7D: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
869  case 0x7E: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
870  case 0x7F: l2 = 512; break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
871  case 0x80: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
872  case 0x81: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
873  case 0x82: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
874  case 0x83: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
875  case 0x84: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
876  case 0x85: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
877  case 0x86: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
878  case 0x87: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
879  case 0x88: l3 = 2048; break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
880  case 0x89: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
881  case 0x8A: l3 = 8192; break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
882  case 0x8D: l3 = 3072; break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
883 
884  default: break;
885  }
886  }
887  if(check_for_p2_core2 && l2 == l3)
888  l3 = 0;
889  l1 *= 1024;
890  l2 *= 1024;
891  l3 *= 1024;
892 }
893 
894 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
895 {
896  if(max_std_funcs>=4)
897  queryCacheSizes_intel_direct(l1,l2,l3);
898  else
899  queryCacheSizes_intel_codes(l1,l2,l3);
900 }
901 
902 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
903 {
904  int abcd[4];
905  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
906  EIGEN_CPUID(abcd,0x80000005,0);
907  l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
908  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
909  EIGEN_CPUID(abcd,0x80000006,0);
910  l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
911  l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
912 }
913 #endif
914 
917 inline void queryCacheSizes(int& l1, int& l2, int& l3)
918 {
919  #ifdef EIGEN_CPUID
920  int abcd[4];
921  const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
922  const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
923  const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
924 
925  // identify the CPU vendor
926  EIGEN_CPUID(abcd,0x0,0);
927  int max_std_funcs = abcd[1];
928  if(cpuid_is_vendor(abcd,GenuineIntel))
929  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
930  else if(cpuid_is_vendor(abcd,AuthenticAMD) || cpuid_is_vendor(abcd,AMDisbetter_))
931  queryCacheSizes_amd(l1,l2,l3);
932  else
933  // by default let's use Intel's API
934  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
935 
936  // here is the list of other vendors:
937 // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
938 // ||cpuid_is_vendor(abcd,"CyrixInstead")
939 // ||cpuid_is_vendor(abcd,"CentaurHauls")
940 // ||cpuid_is_vendor(abcd,"GenuineTMx86")
941 // ||cpuid_is_vendor(abcd,"TransmetaCPU")
942 // ||cpuid_is_vendor(abcd,"RiseRiseRise")
943 // ||cpuid_is_vendor(abcd,"Geode by NSC")
944 // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
945 // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
946 // ||cpuid_is_vendor(abcd,"NexGenDriven")
947  #else
948  l1 = l2 = l3 = -1;
949  #endif
950 }
951 
954 inline int queryL1CacheSize()
955 {
956  int l1(-1), l2, l3;
957  queryCacheSizes(l1,l2,l3);
958  return l1;
959 }
960 
963 inline int queryTopLevelCacheSize()
964 {
965  int l1, l2(-1), l3(-1);
966  queryCacheSizes(l1,l2,l3);
967  return (std::max)(l2,l3);
968 }
969 
970 } // end namespace internal
971 
972 } // end namespace Eigen
973 
974 #endif // EIGEN_MEMORY_H
Namespace containing all symbols from the Eigen library.
Definition: Core:271
STL compatible allocator to use with with 16 byte aligned types.
Definition: Memory.h:710
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
Definition: Eigen_Colamd.h:50