dynalloc.cpp
Go to the documentation of this file.
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_MAX_ALIGN_BYTES>0
13 #define ALIGNMENT EIGEN_MAX_ALIGN_BYTES
14 #else
15 #define ALIGNMENT 1
16 #endif
17 
20 
22 {
23  for(int i = 1; i < 1000; i++)
24  {
25  char *p = (char*)internal::handmade_aligned_malloc(i);
27  // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
28  for(int j = 0; j < i; j++) p[j]=0;
30  }
31 }
32 
34 {
35  for(int i = ALIGNMENT; i < 1000; i++)
36  {
37  char *p = (char*)internal::aligned_malloc(i);
39  // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
40  for(int j = 0; j < i; j++) p[j]=0;
42  }
43 }
44 
46 {
47  for(int i = ALIGNMENT; i < 1000; i++)
48  {
49  float *p = internal::aligned_new<float>(i);
51  // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
52  for(int j = 0; j < i; j++) p[j]=0;
54  }
55 }
56 
58 {
59  for(int i = ALIGNMENT; i < 400; i++)
60  {
63  // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
64  for(int j = 0; j < i; j++) p[j]=0;
65  }
66 }
67 
68 
69 // test compilation with both a struct and a class...
70 struct MyStruct
71 {
73  char dummychar;
75 };
76 
77 class MyClassA
78 {
79  public:
81  char dummychar;
83 };
84 
85 template<typename T> void check_dynaligned()
86 {
87  // TODO have to be updated once we support multiple alignment values
88  if(T::SizeAtCompileTime % ALIGNMENT == 0)
89  {
90  T* obj = new T;
91  VERIFY(T::NeedsToAlign==1);
93  delete obj;
94  }
95 }
96 
97 template<typename T> void check_custom_new_delete()
98 {
99  {
100  T* t = new T;
101  delete t;
102  }
103 
104  {
105  std::size_t N = internal::random<std::size_t>(1,10);
106  T* t = new T[N];
107  delete[] t;
108  }
109 
110 #if EIGEN_MAX_ALIGN_BYTES>0 && (!EIGEN_HAS_CXX17_OVERALIGN)
111  {
112  T* t = static_cast<T *>((T::operator new)(sizeof(T)));
113  (T::operator delete)(t, sizeof(T));
114  }
115 
116  {
117  T* t = static_cast<T *>((T::operator new)(sizeof(T)));
118  (T::operator delete)(t);
119  }
120 #endif
121 }
122 
124 {
125  // low level dynamic memory allocation
130 
131  for (int i=0; i<g_repeat*100; ++i)
132  {
133  CALL_SUBTEST( check_custom_new_delete<Vector4f>() );
134  CALL_SUBTEST( check_custom_new_delete<Vector2f>() );
135  CALL_SUBTEST( check_custom_new_delete<Matrix4f>() );
136  CALL_SUBTEST( check_custom_new_delete<MatrixXi>() );
137  }
138 
139  // check static allocation, who knows ?
140  #if EIGEN_MAX_STATIC_ALIGN_BYTES
141  for (int i=0; i<g_repeat*100; ++i)
142  {
143  CALL_SUBTEST(check_dynaligned<Vector4f>() );
144  CALL_SUBTEST(check_dynaligned<Vector2d>() );
145  CALL_SUBTEST(check_dynaligned<Matrix4f>() );
146  CALL_SUBTEST(check_dynaligned<Vector4d>() );
147  CALL_SUBTEST(check_dynaligned<Vector4i>() );
148  CALL_SUBTEST(check_dynaligned<Vector8f>() );
149  CALL_SUBTEST(check_dynaligned<Vector16f>() );
150  }
151 
152  {
155  }
156 
157  // dynamic allocation, single object
158  for (int i=0; i<g_repeat*100; ++i)
159  {
160  MyStruct *foo0 = new MyStruct(); VERIFY(internal::UIntPtr(foo0->avec.data())%ALIGNMENT==0);
161  MyClassA *fooA = new MyClassA(); VERIFY(internal::UIntPtr(fooA->avec.data())%ALIGNMENT==0);
162  delete foo0;
163  delete fooA;
164  }
165 
166  // dynamic allocation, array
167  const int N = 10;
168  for (int i=0; i<g_repeat*100; ++i)
169  {
170  MyStruct *foo0 = new MyStruct[N]; VERIFY(internal::UIntPtr(foo0->avec.data())%ALIGNMENT==0);
171  MyClassA *fooA = new MyClassA[N]; VERIFY(internal::UIntPtr(fooA->avec.data())%ALIGNMENT==0);
172  delete[] foo0;
173  delete[] fooA;
174  }
175  #endif
176 
177 }
Eigen::internal::UIntPtr
std::size_t UIntPtr
Definition: Meta.h:92
check_aligned_malloc
void check_aligned_malloc()
Definition: dynalloc.cpp:33
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
MyStruct
Definition: dynalloc.cpp:70
Eigen::internal::aligned_malloc
EIGEN_DEVICE_FUNC void * aligned_malloc(std::size_t size)
Definition: Memory.h:174
ei_declare_aligned_stack_constructed_variable
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:768
Eigen::internal::handmade_aligned_malloc
EIGEN_DEVICE_FUNC void * handmade_aligned_malloc(std::size_t size, std::size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition: Memory.h:100
check_dynaligned
void check_dynaligned()
Definition: dynalloc.cpp:85
Eigen::internal::aligned_free
EIGEN_DEVICE_FUNC void aligned_free(void *ptr)
Definition: Memory.h:198
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
check_custom_new_delete
void check_custom_new_delete()
Definition: dynalloc.cpp:97
check_aligned_stack_alloc
void check_aligned_stack_alloc()
Definition: dynalloc.cpp:57
Eigen::internal::handmade_aligned_free
EIGEN_DEVICE_FUNC void handmade_aligned_free(void *ptr)
Definition: Memory.h:114
ALIGNMENT
#define ALIGNMENT
Definition: dynalloc.cpp:15
check_handmade_aligned_malloc
void check_handmade_aligned_malloc()
Definition: dynalloc.cpp:21
MyStruct::dummychar
EIGEN_MAKE_ALIGNED_OPERATOR_NEW char dummychar
Definition: dynalloc.cpp:73
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
Eigen::Triplet< double >
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:841
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
Eigen::internal::aligned_delete
EIGEN_DEVICE_FUNC void aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:361
Vector8f
Matrix< float, 8, 1 > Vector8f
Definition: dynalloc.cpp:19
main.h
MyClassA::avec
Vector16f avec
Definition: dynalloc.cpp:82
MyStruct::avec
Vector16f avec
Definition: dynalloc.cpp:74
p
float * p
Definition: Tutorial_Map_using.cpp:9
MyClassA::dummychar
EIGEN_MAKE_ALIGNED_OPERATOR_NEW char dummychar
Definition: dynalloc.cpp:81
Eigen::PlainObjectBase::data
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar * data() const
Definition: PlainObjectBase.h:247
Eigen::Matrix< float, 16, 1 >
Vector16f
Matrix< float, 16, 1 > Vector16f
Definition: dynalloc.cpp:18
N
#define N
Definition: igam.h:9
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(dynalloc)
Definition: dynalloc.cpp:123
align_3::t
Point2 t(10, 10)
check_aligned_new
void check_aligned_new()
Definition: dynalloc.cpp:45
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
VERIFY
#define VERIFY(a)
Definition: main.h:380
MyClassA
Definition: dynalloc.cpp:77


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:29