Memory.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 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 //
10 // Eigen is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 3 of the License, or (at your option) any later version.
14 //
15 // Alternatively, you can redistribute it and/or
16 // modify it under the terms of the GNU General Public License as
17 // published by the Free Software Foundation; either version 2 of
18 // the License, or (at your option) any later version.
19 //
20 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU Lesser General Public
26 // License and a copy of the GNU General Public License along with
27 // Eigen. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 /*****************************************************************************
31 *** Platform checks for aligned malloc functions ***
32 *****************************************************************************/
33 
34 #ifndef EIGEN_MEMORY_H
35 #define EIGEN_MEMORY_H
36 
37 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
38 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
39 // This is true at least since glibc 2.8.
40 // This leaves the question how to detect 64-bit. According to this document,
41 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
42 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
43 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
44 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
45  && defined(__LP64__)
46  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
47 #else
48  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
49 #endif
50 
51 // FreeBSD 6 seems to have 16-byte aligned malloc
52 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
53 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
54 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
55 #if defined(__FreeBSD__) && !defined(__arm__) && !defined(__mips__)
56  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
57 #else
58  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
59 #endif
60 
61 #if defined(__APPLE__) \
62  || defined(_WIN64) \
63  || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED \
64  || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
65  #define EIGEN_MALLOC_ALREADY_ALIGNED 1
66 #else
67  #define EIGEN_MALLOC_ALREADY_ALIGNED 0
68 #endif
69 
70 #if ((defined __QNXNTO__) || (defined _GNU_SOURCE) || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 600))) \
71  && (defined _POSIX_ADVISORY_INFO) && (_POSIX_ADVISORY_INFO > 0)
72  #define EIGEN_HAS_POSIX_MEMALIGN 1
73 #else
74  #define EIGEN_HAS_POSIX_MEMALIGN 0
75 #endif
76 
77 #ifdef EIGEN_VECTORIZE_SSE
78  #define EIGEN_HAS_MM_MALLOC 1
79 #else
80  #define EIGEN_HAS_MM_MALLOC 0
81 #endif
82 
83 namespace Eigen {
84 
85 namespace internal {
86 
87 inline void throw_std_bad_alloc()
88 {
89  #ifdef EIGEN_EXCEPTIONS
90  throw std::bad_alloc();
91  #else
92  std::size_t huge = -1;
93  new int[huge];
94  #endif
95 }
96 
97 /*****************************************************************************
98 *** Implementation of handmade aligned functions ***
99 *****************************************************************************/
100 
101 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
102 
106 inline void* handmade_aligned_malloc(size_t size)
107 {
108  void *original = std::malloc(size+16);
109  if (original == 0) return 0;
110  void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
111  *(reinterpret_cast<void**>(aligned) - 1) = original;
112  return aligned;
113 }
114 
116 inline void handmade_aligned_free(void *ptr)
117 {
118  if (ptr) std::free(*(reinterpret_cast<void**>(ptr) - 1));
119 }
120 
126 inline void* handmade_aligned_realloc(void* ptr, size_t size, size_t = 0)
127 {
128  if (ptr == 0) return handmade_aligned_malloc(size);
129  void *original = *(reinterpret_cast<void**>(ptr) - 1);
130  original = std::realloc(original,size+16);
131  if (original == 0) return 0;
132  void *aligned = reinterpret_cast<void*>((reinterpret_cast<size_t>(original) & ~(size_t(15))) + 16);
133  *(reinterpret_cast<void**>(aligned) - 1) = original;
134  return aligned;
135 }
136 
137 /*****************************************************************************
138 *** Implementation of generic aligned realloc (when no realloc can be used)***
139 *****************************************************************************/
140 
141 void* aligned_malloc(size_t size);
142 void aligned_free(void *ptr);
143 
149 inline void* generic_aligned_realloc(void* ptr, size_t size, size_t old_size)
150 {
151  if (ptr==0)
152  return aligned_malloc(size);
153 
154  if (size==0)
155  {
156  aligned_free(ptr);
157  return 0;
158  }
159 
160  void* newptr = aligned_malloc(size);
161  if (newptr == 0)
162  {
163  #ifdef EIGEN_HAS_ERRNO
164  errno = ENOMEM; // according to the standard
165  #endif
166  return 0;
167  }
168 
169  if (ptr != 0)
170  {
171  std::memcpy(newptr, ptr, (std::min)(size,old_size));
172  aligned_free(ptr);
173  }
174 
175  return newptr;
176 }
177 
178 /*****************************************************************************
179 *** Implementation of portable aligned versions of malloc/free/realloc ***
180 *****************************************************************************/
181 
182 #ifdef EIGEN_NO_MALLOC
183 inline void check_that_malloc_is_allowed()
184 {
185  eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
186 }
187 #elif defined EIGEN_RUNTIME_NO_MALLOC
188 inline bool is_malloc_allowed_impl(bool update, bool new_value = false)
189 {
190  static bool value = true;
191  if (update == 1)
192  value = new_value;
193  return value;
194 }
195 inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
196 inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
197 inline void check_that_malloc_is_allowed()
198 {
199  eigen_assert(is_malloc_allowed() && "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
200 }
201 #else
203 {}
204 #endif
205 
209 inline void* aligned_malloc(size_t size)
210 {
212 
213  void *result;
214  #if !EIGEN_ALIGN
215  result = std::malloc(size);
216  #elif EIGEN_MALLOC_ALREADY_ALIGNED
217  result = std::malloc(size);
218  #elif EIGEN_HAS_POSIX_MEMALIGN
219  if(posix_memalign(&result, 16, size)) result = 0;
220  #elif EIGEN_HAS_MM_MALLOC
221  result = _mm_malloc(size, 16);
222  #elif (defined _MSC_VER)
223  result = _aligned_malloc(size, 16);
224  #else
225  result = handmade_aligned_malloc(size);
226  #endif
227 
228  if(!result && size)
230 
231  return result;
232 }
233 
235 inline void aligned_free(void *ptr)
236 {
237  #if !EIGEN_ALIGN
238  std::free(ptr);
239  #elif EIGEN_MALLOC_ALREADY_ALIGNED
240  std::free(ptr);
241  #elif EIGEN_HAS_POSIX_MEMALIGN
242  std::free(ptr);
243  #elif EIGEN_HAS_MM_MALLOC
244  _mm_free(ptr);
245  #elif defined(_MSC_VER)
246  _aligned_free(ptr);
247  #else
249  #endif
250 }
251 
257 inline void* aligned_realloc(void *ptr, size_t new_size, size_t old_size)
258 {
259  EIGEN_UNUSED_VARIABLE(old_size);
260 
261  void *result;
262 #if !EIGEN_ALIGN
263  result = std::realloc(ptr,new_size);
264 #elif EIGEN_MALLOC_ALREADY_ALIGNED
265  result = std::realloc(ptr,new_size);
266 #elif EIGEN_HAS_POSIX_MEMALIGN
267  result = generic_aligned_realloc(ptr,new_size,old_size);
268 #elif EIGEN_HAS_MM_MALLOC
269  // The defined(_mm_free) is just here to verify that this MSVC version
270  // implements _mm_malloc/_mm_free based on the corresponding _aligned_
271  // functions. This may not always be the case and we just try to be safe.
272  #if defined(_MSC_VER) && defined(_mm_free)
273  result = _aligned_realloc(ptr,new_size,16);
274  #else
275  result = generic_aligned_realloc(ptr,new_size,old_size);
276  #endif
277 #elif defined(_MSC_VER)
278  result = _aligned_realloc(ptr,new_size,16);
279 #else
280  result = handmade_aligned_realloc(ptr,new_size,old_size);
281 #endif
282 
283  if (!result && new_size)
285 
286  return result;
287 }
288 
289 /*****************************************************************************
290 *** Implementation of conditionally aligned functions ***
291 *****************************************************************************/
292 
296 template<bool Align> inline void* conditional_aligned_malloc(size_t size)
297 {
298  return aligned_malloc(size);
299 }
300 
301 template<> inline void* conditional_aligned_malloc<false>(size_t size)
302 {
304 
305  void *result = std::malloc(size);
306  if(!result && size)
308  return result;
309 }
310 
312 template<bool Align> inline void conditional_aligned_free(void *ptr)
313 {
314  aligned_free(ptr);
315 }
316 
317 template<> inline void conditional_aligned_free<false>(void *ptr)
318 {
319  std::free(ptr);
320 }
321 
322 template<bool Align> inline void* conditional_aligned_realloc(void* ptr, size_t new_size, size_t old_size)
323 {
324  return aligned_realloc(ptr, new_size, old_size);
325 }
326 
327 template<> inline void* conditional_aligned_realloc<false>(void* ptr, size_t new_size, size_t)
328 {
329  return std::realloc(ptr, new_size);
330 }
331 
332 /*****************************************************************************
333 *** Construction/destruction of array elements ***
334 *****************************************************************************/
335 
339 template<typename T> inline T* construct_elements_of_array(T *ptr, size_t size)
340 {
341  for (size_t i=0; i < size; ++i) ::new (ptr + i) T;
342  return ptr;
343 }
344 
348 template<typename T> inline void destruct_elements_of_array(T *ptr, size_t size)
349 {
350  // always destruct an array starting from the end.
351  if(ptr)
352  while(size) ptr[--size].~T();
353 }
354 
355 /*****************************************************************************
356 *** Implementation of aligned new/delete-like functions ***
357 *****************************************************************************/
358 
359 template<typename T>
361 {
362  if(size > size_t(-1) / sizeof(T))
364 }
365 
370 template<typename T> inline T* aligned_new(size_t size)
371 {
372  check_size_for_overflow<T>(size);
373  T *result = reinterpret_cast<T*>(aligned_malloc(sizeof(T)*size));
374  return construct_elements_of_array(result, size);
375 }
376 
377 template<typename T, bool Align> inline T* conditional_aligned_new(size_t size)
378 {
379  check_size_for_overflow<T>(size);
380  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
381  return construct_elements_of_array(result, size);
382 }
383 
387 template<typename T> inline void aligned_delete(T *ptr, size_t size)
388 {
389  destruct_elements_of_array<T>(ptr, size);
390  aligned_free(ptr);
391 }
392 
396 template<typename T, bool Align> inline void conditional_aligned_delete(T *ptr, size_t size)
397 {
398  destruct_elements_of_array<T>(ptr, size);
399  conditional_aligned_free<Align>(ptr);
400 }
401 
402 template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pts, size_t new_size, size_t old_size)
403 {
404  check_size_for_overflow<T>(new_size);
405  check_size_for_overflow<T>(old_size);
406  if(new_size < old_size)
407  destruct_elements_of_array(pts+new_size, old_size-new_size);
408  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
409  if(new_size > old_size)
410  construct_elements_of_array(result+old_size, new_size-old_size);
411  return result;
412 }
413 
414 
415 template<typename T, bool Align> inline T* conditional_aligned_new_auto(size_t size)
416 {
417  check_size_for_overflow<T>(size);
418  T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
420  construct_elements_of_array(result, size);
421  return result;
422 }
423 
424 template<typename T, bool Align> inline T* conditional_aligned_realloc_new_auto(T* pts, size_t new_size, size_t old_size)
425 {
426  check_size_for_overflow<T>(new_size);
427  check_size_for_overflow<T>(old_size);
428  if(NumTraits<T>::RequireInitialization && (new_size < old_size))
429  destruct_elements_of_array(pts+new_size, old_size-new_size);
430  T *result = reinterpret_cast<T*>(conditional_aligned_realloc<Align>(reinterpret_cast<void*>(pts), sizeof(T)*new_size, sizeof(T)*old_size));
431  if(NumTraits<T>::RequireInitialization && (new_size > old_size))
432  construct_elements_of_array(result+old_size, new_size-old_size);
433  return result;
434 }
435 
436 template<typename T, bool Align> inline void conditional_aligned_delete_auto(T *ptr, size_t size)
437 {
439  destruct_elements_of_array<T>(ptr, size);
440  conditional_aligned_free<Align>(ptr);
441 }
442 
443 /****************************************************************************/
444 
461 template<typename Scalar, typename Index>
462 static inline Index first_aligned(const Scalar* array, Index size)
463 {
464  typedef typename packet_traits<Scalar>::type Packet;
465  enum { PacketSize = packet_traits<Scalar>::size,
466  PacketAlignedMask = PacketSize-1
467  };
468 
469  if(PacketSize==1)
470  {
471  // Either there is no vectorization, or a packet consists of exactly 1 scalar so that all elements
472  // of the array have the same alignment.
473  return 0;
474  }
475  else if(size_t(array) & (sizeof(Scalar)-1))
476  {
477  // There is vectorization for this scalar type, but the array is not aligned to the size of a single scalar.
478  // Consequently, no element of the array is well aligned.
479  return size;
480  }
481  else
482  {
483  return std::min<Index>( (PacketSize - (Index((size_t(array)/sizeof(Scalar))) & PacketAlignedMask))
484  & PacketAlignedMask, size);
485  }
486 }
487 
488 
489 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
490 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
491 template<typename T, bool UseMemcpy> struct smart_copy_helper;
492 
493 template<typename T> void smart_copy(const T* start, const T* end, T* target)
494 {
495  smart_copy_helper<T,!NumTraits<T>::RequireInitialization>::run(start, end, target);
496 }
497 
498 template<typename T> struct smart_copy_helper<T,true> {
499  static inline void run(const T* start, const T* end, T* target)
500  { memcpy(target, start, std::ptrdiff_t(end)-std::ptrdiff_t(start)); }
501 };
502 
503 template<typename T> struct smart_copy_helper<T,false> {
504  static inline void run(const T* start, const T* end, T* target)
505  { std::copy(start, end, target); }
506 };
507 
508 
509 /*****************************************************************************
510 *** Implementation of runtime stack allocation (falling back to malloc) ***
511 *****************************************************************************/
512 
513 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
514 // to the appropriate stack allocation function
515 #ifndef EIGEN_ALLOCA
516  #if (defined __linux__)
517  #define EIGEN_ALLOCA alloca
518  #elif defined(_MSC_VER)
519  #define EIGEN_ALLOCA _alloca
520  #endif
521 #endif
522 
523 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
524 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
525 template<typename T> class aligned_stack_memory_handler
526 {
527  public:
528  /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
529  * Note that \a ptr can be 0 regardless of the other parameters.
530  * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type T (see NumTraits<T>::RequireInitialization).
531  * In this case, the buffer elements will also be destructed when this handler will be destructed.
532  * Finally, if \a dealloc is true, then the pointer \a ptr is freed.
533  **/
534  aligned_stack_memory_handler(T* ptr, size_t size, bool dealloc)
535  : m_ptr(ptr), m_size(size), m_deallocate(dealloc)
536  {
537  if(NumTraits<T>::RequireInitialization && m_ptr)
539  }
540  ~aligned_stack_memory_handler()
541  {
542  if(NumTraits<T>::RequireInitialization && m_ptr)
543  Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
544  if(m_deallocate)
546  }
547  protected:
548  T* m_ptr;
549  size_t m_size;
550  bool m_deallocate;
551 };
552 
553 } // end namespace internal
554 
570 #ifdef EIGEN_ALLOCA
571 
572  #ifdef __arm__
573  #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<size_t>(EIGEN_ALLOCA(SIZE+16)) & ~(size_t(15))) + 16)
574  #else
575  #define EIGEN_ALIGNED_ALLOCA EIGEN_ALLOCA
576  #endif
577 
578  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
579  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
580  TYPE* NAME = (BUFFER)!=0 ? (BUFFER) \
581  : reinterpret_cast<TYPE*>( \
582  (sizeof(TYPE)*SIZE<=EIGEN_STACK_ALLOCATION_LIMIT) ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE)*SIZE) \
583  : Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE) ); \
584  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)
585 
586 #else
587 
588  #define ei_declare_aligned_stack_constructed_variable(TYPE,NAME,SIZE,BUFFER) \
589  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
590  TYPE* NAME = (BUFFER)!=0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE)*SIZE)); \
591  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME,_stack_memory_destructor)((BUFFER)==0 ? NAME : 0,SIZE,true)
592 
593 #endif
594 
595 
596 /*****************************************************************************
597 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
598 *****************************************************************************/
599 
600 #if EIGEN_ALIGN
601  #ifdef EIGEN_EXCEPTIONS
602  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
603  void* operator new(size_t size, const std::nothrow_t&) throw() { \
604  try { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
605  catch (...) { return 0; } \
606  return 0; \
607  }
608  #else
609  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
610  void* operator new(size_t size, const std::nothrow_t&) throw() { \
611  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
612  }
613  #endif
614 
615  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
616  void *operator new(size_t size) { \
617  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
618  } \
619  void *operator new[](size_t size) { \
620  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
621  } \
622  void operator delete(void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
623  void operator delete[](void * ptr) throw() { Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); } \
624  /* in-place new and delete. since (at least afaik) there is no actual */ \
625  /* memory allocated we can safely let the default implementation handle */ \
626  /* this particular case. */ \
627  static void *operator new(size_t size, void *ptr) { return ::operator new(size,ptr); } \
628  void operator delete(void * memory, void *ptr) throw() { return ::operator delete(memory,ptr); } \
629  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
630  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
631  void operator delete(void *ptr, const std::nothrow_t&) throw() { \
632  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
633  } \
634  typedef void eigen_aligned_operator_new_marker_type;
635 #else
636  #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
637 #endif
638 
639 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
640 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar,Size) \
641  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(((Size)!=Eigen::Dynamic) && ((sizeof(Scalar)*(Size))%16==0)))
642 
643 /****************************************************************************/
644 
661 template<class T>
663 {
664 public:
665  typedef size_t size_type;
666  typedef std::ptrdiff_t difference_type;
667  typedef T* pointer;
668  typedef const T* const_pointer;
669  typedef T& reference;
670  typedef const T& const_reference;
671  typedef T value_type;
672 
673  template<class U>
674  struct rebind
675  {
677  };
678 
679  pointer address( reference value ) const
680  {
681  return &value;
682  }
683 
685  {
686  return &value;
687  }
688 
690  {
691  }
692 
694  {
695  }
696 
697  template<class U>
699  {
700  }
701 
703  {
704  }
705 
707  {
708  return (std::numeric_limits<size_type>::max)();
709  }
710 
711  pointer allocate( size_type num, const void* hint = 0 )
712  {
713  EIGEN_UNUSED_VARIABLE(hint);
714  internal::check_size_for_overflow<T>(num);
715  return static_cast<pointer>( internal::aligned_malloc( num * sizeof(T) ) );
716  }
717 
718  void construct( pointer p, const T& value )
719  {
720  ::new( p ) T( value );
721  }
722 
723  // Support for c++11
724 #if (__cplusplus >= 201103L)
725  template<typename... Args>
726  void construct(pointer p, Args&&... args)
727  {
728  ::new(p) T(std::forward<Args>(args)...);
729  }
730 #endif
731 
732  void destroy( pointer p )
733  {
734  p->~T();
735  }
736 
737  void deallocate( pointer p, size_type /*num*/ )
738  {
740  }
741 
742  bool operator!=(const aligned_allocator<T>& ) const
743  { return false; }
744 
745  bool operator==(const aligned_allocator<T>& ) const
746  { return true; }
747 };
748 
749 //---------- Cache sizes ----------
750 
751 #if !defined(EIGEN_NO_CPUID)
752 # if defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
753 # if defined(__PIC__) && defined(__i386__)
754  // Case for x86 with PIC
755 # define EIGEN_CPUID(abcd,func,id) \
756  __asm__ __volatile__ ("xchgl %%ebx, %%esi;cpuid; xchgl %%ebx,%%esi": "=a" (abcd[0]), "=S" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id));
757 # else
758  // Case for x86_64 or x86 w/o PIC
759 # define EIGEN_CPUID(abcd,func,id) \
760  __asm__ __volatile__ ("cpuid": "=a" (abcd[0]), "=b" (abcd[1]), "=c" (abcd[2]), "=d" (abcd[3]) : "a" (func), "c" (id) );
761 # endif
762 # elif defined(_MSC_VER)
763 # if (_MSC_VER > 1500)
764 # define EIGEN_CPUID(abcd,func,id) __cpuidex((int*)abcd,func,id)
765 # endif
766 # endif
767 #endif
768 
769 namespace internal {
770 
771 #ifdef EIGEN_CPUID
772 
773 inline bool cpuid_is_vendor(int abcd[4], const char* vendor)
774 {
775  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];
776 }
777 
778 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3)
779 {
780  int abcd[4];
781  l1 = l2 = l3 = 0;
782  int cache_id = 0;
783  int cache_type = 0;
784  do {
785  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
786  EIGEN_CPUID(abcd,0x4,cache_id);
787  cache_type = (abcd[0] & 0x0F) >> 0;
788  if(cache_type==1||cache_type==3) // data or unified cache
789  {
790  int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
791  int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
792  int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
793  int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
794  int sets = (abcd[2]); // C[31:0]
795 
796  int cache_size = (ways+1) * (partitions+1) * (line_size+1) * (sets+1);
797 
798  switch(cache_level)
799  {
800  case 1: l1 = cache_size; break;
801  case 2: l2 = cache_size; break;
802  case 3: l3 = cache_size; break;
803  default: break;
804  }
805  }
806  cache_id++;
807  } while(cache_type>0 && cache_id<16);
808 }
809 
810 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3)
811 {
812  int abcd[4];
813  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
814  l1 = l2 = l3 = 0;
815  EIGEN_CPUID(abcd,0x00000002,0);
816  unsigned char * bytes = reinterpret_cast<unsigned char *>(abcd)+2;
817  bool check_for_p2_core2 = false;
818  for(int i=0; i<14; ++i)
819  {
820  switch(bytes[i])
821  {
822  case 0x0A: l1 = 8; break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
823  case 0x0C: l1 = 16; break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
824  case 0x0E: l1 = 24; break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
825  case 0x10: l1 = 16; break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
826  case 0x15: l1 = 16; break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
827  case 0x2C: l1 = 32; break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
828  case 0x30: l1 = 32; break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
829  case 0x60: l1 = 16; break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
830  case 0x66: l1 = 8; break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
831  case 0x67: l1 = 16; break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
832  case 0x68: l1 = 32; break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
833  case 0x1A: l2 = 96; break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
834  case 0x22: l3 = 512; break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
835  case 0x23: l3 = 1024; break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
836  case 0x25: l3 = 2048; break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
837  case 0x29: l3 = 4096; break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
838  case 0x39: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
839  case 0x3A: l2 = 192; break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
840  case 0x3B: l2 = 128; break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
841  case 0x3C: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
842  case 0x3D: l2 = 384; break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
843  case 0x3E: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
844  case 0x40: l2 = 0; break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
845  case 0x41: l2 = 128; break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
846  case 0x42: l2 = 256; break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
847  case 0x43: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
848  case 0x44: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
849  case 0x45: l2 = 2048; break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
850  case 0x46: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
851  case 0x47: l3 = 8192; break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
852  case 0x48: l2 = 3072; break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
853  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
854  case 0x4A: l3 = 6144; break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
855  case 0x4B: l3 = 8192; break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
856  case 0x4C: l3 = 12288; break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
857  case 0x4D: l3 = 16384; break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
858  case 0x4E: l2 = 6144; break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
859  case 0x78: l2 = 1024; break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
860  case 0x79: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
861  case 0x7A: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
862  case 0x7B: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
863  case 0x7C: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
864  case 0x7D: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
865  case 0x7E: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
866  case 0x7F: l2 = 512; break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
867  case 0x80: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
868  case 0x81: l2 = 128; break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
869  case 0x82: l2 = 256; break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
870  case 0x83: l2 = 512; break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
871  case 0x84: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
872  case 0x85: l2 = 2048; break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
873  case 0x86: l2 = 512; break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
874  case 0x87: l2 = 1024; break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
875  case 0x88: l3 = 2048; break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
876  case 0x89: l3 = 4096; break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
877  case 0x8A: l3 = 8192; break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
878  case 0x8D: l3 = 3072; break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
879 
880  default: break;
881  }
882  }
883  if(check_for_p2_core2 && l2 == l3)
884  l3 = 0;
885  l1 *= 1024;
886  l2 *= 1024;
887  l3 *= 1024;
888 }
889 
890 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs)
891 {
892  if(max_std_funcs>=4)
893  queryCacheSizes_intel_direct(l1,l2,l3);
894  else
895  queryCacheSizes_intel_codes(l1,l2,l3);
896 }
897 
898 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3)
899 {
900  int abcd[4];
901  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
902  EIGEN_CPUID(abcd,0x80000005,0);
903  l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
904  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
905  EIGEN_CPUID(abcd,0x80000006,0);
906  l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
907  l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
908 }
909 #endif
910 
913 inline void queryCacheSizes(int& l1, int& l2, int& l3)
914 {
915  #ifdef EIGEN_CPUID
916  int abcd[4];
917 
918  // identify the CPU vendor
919  EIGEN_CPUID(abcd,0x0,0);
920  int max_std_funcs = abcd[1];
921  if(cpuid_is_vendor(abcd,"GenuineIntel"))
922  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
923  else if(cpuid_is_vendor(abcd,"AuthenticAMD") || cpuid_is_vendor(abcd,"AMDisbetter!"))
924  queryCacheSizes_amd(l1,l2,l3);
925  else
926  // by default let's use Intel's API
927  queryCacheSizes_intel(l1,l2,l3,max_std_funcs);
928 
929  // here is the list of other vendors:
930 // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
931 // ||cpuid_is_vendor(abcd,"CyrixInstead")
932 // ||cpuid_is_vendor(abcd,"CentaurHauls")
933 // ||cpuid_is_vendor(abcd,"GenuineTMx86")
934 // ||cpuid_is_vendor(abcd,"TransmetaCPU")
935 // ||cpuid_is_vendor(abcd,"RiseRiseRise")
936 // ||cpuid_is_vendor(abcd,"Geode by NSC")
937 // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
938 // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
939 // ||cpuid_is_vendor(abcd,"NexGenDriven")
940  #else
941  l1 = l2 = l3 = -1;
942  #endif
943 }
944 
947 inline int queryL1CacheSize()
948 {
949  int l1(-1), l2, l3;
950  queryCacheSizes(l1,l2,l3);
951  return l1;
952 }
953 
957 {
958  int l1, l2(-1), l3(-1);
959  queryCacheSizes(l1,l2,l3);
960  return (std::max)(l2,l3);
961 }
962 
963 } // end namespace internal
964 
965 } // end namespace Eigen
966 
967 #endif // EIGEN_MEMORY_H