dynalloc.cpp File Reference
#include "main.h"

Classes

struct  MyStruct
 
class  MyClassA
 

Macros

#define ALIGNMENT   1
 

Typedefs

typedef Matrix< float, 16, 1 > Vector16f
 
typedef Matrix< float, 8, 1 > Vector8f
 

Functions

void check_handmade_aligned_malloc ()
 
void check_aligned_malloc ()
 
void check_aligned_new ()
 
void check_aligned_stack_alloc ()
 
template<typename T >
void check_dynaligned ()
 
template<typename T >
void check_custom_new_delete ()
 
 EIGEN_DECLARE_TEST (dynalloc)
 

Macro Definition Documentation

◆ ALIGNMENT

#define ALIGNMENT   1

Typedef Documentation

◆ Vector16f

typedef Matrix<float, 16, 1> Vector16f

◆ Vector8f

typedef Matrix<float, 8, 1> Vector8f

Function Documentation

◆ check_aligned_malloc()

void check_aligned_malloc ( )
34  {
35  for (int i = ALIGNMENT; i < 1000; i++) {
36  char *p = (char *)internal::aligned_malloc(i);
37  VERIFY(std::uintptr_t(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;
41  }
42 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
float * p
Definition: Tutorial_Map_using.cpp:9
#define ALIGNMENT
Definition: dynalloc.cpp:15
#define VERIFY(a)
Definition: main.h:362
EIGEN_DEVICE_FUNC void * aligned_malloc(std::size_t size)
Definition: Memory.h:199
EIGEN_DEVICE_FUNC void aligned_free(void *ptr)
Definition: Memory.h:224
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::internal::aligned_free(), Eigen::internal::aligned_malloc(), ALIGNMENT, i, j, p, and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ check_aligned_new()

void check_aligned_new ( )
44  {
45  for (int i = ALIGNMENT; i < 1000; i++) {
46  float *p = internal::aligned_new<float>(i);
47  VERIFY(std::uintptr_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;
51  }
52 }
EIGEN_DEVICE_FUNC void aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:430

References Eigen::internal::aligned_delete(), ALIGNMENT, i, j, p, and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ check_aligned_stack_alloc()

void check_aligned_stack_alloc ( )
54  {
55  for (int i = ALIGNMENT; i < 400; i++) {
57  VERIFY(std::uintptr_t(p) % ALIGNMENT == 0);
58  // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
59  for (int j = 0; j < i; j++) p[j] = 0;
60  }
61 }
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
Definition: Memory.h:806

References ALIGNMENT, ei_declare_aligned_stack_constructed_variable, i, j, p, and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ check_custom_new_delete()

template<typename T >
void check_custom_new_delete ( )
89  {
90  {
91  T *t = new T;
92  delete t;
93  }
94 
95  {
96  std::size_t N = internal::random<std::size_t>(1, 10);
97  T *t = new T[N];
98  delete[] t;
99  }
100 
101 #if EIGEN_MAX_ALIGN_BYTES > 0 && (!EIGEN_HAS_CXX17_OVERALIGN)
102  {
103  T *t = static_cast<T *>((T::operator new)(sizeof(T)));
104  (T::operator delete)(t, sizeof(T));
105  }
106 
107  {
108  T *t = static_cast<T *>((T::operator new)(sizeof(T)));
109  (T::operator delete)(t);
110  }
111 #endif
112 }
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
@ N
Definition: constructor.cpp:22
t
Definition: plotPSD.py:36

References N, and plotPSD::t.

◆ check_dynaligned()

template<typename T >
void check_dynaligned ( )
78  {
79  // TODO have to be updated once we support multiple alignment values
80  if (T::SizeAtCompileTime % ALIGNMENT == 0) {
81  T *obj = new T;
82  VERIFY(T::NeedsToAlign == 1);
83  VERIFY(std::uintptr_t(obj) % ALIGNMENT == 0);
84  delete obj;
85  }
86 }

References ALIGNMENT, and VERIFY.

◆ check_handmade_aligned_malloc()

void check_handmade_aligned_malloc ( )
21  {
22  // Hand-make alignment needs at least sizeof(void*) to store the offset.
23  constexpr int alignment = (std::max<int>)(EIGEN_DEFAULT_ALIGN_BYTES, sizeof(void *));
24 
25  for (int i = 1; i < 1000; i++) {
26  char *p = (char *)internal::handmade_aligned_malloc(i, alignment);
27  VERIFY(std::uintptr_t(p) % ALIGNMENT == 0);
28  // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
29  for (int j = 0; j < i; j++) p[j] = 0;
31  }
32 }
#define EIGEN_DEFAULT_ALIGN_BYTES
Definition: ConfigureVectorization.h:169
EIGEN_DEVICE_FUNC void handmade_aligned_free(void *ptr)
Definition: Memory.h:158
EIGEN_DEVICE_FUNC void * handmade_aligned_malloc(std::size_t size, std::size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition: Memory.h:142

References ALIGNMENT, EIGEN_DEFAULT_ALIGN_BYTES, Eigen::internal::handmade_aligned_free(), Eigen::internal::handmade_aligned_malloc(), i, j, p, and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( dynalloc  )
114  {
115  // low level dynamic memory allocation
120 
121  for (int i = 0; i < g_repeat * 100; ++i) {
122  CALL_SUBTEST(check_custom_new_delete<Vector4f>());
123  CALL_SUBTEST(check_custom_new_delete<Vector2f>());
124  CALL_SUBTEST(check_custom_new_delete<Matrix4f>());
125  CALL_SUBTEST(check_custom_new_delete<MatrixXi>());
126  }
127 
128 // check static allocation, who knows ?
129 #if EIGEN_MAX_STATIC_ALIGN_BYTES
130  for (int i = 0; i < g_repeat * 100; ++i) {
131  CALL_SUBTEST(check_dynaligned<Vector4f>());
132  CALL_SUBTEST(check_dynaligned<Vector2d>());
133  CALL_SUBTEST(check_dynaligned<Matrix4f>());
134  CALL_SUBTEST(check_dynaligned<Vector4d>());
135  CALL_SUBTEST(check_dynaligned<Vector4i>());
136  CALL_SUBTEST(check_dynaligned<Vector8f>());
137  CALL_SUBTEST(check_dynaligned<Vector16f>());
138  }
139 
140  {
141  MyStruct foo0;
142  VERIFY(std::uintptr_t(foo0.avec.data()) % ALIGNMENT == 0);
143  MyClassA fooA;
144  VERIFY(std::uintptr_t(fooA.avec.data()) % ALIGNMENT == 0);
145  }
146 
147  // dynamic allocation, single object
148  for (int i = 0; i < g_repeat * 100; ++i) {
149  MyStruct *foo0 = new MyStruct();
150  VERIFY(std::uintptr_t(foo0->avec.data()) % ALIGNMENT == 0);
151  MyClassA *fooA = new MyClassA();
152  VERIFY(std::uintptr_t(fooA->avec.data()) % ALIGNMENT == 0);
153  delete foo0;
154  delete fooA;
155  }
156 
157  // dynamic allocation, array
158  const int N = 10;
159  for (int i = 0; i < g_repeat * 100; ++i) {
160  MyStruct *foo0 = new MyStruct[N];
161  VERIFY(std::uintptr_t(foo0->avec.data()) % ALIGNMENT == 0);
162  MyClassA *fooA = new MyClassA[N];
163  VERIFY(std::uintptr_t(fooA->avec.data()) % ALIGNMENT == 0);
164  delete[] foo0;
165  delete[] fooA;
166  }
167 #endif
168 }
constexpr EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: PlainObjectBase.h:273
Definition: dynalloc.cpp:70
Vector16f avec
Definition: dynalloc.cpp:74
void check_aligned_new()
Definition: dynalloc.cpp:44
void check_aligned_stack_alloc()
Definition: dynalloc.cpp:54
void check_aligned_malloc()
Definition: dynalloc.cpp:34
void check_handmade_aligned_malloc()
Definition: dynalloc.cpp:21
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
static int g_repeat
Definition: main.h:191
Definition: dynalloc.cpp:64
Vector16f avec
Definition: dynalloc.cpp:67

References ALIGNMENT, MyStruct::avec, MyClassA::avec, CALL_SUBTEST, check_aligned_malloc(), check_aligned_new(), check_aligned_stack_alloc(), check_handmade_aligned_malloc(), Eigen::PlainObjectBase< Derived >::data(), Eigen::g_repeat, i, N, and VERIFY.