mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
the big memory changes. the most important changes are:
ei_aligned_malloc now really behaves like a malloc (untyped, doesn't call ctor) ei_aligned_new is the typed variant calling ctor EIGEN_MAKE_ALIGNED_OPERATOR_NEW now takes the class name as parameter
This commit is contained in:
@@ -25,38 +25,17 @@
|
||||
#include "main.h"
|
||||
|
||||
// test compilation with both a struct and a class...
|
||||
struct MyStruct : WithAlignedOperatorNew
|
||||
struct MyStruct
|
||||
{
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW(MyStruct)
|
||||
char dummychar;
|
||||
Vector4f avec;
|
||||
};
|
||||
|
||||
class MyClassA : public WithAlignedOperatorNew
|
||||
{
|
||||
public:
|
||||
char dummychar;
|
||||
Vector4f avec;
|
||||
};
|
||||
|
||||
// ..as well as with some other base classes
|
||||
|
||||
class MyBaseClass
|
||||
{
|
||||
public:
|
||||
char dummychar;
|
||||
float afloat;
|
||||
};
|
||||
|
||||
class MyClassB : public WithAlignedOperatorNew, public MyBaseClass
|
||||
{
|
||||
public:
|
||||
char dummychar;
|
||||
Vector4f avec;
|
||||
};
|
||||
|
||||
class MyClassC : public MyBaseClass, public WithAlignedOperatorNew
|
||||
class MyClassA
|
||||
{
|
||||
public:
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW(MyClassA)
|
||||
char dummychar;
|
||||
Vector4f avec;
|
||||
};
|
||||
@@ -85,8 +64,6 @@ void test_dynalloc()
|
||||
{
|
||||
MyStruct foo0; VERIFY(size_t(foo0.avec.data())%16==0);
|
||||
MyClassA fooA; VERIFY(size_t(fooA.avec.data())%16==0);
|
||||
MyClassB fooB; VERIFY(size_t(fooB.avec.data())%16==0);
|
||||
MyClassC fooC; VERIFY(size_t(fooC.avec.data())%16==0);
|
||||
}
|
||||
|
||||
// dynamic allocation, single object
|
||||
@@ -94,12 +71,8 @@ void test_dynalloc()
|
||||
{
|
||||
MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%16==0);
|
||||
MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%16==0);
|
||||
MyClassB *fooB = new MyClassB(); VERIFY(size_t(fooB->avec.data())%16==0);
|
||||
MyClassC *fooC = new MyClassC(); VERIFY(size_t(fooC->avec.data())%16==0);
|
||||
delete foo0;
|
||||
delete fooA;
|
||||
delete fooB;
|
||||
delete fooC;
|
||||
}
|
||||
|
||||
// dynamic allocation, array
|
||||
@@ -108,12 +81,8 @@ void test_dynalloc()
|
||||
{
|
||||
MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%16==0);
|
||||
MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%16==0);
|
||||
MyClassB *fooB = new MyClassB[N]; VERIFY(size_t(fooB->avec.data())%16==0);
|
||||
MyClassC *fooC = new MyClassC[N]; VERIFY(size_t(fooC->avec.data())%16==0);
|
||||
delete[] foo0;
|
||||
delete[] fooA;
|
||||
delete[] fooB;
|
||||
delete[] fooC;
|
||||
}
|
||||
|
||||
// std::vector
|
||||
|
||||
16
test/map.cpp
16
test/map.cpp
@@ -31,8 +31,8 @@ template<typename VectorType> void map_class(const VectorType& m)
|
||||
int size = m.size();
|
||||
|
||||
// test Map.h
|
||||
Scalar* array1 = ei_aligned_malloc<Scalar>(size);
|
||||
Scalar* array2 = ei_aligned_malloc<Scalar>(size);
|
||||
Scalar* array1 = ei_aligned_new<Scalar>(size);
|
||||
Scalar* array2 = ei_aligned_new<Scalar>(size);
|
||||
Scalar* array3 = new Scalar[size+1];
|
||||
Scalar* array3unaligned = size_t(array3)%16 == 0 ? array3+1 : array3;
|
||||
|
||||
@@ -45,8 +45,8 @@ template<typename VectorType> void map_class(const VectorType& m)
|
||||
VERIFY_IS_APPROX(ma1, ma2);
|
||||
VERIFY_IS_APPROX(ma1, ma3);
|
||||
|
||||
ei_aligned_free(array1, size);
|
||||
ei_aligned_free(array2, size);
|
||||
ei_aligned_delete(array1, size);
|
||||
ei_aligned_delete(array2, size);
|
||||
delete[] array3;
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ template<typename VectorType> void map_static_methods(const VectorType& m)
|
||||
int size = m.size();
|
||||
|
||||
// test Map.h
|
||||
Scalar* array1 = ei_aligned_malloc<Scalar>(size);
|
||||
Scalar* array2 = ei_aligned_malloc<Scalar>(size);
|
||||
Scalar* array1 = ei_aligned_new<Scalar>(size);
|
||||
Scalar* array2 = ei_aligned_new<Scalar>(size);
|
||||
Scalar* array3 = new Scalar[size+1];
|
||||
Scalar* array3unaligned = size_t(array3)%16 == 0 ? array3+1 : array3;
|
||||
|
||||
@@ -71,8 +71,8 @@ template<typename VectorType> void map_static_methods(const VectorType& m)
|
||||
VERIFY_IS_APPROX(ma1, ma2);
|
||||
VERIFY_IS_APPROX(ma1, ma3);
|
||||
|
||||
ei_aligned_free(array1, size);
|
||||
ei_aligned_free(array2, size);
|
||||
ei_aligned_delete(array1, size);
|
||||
ei_aligned_delete(array2, size);
|
||||
delete[] array3;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,28 +25,13 @@
|
||||
|
||||
// this hack is needed to make this file compiles with -pedantic (gcc)
|
||||
#define throw(X)
|
||||
// discard vectorization since the global operator new is not called in that case
|
||||
#define EIGEN_DONT_VECTORIZE 1
|
||||
// discard stack allocation as that too bypasses the global operator new
|
||||
// discard stack allocation as that too bypasses malloc
|
||||
#define EIGEN_STACK_ALLOCATION_LIMIT 0
|
||||
// any heap allocation will raise an assert
|
||||
#define EIGEN_NO_MALLOC
|
||||
|
||||
#include "main.h"
|
||||
|
||||
void* operator new[] (size_t n)
|
||||
{
|
||||
ei_assert(false && "operator new should never be called with fixed size path");
|
||||
// the following is in case assertion are disabled
|
||||
std::cerr << "operator new should never be called with fixed size path" << std::endl;
|
||||
exit(2);
|
||||
void* p = malloc(n);
|
||||
return p;
|
||||
}
|
||||
|
||||
void operator delete[](void* p) throw()
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
|
||||
template<typename MatrixType> void nomalloc(const MatrixType& m)
|
||||
{
|
||||
/* this test check no dynamic memory allocation are issued with fixed-size matrices
|
||||
|
||||
@@ -55,15 +55,16 @@ struct Bad6
|
||||
Matrix<double, 3, 4> m; // bad: same reason
|
||||
};
|
||||
|
||||
struct Good7 : Eigen::WithAlignedOperatorNew
|
||||
struct Good7
|
||||
{
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW(Good7)
|
||||
Vector2d m;
|
||||
float f; // make the struct have sizeof%16!=0 to make it a little more tricky when we allow an array of 2 such objects
|
||||
};
|
||||
|
||||
struct Good8
|
||||
{
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW(Good8)
|
||||
float f; // try the f at first -- the EIGEN_ALIGN_128 attribute of m should make that still work
|
||||
Matrix4f m;
|
||||
};
|
||||
@@ -76,7 +77,7 @@ struct Good9
|
||||
|
||||
template<bool Align> struct Depends
|
||||
{
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Align)
|
||||
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(Depends,Align)
|
||||
Vector2d m;
|
||||
float f;
|
||||
};
|
||||
@@ -97,7 +98,7 @@ void check_unalignedassert_bad()
|
||||
float buf[sizeof(T)+16];
|
||||
float *unaligned = buf;
|
||||
while((reinterpret_cast<size_t>(unaligned)&0xf)==0) ++unaligned; // make sure unaligned is really unaligned
|
||||
T *x = new(static_cast<void*>(unaligned)) T;
|
||||
T *x = ::new(static_cast<void*>(unaligned)) T;
|
||||
x->~T();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user