From f674d5a20a2caed3b542ea9dc9a14b143097fcf1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 7 Feb 2017 10:26:31 +0100 Subject: [PATCH] Assume that malloc returns memory that is 16 byte-aligned This fixes an issue with inconsistent memory allocation/deallocation of MeshPoint in Python. --- libsrc/general/mysimd.hpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/libsrc/general/mysimd.hpp b/libsrc/general/mysimd.hpp index 62a87baf..124ff91b 100644 --- a/libsrc/general/mysimd.hpp +++ b/libsrc/general/mysimd.hpp @@ -60,12 +60,30 @@ namespace netgen template class AlignedAlloc { + protected: + static void * aligned_malloc(size_t s) + { + // Assume 16 byte alignment of standard library + if(alignof(T)<=16) + return malloc(s); + else + return _mm_malloc(s, alignof(T)); + } + + static void aligned_free(void *p) + { + if(alignof(T)<=16) + free(p); + else + _mm_free(p); + } + public: void * operator new (size_t s, void *p) { return p; } - void * operator new (size_t s) { return _mm_malloc(s, alignof(T)); } - void * operator new[] (size_t s) { return _mm_malloc(s, alignof(T)); } - void operator delete (void * p) { _mm_free(p); } - void operator delete[] (void * p) { _mm_free(p); } + void * operator new (size_t s) { return aligned_malloc(s); } + void * operator new[] (size_t s) { return aligned_malloc(s); } + void operator delete (void * p) { aligned_free(p); } + void operator delete[] (void * p) { aligned_free(p); } }; template<>