Aligned memory allocation and manual vector instruction dispatch

In our code we are detecting the availability of SIMD instruction sets and then are dispatching to different compilation units that support this instruction set. Naturally, these compilation modules are compiled with support for these instruction sets an produce the corresponding instructions when using Eigen. The code that makes the dispatch decision runs on a baseline instruction set (SSE41 in our case).

The problem that I have found was crashes in the release build with mallocguard enabled. It turns out that, because the aligned malloc provided by Eigen is inlined and the alignment boundaries are SIMD instruction set dependent, objects who cross the compiled module boundaries during their lifetimes may be allocated and deallocated with different alignments. This wouldn't be a problem if the aligned pointer format was consistent for all alignment sizes, but unfortunately, it isn't. Eigen switches to plain allocation/deallocation if the alignment is already provided by the OS. This results in the possible issue of memory being allocated with standard malloc and attempted to deallocate with handmade aligned alloc.

I would suggest that you switch to consistently using handmade aligned alloc/dealloc, even if there is already OS alignment. I'm open to alternative solutions.

I'm aware that #define EIGEN_MALLOC_ALREADY_ALIGNED 0 resolves this problem, but it would be good to have this as the default.

Edited by atell soundtheory