1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
2 | // for linear algebra.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
---|
5 | //
|
---|
6 | // This Source Code Form is subject to the terms of the Mozilla
|
---|
7 | // Public License v. 2.0. If a copy of the MPL was not distributed
|
---|
8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
---|
9 |
|
---|
10 | #include "main.h"
|
---|
11 |
|
---|
12 | #if EIGEN_ALIGN
|
---|
13 | #define ALIGNMENT 16
|
---|
14 | #else
|
---|
15 | #define ALIGNMENT 1
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | void check_handmade_aligned_malloc()
|
---|
19 | {
|
---|
20 | for(int i = 1; i < 1000; i++)
|
---|
21 | {
|
---|
22 | char *p = (char*)internal::handmade_aligned_malloc(i);
|
---|
23 | VERIFY(size_t(p)%ALIGNMENT==0);
|
---|
24 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
|
---|
25 | for(int j = 0; j < i; j++) p[j]=0;
|
---|
26 | internal::handmade_aligned_free(p);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | void check_aligned_malloc()
|
---|
31 | {
|
---|
32 | for(int i = 1; i < 1000; i++)
|
---|
33 | {
|
---|
34 | char *p = (char*)internal::aligned_malloc(i);
|
---|
35 | VERIFY(size_t(p)%ALIGNMENT==0);
|
---|
36 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
|
---|
37 | for(int j = 0; j < i; j++) p[j]=0;
|
---|
38 | internal::aligned_free(p);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | void check_aligned_new()
|
---|
43 | {
|
---|
44 | for(int i = 1; i < 1000; i++)
|
---|
45 | {
|
---|
46 | float *p = internal::aligned_new<float>(i);
|
---|
47 | VERIFY(size_t(p)%ALIGNMENT==0);
|
---|
48 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
|
---|
49 | for(int j = 0; j < i; j++) p[j]=0;
|
---|
50 | internal::aligned_delete(p,i);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | void check_aligned_stack_alloc()
|
---|
55 | {
|
---|
56 | for(int i = 1; i < 400; i++)
|
---|
57 | {
|
---|
58 | ei_declare_aligned_stack_constructed_variable(float,p,i,0);
|
---|
59 | VERIFY(size_t(p)%ALIGNMENT==0);
|
---|
60 | // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
|
---|
61 | for(int j = 0; j < i; j++) p[j]=0;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | // test compilation with both a struct and a class...
|
---|
67 | struct MyStruct
|
---|
68 | {
|
---|
69 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW
|
---|
70 | char dummychar;
|
---|
71 | Vector4f avec;
|
---|
72 | };
|
---|
73 |
|
---|
74 | class MyClassA
|
---|
75 | {
|
---|
76 | public:
|
---|
77 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW
|
---|
78 | char dummychar;
|
---|
79 | Vector4f avec;
|
---|
80 | };
|
---|
81 |
|
---|
82 | template<typename T> void check_dynaligned()
|
---|
83 | {
|
---|
84 | T* obj = new T;
|
---|
85 | VERIFY(T::NeedsToAlign==1);
|
---|
86 | VERIFY(size_t(obj)%ALIGNMENT==0);
|
---|
87 | delete obj;
|
---|
88 | }
|
---|
89 |
|
---|
90 | template<typename T> void check_custom_new_delete()
|
---|
91 | {
|
---|
92 | {
|
---|
93 | T* t = new T;
|
---|
94 | delete t;
|
---|
95 | }
|
---|
96 |
|
---|
97 | {
|
---|
98 | std::size_t N = internal::random<std::size_t>(1,10);
|
---|
99 | T* t = new T[N];
|
---|
100 | delete[] t;
|
---|
101 | }
|
---|
102 |
|
---|
103 | #ifdef EIGEN_ALIGN
|
---|
104 | {
|
---|
105 | T* t = static_cast<T *>((T::operator new)(sizeof(T)));
|
---|
106 | (T::operator delete)(t, sizeof(T));
|
---|
107 | }
|
---|
108 |
|
---|
109 | {
|
---|
110 | T* t = static_cast<T *>((T::operator new)(sizeof(T)));
|
---|
111 | (T::operator delete)(t);
|
---|
112 | }
|
---|
113 | #endif
|
---|
114 | }
|
---|
115 |
|
---|
116 | void test_dynalloc()
|
---|
117 | {
|
---|
118 | // low level dynamic memory allocation
|
---|
119 | CALL_SUBTEST(check_handmade_aligned_malloc());
|
---|
120 | CALL_SUBTEST(check_aligned_malloc());
|
---|
121 | CALL_SUBTEST(check_aligned_new());
|
---|
122 | CALL_SUBTEST(check_aligned_stack_alloc());
|
---|
123 |
|
---|
124 | // check static allocation, who knows ?
|
---|
125 | #if EIGEN_ALIGN_STATICALLY
|
---|
126 | for (int i=0; i<g_repeat*100; ++i)
|
---|
127 | {
|
---|
128 | CALL_SUBTEST(check_dynaligned<Vector4f>() );
|
---|
129 | CALL_SUBTEST(check_dynaligned<Vector2d>() );
|
---|
130 | CALL_SUBTEST(check_dynaligned<Matrix4f>() );
|
---|
131 | CALL_SUBTEST(check_dynaligned<Vector4d>() );
|
---|
132 | CALL_SUBTEST(check_dynaligned<Vector4i>() );
|
---|
133 |
|
---|
134 | CALL_SUBTEST( check_custom_new_delete<Vector4f>() );
|
---|
135 | CALL_SUBTEST( check_custom_new_delete<Vector2f>() );
|
---|
136 | CALL_SUBTEST( check_custom_new_delete<Matrix4f>() );
|
---|
137 | CALL_SUBTEST( check_custom_new_delete<MatrixXi>() );
|
---|
138 | }
|
---|
139 |
|
---|
140 | {
|
---|
141 | MyStruct foo0; VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0);
|
---|
142 | MyClassA fooA; VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0);
|
---|
143 | }
|
---|
144 |
|
---|
145 | // dynamic allocation, single object
|
---|
146 | for (int i=0; i<g_repeat*100; ++i)
|
---|
147 | {
|
---|
148 | MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
|
---|
149 | MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
|
---|
150 | delete foo0;
|
---|
151 | delete fooA;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // dynamic allocation, array
|
---|
155 | const int N = 10;
|
---|
156 | for (int i=0; i<g_repeat*100; ++i)
|
---|
157 | {
|
---|
158 | MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
|
---|
159 | MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
|
---|
160 | delete[] foo0;
|
---|
161 | delete[] fooA;
|
---|
162 | }
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | }
|
---|