Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 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/. |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 9 | |
| 10 | #include "main.h" |
| 11 | |
Gael Guennebaud | aec4814 | 2015-07-29 11:11:23 +0200 | [diff] [blame^] | 12 | #if EIGEN_MAX_ALIGN_BYTES>0 |
| 13 | #define ALIGNMENT EIGEN_MAX_ALIGN_BYTES |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 14 | #else |
| 15 | #define ALIGNMENT 1 |
| 16 | #endif |
| 17 | |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 18 | typedef Matrix<float,8,1> Vector8f; |
| 19 | |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 20 | void check_handmade_aligned_malloc() |
| 21 | { |
| 22 | for(int i = 1; i < 1000; i++) |
| 23 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 24 | char *p = (char*)internal::handmade_aligned_malloc(i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 25 | VERIFY(size_t(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 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; |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 28 | internal::handmade_aligned_free(p); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
| 32 | void check_aligned_malloc() |
| 33 | { |
| 34 | for(int i = 1; i < 1000; i++) |
| 35 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 36 | char *p = (char*)internal::aligned_malloc(i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 37 | VERIFY(size_t(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 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; |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 40 | internal::aligned_free(p); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | void check_aligned_new() |
| 45 | { |
| 46 | for(int i = 1; i < 1000; i++) |
| 47 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 48 | float *p = internal::aligned_new<float>(i); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 49 | VERIFY(size_t(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 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; |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 52 | internal::aligned_delete(p,i); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | void check_aligned_stack_alloc() |
| 57 | { |
Gael Guennebaud | e51da9c | 2014-08-04 12:46:00 +0200 | [diff] [blame] | 58 | for(int i = 1; i < 400; i++) |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 59 | { |
Gael Guennebaud | bbb4b35 | 2011-03-19 08:51:38 +0100 | [diff] [blame] | 60 | ei_declare_aligned_stack_constructed_variable(float,p,i,0); |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 61 | VERIFY(size_t(p)%ALIGNMENT==0); |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 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; |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 68 | // test compilation with both a struct and a class... |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 69 | struct MyStruct |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 70 | { |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 71 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 72 | char dummychar; |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 73 | Vector8f avec; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Benoit Jacob | 1d52bd4 | 2009-01-08 15:20:21 +0000 | [diff] [blame] | 76 | class MyClassA |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 77 | { |
| 78 | public: |
Benoit Jacob | eb7dcbb | 2009-01-08 15:37:13 +0000 | [diff] [blame] | 79 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 80 | char dummychar; |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 81 | Vector8f avec; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | template<typename T> void check_dynaligned() |
| 85 | { |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 86 | // 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(size_t(obj)%ALIGNMENT==0); |
| 92 | delete obj; |
| 93 | } |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Gael Guennebaud | ba694ce | 2014-07-30 09:32:35 +0200 | [diff] [blame] | 96 | template<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 | |
Gael Guennebaud | aec4814 | 2015-07-29 11:11:23 +0200 | [diff] [blame^] | 109 | #if EIGEN_MAX_ALIGN_BYTES>0 |
Gael Guennebaud | ba694ce | 2014-07-30 09:32:35 +0200 | [diff] [blame] | 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 |
| 120 | } |
| 121 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 122 | void test_dynalloc() |
| 123 | { |
Benoit Jacob | fd831d5 | 2009-01-09 14:56:44 +0000 | [diff] [blame] | 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()); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 129 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 130 | for (int i=0; i<g_repeat*100; ++i) |
| 131 | { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 132 | CALL_SUBTEST(check_dynaligned<Vector4f>() ); |
| 133 | CALL_SUBTEST(check_dynaligned<Vector2d>() ); |
| 134 | CALL_SUBTEST(check_dynaligned<Matrix4f>() ); |
| 135 | CALL_SUBTEST(check_dynaligned<Vector4d>() ); |
| 136 | CALL_SUBTEST(check_dynaligned<Vector4i>() ); |
Gael Guennebaud | 2606abe | 2014-04-18 21:14:40 +0200 | [diff] [blame] | 137 | CALL_SUBTEST(check_dynaligned<Vector8f>() ); |
Gael Guennebaud | ba694ce | 2014-07-30 09:32:35 +0200 | [diff] [blame] | 138 | |
| 139 | CALL_SUBTEST( check_custom_new_delete<Vector4f>() ); |
| 140 | CALL_SUBTEST( check_custom_new_delete<Vector2f>() ); |
| 141 | CALL_SUBTEST( check_custom_new_delete<Matrix4f>() ); |
| 142 | CALL_SUBTEST( check_custom_new_delete<MatrixXi>() ); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // check static allocation, who knows ? |
Gael Guennebaud | aec4814 | 2015-07-29 11:11:23 +0200 | [diff] [blame^] | 146 | #if EIGEN_MAX_STATIC_ALIGN_BYTES |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 147 | { |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 148 | MyStruct foo0; VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0); |
| 149 | MyClassA fooA; VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 150 | } |
Gael Guennebaud | 2359486 | 2011-03-15 09:53:23 +0100 | [diff] [blame] | 151 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 152 | // dynamic allocation, single object |
| 153 | for (int i=0; i<g_repeat*100; ++i) |
| 154 | { |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 155 | MyStruct *foo0 = new MyStruct(); VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0); |
| 156 | MyClassA *fooA = new MyClassA(); VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 157 | delete foo0; |
| 158 | delete fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // dynamic allocation, array |
| 162 | const int N = 10; |
| 163 | for (int i=0; i<g_repeat*100; ++i) |
| 164 | { |
Benoit Jacob | 93a089a | 2009-02-04 16:53:03 +0000 | [diff] [blame] | 165 | MyStruct *foo0 = new MyStruct[N]; VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0); |
| 166 | MyClassA *fooA = new MyClassA[N]; VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0); |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 167 | delete[] foo0; |
| 168 | delete[] fooA; |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 169 | } |
Gael Guennebaud | 2359486 | 2011-03-15 09:53:23 +0100 | [diff] [blame] | 170 | #endif |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 171 | |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 172 | } |