Skip to content

Commit d19952e

Browse files
committed
Eigen assumes that structures are aligned to 16 bytes, which is a requirement typically satisfied by a x64 compiler but not by a x86 compiler. This commit adds custom operators new/delete to various classes working with Eigen structures to guarantee the memory alignment.
1 parent 934899f commit d19952e

File tree

7 files changed

+95
-6
lines changed

7 files changed

+95
-6
lines changed

Common/Common.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,47 @@ namespace PBD
2929
using AlignedBox3r = Eigen::AlignedBox<Real, 3>;
3030
using AngleAxisr = Eigen::AngleAxis<Real>;
3131
using Quaternionr = Eigen::Quaternion<Real>;
32+
33+
//allocators to be used in STL collections containing Eigen structures
34+
using Alloc_Vector2r = Eigen::aligned_allocator<Vector2r>;
35+
using Alloc_Vector3r = Eigen::aligned_allocator<Vector2r>;
36+
using Alloc_Vector4r = Eigen::aligned_allocator<Vector4r>;
37+
using Alloc_Matrix2r = Eigen::aligned_allocator<Matrix2r>;
38+
using Alloc_Matrix3r = Eigen::aligned_allocator<Matrix3r>;
39+
using Alloc_Matrix4r = Eigen::aligned_allocator<Matrix4r>;
40+
using Alloc_AlignedBox2r = Eigen::aligned_allocator<AlignedBox2r>;
41+
using Alloc_AlignedBox3r = Eigen::aligned_allocator<AlignedBox3r>;
42+
using Alloc_AngleAxisr = Eigen::aligned_allocator<AngleAxisr>;
43+
using Alloc_Quaternionr = Eigen::aligned_allocator<Quaternionr>;
44+
45+
#if EIGEN_ALIGN
46+
#define PDB_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW
47+
48+
#if defined(WIN32) || defined(_WIN32) || defined(WIN64)
49+
#ifdef _DEBUG
50+
// Enable memory leak detection for Eigen new
51+
#undef PDB_MAKE_ALIGNED_OPERATOR_NEW
52+
#define PDB_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
53+
void *operator new(size_t size, int const block_use, char const* file_name, int const line_number) { \
54+
\
55+
return _aligned_malloc_dbg(size, 16, file_name, line_number); \
56+
} \
57+
void *operator new[](size_t size, int const block_use, char const* file_name, int const line_number) { \
58+
return operator new(size, block_use, file_name, line_number); \
59+
}\
60+
void operator delete(void* block, int const block_use, char const* file_name, int const line_number) noexcept { \
61+
\
62+
return _aligned_free_dbg(block); \
63+
} \
64+
void operator delete[](void* block, int const block_use, char const* file_name, int const line_number) noexcept { \
65+
return operator delete(block, block_use, file_name, line_number); \
66+
}
67+
68+
#endif
69+
#endif
70+
#else
71+
#define PDB_MAKE_ALIGNED_OPERATOR_NEW
72+
#endif
3273
}
3374

3475
#endif

Demos/Simulation/CollisionDetection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ namespace PBD
3333

3434
virtual ~CollisionObject() {}
3535
virtual int &getTypeId() const = 0;
36+
37+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
38+
PDB_MAKE_ALIGNED_OPERATOR_NEW
3639
};
3740

3841
struct CollisionObjectWithoutGeometry : public CollisionObject

Demos/Simulation/Constraints.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace PBD
2727
virtual bool updateConstraint(SimulationModel &model) { return true; };
2828
virtual bool solvePositionConstraint(SimulationModel &model) { return true; };
2929
virtual bool solveVelocityConstraint(SimulationModel &model) { return true; };
30+
31+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
32+
PDB_MAKE_ALIGNED_OPERATOR_NEW
3033
};
3134

3235
class BallJoint : public Constraint

Demos/Simulation/RigidBody.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ namespace PBD
539539
{
540540
return m_geometry;
541541
}
542+
543+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
544+
PDB_MAKE_ALIGNED_OPERATOR_NEW
542545
};
543546
}
544547

Demos/Utils/IndexedFaceMesh.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ namespace PBD
9595

9696
public:
9797
typedef std::vector<unsigned int> Faces;
98-
typedef std::vector<Vector3r> FaceNormals;
99-
typedef std::vector<Vector3r> VertexNormals;
98+
typedef std::vector<Vector3r, Alloc_Vector3r> FaceNormals;
99+
typedef std::vector<Vector3r, Alloc_Vector3r> VertexNormals;
100100
typedef std::vector<Face> FaceData;
101101
typedef std::vector<Edge> Edges;
102102
typedef std::vector<VertexFaces> VerticesFaces;
103103
typedef std::vector<VertexEdges> VerticesEdges;
104104
typedef std::vector<unsigned int> UVIndices;
105-
typedef std::vector<Vector2r> UVs;
105+
typedef std::vector<Vector2r, Alloc_Vector2r> UVs;
106106

