[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to build compatible packages that use Eigen?



I hit this problem again at work yesterday, so let's fix it.

I'm attaching a patch that can be added to the quilt patch stack in our
libeigen3-dev package. This disables the conditional preprocessor logic
that selects the allocator/deallocator behavior. With this patch we
always make no assumptions about the alignment of malloc(), and we
ALWAYS use eigen's manual allocator to force alignment. This is maybe a
bit less efficient than the other path, but dynamic allocation shouldn't
be in any hot code path anyway.

We could also use aligned_alloc() or memalign() instead here, also
unconditionally. The "unconditional" is the important part: it must be
consistent with any build flags, otherwise stuff can crash.

Is this reasonable?

Thanks.

Description: https://www.mail-archive.com/debian-science@lists.debian.org/msg13666.html
 This patch ensures that Eigen memory allocation/free are implemented the same
 way REGARDLESS of build flags. This ensures that an application using Eigen
 (possibly indirectly, through libraries) will not crash due to non-identical
 build flags
Author: Dima Kogan <dkogan@debian.org>
diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h
index 875318c..38d55d1 100644
--- a/Eigen/src/Core/util/Memory.h
+++ b/Eigen/src/Core/util/Memory.h
@@ -17,50 +17,13 @@
 *** Platform checks for aligned malloc functions                           ***
 *****************************************************************************/
 
 #ifndef EIGEN_MEMORY_H
 #define EIGEN_MEMORY_H
 
-#ifndef EIGEN_MALLOC_ALREADY_ALIGNED
-
-// Try to determine automatically if malloc is already aligned.
-
-// On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
-//   http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
-// This is true at least since glibc 2.8.
-// This leaves the question how to detect 64-bit. According to this document,
-//   http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
-// page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
-// quite safe, at least within the context of glibc, to equate 64-bit with LP64.
-#if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 8) || __GLIBC__>2) \
- && defined(__LP64__) && ! defined( __SANITIZE_ADDRESS__ ) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
-  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
-#else
-  #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
-#endif
-
-// 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__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
-  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
-#else
-  #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
-#endif
-
-#if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16))     \
- || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16))   \
- || EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED              \
- || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
-  #define EIGEN_MALLOC_ALREADY_ALIGNED 1
-#else
-  #define EIGEN_MALLOC_ALREADY_ALIGNED 0
-#endif
-
-#endif
+#define EIGEN_MALLOC_ALREADY_ALIGNED 0
 
 namespace Eigen {
 
 namespace internal {
 
 EIGEN_DEVICE_FUNC

Reply to: