Assume that malloc returns memory that is 16 byte-aligned

This fixes an issue with inconsistent memory allocation/deallocation of
MeshPoint in Python.
This commit is contained in:
Matthias 2017-02-07 10:26:31 +01:00
parent fd3d3e28e6
commit f674d5a20a

View File

@ -60,12 +60,30 @@ namespace netgen
template <typename T> template <typename T>
class AlignedAlloc 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: public:
void * operator new (size_t s, void *p) { return p; } 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 aligned_malloc(s); }
void * operator new[] (size_t s) { return _mm_malloc(s, alignof(T)); } void * operator new[] (size_t s) { return aligned_malloc(s); }
void operator delete (void * p) { _mm_free(p); } void operator delete (void * p) { aligned_free(p); }
void operator delete[] (void * p) { _mm_free(p); } void operator delete[] (void * p) { aligned_free(p); }
}; };
template<> template<>