107107
protected:
108108
unsigned int m_numPoints;

Demos/Utils/OBJLoader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ void OBJLoader::loadObj(const std::string &filename, VertexData &vertexData, Ind
3131
{
3232
std::cout << "Loading " << filename << std::endl;
3333

34-
vector<Vector3r> positions;
35-
vector<Vector2r> texcoords;
36-
vector<Vector3r> normals;
34+
vector<Vector3r, Alloc_Vector3r> positions;
35+
vector<Vector2r, Alloc_Vector2r> texcoords;
36+
vector<Vector3r, Alloc_Vector3r> normals;
3737
vector<MeshFaceIndices> faces;
3838

3939
ifstream filestream;

Demos/Utils/SceneLoader.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ namespace PBD
3434
bool m_testMesh;
3535
Vector3r m_collisionObjectScale;
3636
boost::property_tree::ptree *pt;
37+
38+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
39+
PDB_MAKE_ALIGNED_OPERATOR_NEW
3740
};
3841

3942
struct TriangleModelData
@@ -47,6 +50,9 @@ namespace PBD
4750
Real m_restitutionCoeff;
4851
Real m_frictionCoeff;
4952
boost::property_tree::ptree *pt;
53+
54+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
55+
PDB_MAKE_ALIGNED_OPERATOR_NEW
5056
};
5157

5258
struct TetModelData
@@ -63,40 +69,58 @@ namespace PBD
6369
Real m_restitutionCoeff;
6470
Real m_frictionCoeff;
6571
boost::property_tree::ptree *pt;
72+
73+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
74+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
6675
};
6776

6877
struct BallJointData
6978
{
7079
unsigned int m_bodyID[2];
7180
Vector3r m_position;
81+
82+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
83+
PDB_MAKE_ALIGNED_OPERATOR_NEW
7284
};
7385

7486
struct BallOnLineJointData
7587
{
7688
unsigned int m_bodyID[2];
7789
Vector3r m_position;
7890
Vector3r m_axis;
91+
92+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
93+
PDB_MAKE_ALIGNED_OPERATOR_NEW
7994
};
8095

8196
struct HingeJointData
8297
{
8398
unsigned int m_bodyID[2];
8499
Vector3r m_position;
85100
Vector3r m_axis;
101+
102+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
103+
PDB_MAKE_ALIGNED_OPERATOR_NEW
86104
};
87105

88106
struct UniversalJointData
89107
{
90108
unsigned int m_bodyID[2];
91109
Vector3r m_position;
92110
Vector3r m_axis[2];
111+
112+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
113+
PDB_MAKE_ALIGNED_OPERATOR_NEW
93114
};
94115

95116
struct SliderJointData
96117
{
97118
unsigned int m_bodyID[2];
98119
Vector3r m_position;
99120
Vector3r m_axis;
121+
122+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
123+
PDB_MAKE_ALIGNED_OPERATOR_NEW
100124
};
101125

102126
struct RigidBodyParticleBallJointData
@@ -110,6 +134,9 @@ namespace PBD
110134
Vector3r m_position;
111135
Vector3r m_axis;
112136
Real m_target;
137+
138+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
139+
PDB_MAKE_ALIGNED_OPERATOR_NEW
113140
};
114141

115142
struct TargetVelocityMotorHingeJointData
@@ -118,6 +145,9 @@ namespace PBD
118145
Vector3r m_position;
119146
Vector3r m_axis;
120147
Real m_target;
148+
149+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
150+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
121151
};
122152

123153
struct TargetPositionMotorSliderJointData
@@ -126,6 +156,9 @@ namespace PBD
126156
Vector3r m_position;
127157
Vector3r m_axis;
128158
Real m_target;
159+
160+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
161+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
129162
};
130163

131164
struct TargetVelocityMotorSliderJointData
@@ -134,6 +167,9 @@ namespace PBD
134167
Vector3r m_position;
135168
Vector3r m_axis;
136169
Real m_target;
170+
171+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
172+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
137173
};
138174

139175
struct SceneData
@@ -187,6 +223,9 @@ namespace PBD
187223
ObjectArray<TargetVelocityMotorHingeJointData> m_targetVelocityMotorHingeJointData;
188224
ObjectArray<TargetPositionMotorSliderJointData> m_targetPositionMotorSliderJointData;
189225
ObjectArray<TargetVelocityMotorSliderJointData> m_targetVelocityMotorSliderJointData;
226+
227+
public: //BES: 23.8.2016 - make sure the class is aligned to 16 bytes even for x86 build
228+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
190229
};
191230

192231
void readScene(const std::string &fileName, SceneData &sceneData);

0 commit comments

Comments
 (0)