Skip to content

Commit c2da564

Browse files
committed
core: add aligned_malloc
1 parent 4bf12f9 commit c2da564

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/eigenpy/alignment.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ struct aligned_instance {
2929
typename aligned_storage<sizeof(Data)>::type storage;
3030
};
3131

32+
inline void *aligned_malloc(
33+
std::size_t size, std::size_t alignment = EIGENPY_DEFAULT_ALIGN_BYTES) {
34+
void *original = std::malloc(size + alignment);
35+
if (original == 0) return 0;
36+
void *aligned =
37+
reinterpret_cast<void *>((reinterpret_cast<std::size_t>(original) &
38+
~(std::size_t(alignment - 1))) +
39+
alignment);
40+
*(reinterpret_cast<void **>(aligned) - 1) = original;
41+
return aligned;
42+
}
43+
3244
} // namespace eigenpy
3345

3446
namespace boost {

0 commit comments

Comments
 (0)