1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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/.
12 #if EIGEN_MAX_ALIGN_BYTES>0
13 #define ALIGNMENT EIGEN_MAX_ALIGN_BYTES
18 typedef Matrix
<float,8,1> Vector8f
;
20 void check_handmade_aligned_malloc()
22 for(int i
= 1; i
< 1000; i
++)
24 char *p
= (char*)internal::handmade_aligned_malloc(i
);
25 VERIFY(internal::UIntPtr(p
)%ALIGNMENT
==0);
26 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
27 for(int j
= 0; j
< i
; j
++) p
[j
]=0;
28 internal::handmade_aligned_free(p
);
32 void check_aligned_malloc()
34 for(int i
= ALIGNMENT
; i
< 1000; i
++)
36 char *p
= (char*)internal::aligned_malloc(i
);
37 VERIFY(internal::UIntPtr(p
)%ALIGNMENT
==0);
38 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
39 for(int j
= 0; j
< i
; j
++) p
[j
]=0;
40 internal::aligned_free(p
);
44 void check_aligned_new()
46 for(int i
= ALIGNMENT
; i
< 1000; i
++)
48 float *p
= internal::aligned_new
<float>(i
);
49 VERIFY(internal::UIntPtr(p
)%ALIGNMENT
==0);
50 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
51 for(int j
= 0; j
< i
; j
++) p
[j
]=0;
52 internal::aligned_delete(p
,i
);
56 void check_aligned_stack_alloc()
58 for(int i
= ALIGNMENT
; i
< 400; i
++)
60 ei_declare_aligned_stack_constructed_variable(float,p
,i
,0);
61 VERIFY(internal::UIntPtr(p
)%ALIGNMENT
==0);
62 // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
63 for(int j
= 0; j
< i
; j
++) p
[j
]=0;
68 // test compilation with both a struct and a class...
71 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
79 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
84 template<typename T
> void check_dynaligned()
86 // TODO have to be updated once we support multiple alignment values
87 if(T::SizeAtCompileTime
% ALIGNMENT
== 0)
90 VERIFY(T::NeedsToAlign
==1);
91 VERIFY(internal::UIntPtr(obj
)%ALIGNMENT
==0);
96 template<typename T
> void check_custom_new_delete()
104 std::size_t N
= internal::random
<std::size_t>(1,10);
109 #if EIGEN_MAX_ALIGN_BYTES>0
111 T
* t
= static_cast<T
*>((T::operator new)(sizeof(T
)));
112 (T::operator delete)(t
, sizeof(T
));
116 T
* t
= static_cast<T
*>((T::operator new)(sizeof(T
)));
117 (T::operator delete)(t
);
124 // low level dynamic memory allocation
125 CALL_SUBTEST(check_handmade_aligned_malloc());
126 CALL_SUBTEST(check_aligned_malloc());
127 CALL_SUBTEST(check_aligned_new());
128 CALL_SUBTEST(check_aligned_stack_alloc());
130 for (int i
=0; i
<g_repeat
*100; ++i
)
132 CALL_SUBTEST( check_custom_new_delete
<Vector4f
>() );
133 CALL_SUBTEST( check_custom_new_delete
<Vector2f
>() );
134 CALL_SUBTEST( check_custom_new_delete
<Matrix4f
>() );
135 CALL_SUBTEST( check_custom_new_delete
<MatrixXi
>() );
138 // check static allocation, who knows ?
139 #if EIGEN_MAX_STATIC_ALIGN_BYTES
140 for (int i
=0; i
<g_repeat
*100; ++i
)
142 CALL_SUBTEST(check_dynaligned
<Vector4f
>() );
143 CALL_SUBTEST(check_dynaligned
<Vector2d
>() );
144 CALL_SUBTEST(check_dynaligned
<Matrix4f
>() );
145 CALL_SUBTEST(check_dynaligned
<Vector4d
>() );
146 CALL_SUBTEST(check_dynaligned
<Vector4i
>() );
147 CALL_SUBTEST(check_dynaligned
<Vector8f
>() );
151 MyStruct foo0
; VERIFY(internal::UIntPtr(foo0
.avec
.data())%ALIGNMENT
==0);
152 MyClassA fooA
; VERIFY(internal::UIntPtr(fooA
.avec
.data())%ALIGNMENT
==0);
155 // dynamic allocation, single object
156 for (int i
=0; i
<g_repeat
*100; ++i
)
158 MyStruct
*foo0
= new MyStruct(); VERIFY(internal::UIntPtr(foo0
->avec
.data())%ALIGNMENT
==0);
159 MyClassA
*fooA
= new MyClassA(); VERIFY(internal::UIntPtr(fooA
->avec
.data())%ALIGNMENT
==0);
164 // dynamic allocation, array
166 for (int i
=0; i
<g_repeat
*100; ++i
)
168 MyStruct
*foo0
= new MyStruct
[N
]; VERIFY(internal::UIntPtr(foo0
->avec
.data())%ALIGNMENT
==0);
169 MyClassA
*fooA
= new MyClassA
[N
]; VERIFY(internal::UIntPtr(fooA
->avec
.data())%ALIGNMENT
==0);