blob: f1cc70beeb154f6cd2dccbdfb7be0c6b00ce1434 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// 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
Austin Schuh189376f2018-12-20 22:11:15 +110012#if EIGEN_MAX_ALIGN_BYTES>0
13#define ALIGNMENT EIGEN_MAX_ALIGN_BYTES
Brian Silverman72890c22015-09-19 14:37:37 -040014#else
15#define ALIGNMENT 1
16#endif
17
Austin Schuh189376f2018-12-20 22:11:15 +110018typedef Matrix<float,8,1> Vector8f;
19
Brian Silverman72890c22015-09-19 14:37:37 -040020void check_handmade_aligned_malloc()
21{
22 for(int i = 1; i < 1000; i++)
23 {
24 char *p = (char*)internal::handmade_aligned_malloc(i);
Austin Schuh189376f2018-12-20 22:11:15 +110025 VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
Brian Silverman72890c22015-09-19 14:37:37 -040026 // 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);
29 }
30}
31
32void check_aligned_malloc()
33{
Austin Schuh189376f2018-12-20 22:11:15 +110034 for(int i = ALIGNMENT; i < 1000; i++)
Brian Silverman72890c22015-09-19 14:37:37 -040035 {
36 char *p = (char*)internal::aligned_malloc(i);
Austin Schuh189376f2018-12-20 22:11:15 +110037 VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
Brian Silverman72890c22015-09-19 14:37:37 -040038 // 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);
41 }
42}
43
44void check_aligned_new()
45{
Austin Schuh189376f2018-12-20 22:11:15 +110046 for(int i = ALIGNMENT; i < 1000; i++)
Brian Silverman72890c22015-09-19 14:37:37 -040047 {
48 float *p = internal::aligned_new<float>(i);
Austin Schuh189376f2018-12-20 22:11:15 +110049 VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
Brian Silverman72890c22015-09-19 14:37:37 -040050 // 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);
53 }
54}
55
56void check_aligned_stack_alloc()
57{
Austin Schuh189376f2018-12-20 22:11:15 +110058 for(int i = ALIGNMENT; i < 400; i++)
Brian Silverman72890c22015-09-19 14:37:37 -040059 {
60 ei_declare_aligned_stack_constructed_variable(float,p,i,0);
Austin Schuh189376f2018-12-20 22:11:15 +110061 VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
Brian Silverman72890c22015-09-19 14:37:37 -040062 // 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;
64 }
65}
66
67
68// test compilation with both a struct and a class...
69struct MyStruct
70{
71 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
72 char dummychar;
Austin Schuh189376f2018-12-20 22:11:15 +110073 Vector8f avec;
Brian Silverman72890c22015-09-19 14:37:37 -040074};
75
76class MyClassA
77{
78 public:
79 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
80 char dummychar;
Austin Schuh189376f2018-12-20 22:11:15 +110081 Vector8f avec;
Brian Silverman72890c22015-09-19 14:37:37 -040082};
83
84template<typename T> void check_dynaligned()
85{
Austin Schuh189376f2018-12-20 22:11:15 +110086 // TODO have to be updated once we support multiple alignment values
87 if(T::SizeAtCompileTime % ALIGNMENT == 0)
88 {
89 T* obj = new T;
90 VERIFY(T::NeedsToAlign==1);
91 VERIFY(internal::UIntPtr(obj)%ALIGNMENT==0);
92 delete obj;
93 }
94}
95
96template<typename T> void check_custom_new_delete()
97{
98 {
99 T* t = new T;
100 delete t;
101 }
102
103 {
104 std::size_t N = internal::random<std::size_t>(1,10);
105 T* t = new T[N];
106 delete[] t;
107 }
108
109#if EIGEN_MAX_ALIGN_BYTES>0
110 {
111 T* t = static_cast<T *>((T::operator new)(sizeof(T)));
112 (T::operator delete)(t, sizeof(T));
113 }
114
115 {
116 T* t = static_cast<T *>((T::operator new)(sizeof(T)));
117 (T::operator delete)(t);
118 }
119#endif
Brian Silverman72890c22015-09-19 14:37:37 -0400120}
121
122void test_dynalloc()
123{
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());
129
130 for (int i=0; i<g_repeat*100; ++i)
131 {
Austin Schuh189376f2018-12-20 22:11:15 +1100132 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>() );
136 }
137
138 // check static allocation, who knows ?
139 #if EIGEN_MAX_STATIC_ALIGN_BYTES
140 for (int i=0; i<g_repeat*100; ++i)
141 {
Brian Silverman72890c22015-09-19 14:37:37 -0400142 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>() );
Austin Schuh189376f2018-12-20 22:11:15 +1100147 CALL_SUBTEST(check_dynaligned<Vector8f>() );
Brian Silverman72890c22015-09-19 14:37:37 -0400148 }
Austin Schuh189376f2018-12-20 22:11:15 +1100149
Brian Silverman72890c22015-09-19 14:37:37 -0400150 {
Austin Schuh189376f2018-12-20 22:11:15 +1100151 MyStruct foo0; VERIFY(internal::UIntPtr(foo0.avec.data())%ALIGNMENT==0);
152 MyClassA fooA; VERIFY(internal::UIntPtr(fooA.avec.data())%ALIGNMENT==0);
Brian Silverman72890c22015-09-19 14:37:37 -0400153 }
154
155 // dynamic allocation, single object
156 for (int i=0; i<g_repeat*100; ++i)
157 {
Austin Schuh189376f2018-12-20 22:11:15 +1100158 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);
Brian Silverman72890c22015-09-19 14:37:37 -0400160 delete foo0;
161 delete fooA;
162 }
163
164 // dynamic allocation, array
165 const int N = 10;
166 for (int i=0; i<g_repeat*100; ++i)
167 {
Austin Schuh189376f2018-12-20 22:11:15 +1100168 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);
Brian Silverman72890c22015-09-19 14:37:37 -0400170 delete[] foo0;
171 delete[] fooA;
172 }
173 #endif
174
175}