Main MRPT website > C++ reference for MRPT 1.9.9
aligned_allocator.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <type_traits>
13 #include <memory>
14 
15 // This is to match Eigen expectations on alignment of dynamic objects:
16 #if defined(__AVX2__) || defined(EIGEN_VECTORIZE_AVX512)
17 #define MRPT_MAX_ALIGN_BYTES 64
18 #elif defined(__AVX__)
19 #define MRPT_MAX_ALIGN_BYTES 32
20 #else
21 #define MRPT_MAX_ALIGN_BYTES 16
22 #endif
23 
24 namespace mrpt
25 {
26 void* aligned_malloc(size_t size, size_t alignment);
27 void* aligned_realloc(void* ptr, size_t size, size_t alignment);
28 void aligned_free(void* ptr);
29 /** Identical to aligned_malloc, but it zeroes the reserved memory block. */
30 inline void* aligned_calloc(size_t bytes, size_t alignment);
31 
32 /** Aligned allocator that is compatible with C++11.
33  * Default alignment can be 16 (default), 32 (if __AVX__ is defined) or 64
34  * (if __AVX2__ is defined).
35  * See also: https://bitbucket.org/eigen/eigen/commits/f5b7700
36  */
37 template <class T, size_t AligmentBytes = MRPT_MAX_ALIGN_BYTES>
38 class aligned_allocator_cpp11 : public std::allocator<T>
39 {
40  public:
41  using size_type = std::size_t;
43  using pointer = T*;
44  using const_pointer = const T*;
45  using reference = T&;
46  using const_reference = const T&;
47  using value_type = T;
48 
49  template <class U>
50  struct rebind
51  {
53  };
54 
55  aligned_allocator_cpp11() : std::allocator<T>() {}
57  : std::allocator<T>(other)
58  {
59  }
60  template <class U>
62  : std::allocator<T>(other)
63  {
64  }
66  pointer allocate(size_type num, const void* /*hint*/ = nullptr)
67  {
68  return static_cast<pointer>(
69  mrpt::aligned_malloc(num * sizeof(T), AligmentBytes));
70  }
72 };
73 
74 /** Creates a `shared_ptr` with aligned memory via aligned_allocator_cpp11<>.
75  * \ingroup mrpt_base_grp
76  */
77 template <typename T, class... Args>
78 std::shared_ptr<T> make_aligned_shared(Args&&... args)
79 {
80  using T_nc = typename std::remove_const<T>::type;
81  return std::allocate_shared<T>(
82  mrpt::aligned_allocator_cpp11<T_nc>(), std::forward<Args>(args)...);
83 }
84 
85 /** Put this macro inside any class with members that require {16,32,64}-byte
86  * memory alignment (e.g. Eigen matrices), to override default new()/delete().
87  * Obviously, this macro is only *required* if the class is to be instantiated
88  * in dynamic memory.
89  */
90 #define MRPT_MAKE_ALIGNED_OPERATOR_NEW \
91  void* operator new(size_t size) \
92  { \
93  return mrpt::aligned_malloc(size, MRPT_MAX_ALIGN_BYTES); \
94  } \
95  void* operator new[](size_t size) \
96  { \
97  return mrpt::aligned_malloc(size, MRPT_MAX_ALIGN_BYTES); \
98  } \
99  void operator delete(void* ptr) noexcept { mrpt::aligned_free(ptr); } \
100  void operator delete[](void* ptr) noexcept { mrpt::aligned_free(ptr); } \
101  /* in-place new and delete. since (at least afaik) there is no actual */ \
102  /* memory allocated we can safely let the default implementation handle */ \
103  /* this particular case. */ \
104  static void* operator new(size_t size, void* ptr) \
105  { \
106  return ::operator new(size, ptr); \
107  } \
108  void operator delete(void* memory, void* ptr) noexcept \
109  { \
110  return ::operator delete(memory, ptr); \
111  } \
112  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
113  void* operator new(size_t size, const std::nothrow_t&) noexcept \
114  { \
115  try \
116  { \
117  return mrpt::aligned_malloc(size, MRPT_MAX_ALIGN_BYTES); \
118  } \
119  catch (...) \
120  { \
121  return nullptr; \
122  } \
123  } \
124  void operator delete(void* ptr, const std::nothrow_t&)noexcept \
125  { \
126  mrpt::aligned_free(ptr); \
127  }
128 } // namespace mrpt
mrpt::aligned_free
void aligned_free(void *ptr)
Definition: aligned_malloc.cpp:31
mrpt::aligned_allocator_cpp11
Aligned allocator that is compatible with C++11.
Definition: aligned_allocator.h:38
mrpt::aligned_allocator_cpp11::value_type
T value_type
Definition: aligned_allocator.h:47
mrpt::aligned_allocator_cpp11::size_type
std::size_t size_type
Definition: aligned_allocator.h:41
mrpt::aligned_allocator_cpp11::aligned_allocator_cpp11
aligned_allocator_cpp11()
Definition: aligned_allocator.h:55
mrpt::make_aligned_shared
std::shared_ptr< T > make_aligned_shared(Args &&... args)
Creates a shared_ptr with aligned memory via aligned_allocator_cpp11<>.
Definition: aligned_allocator.h:78
mrpt::aligned_allocator_cpp11::rebind
Definition: aligned_allocator.h:50
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::aligned_malloc
void * aligned_malloc(size_t size, size_t alignment)
Definition: aligned_malloc.cpp:21
type
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::aligned_allocator_cpp11::const_reference
const T & const_reference
Definition: aligned_allocator.h:46
mrpt::aligned_allocator_cpp11::const_pointer
const T * const_pointer
Definition: aligned_allocator.h:44
mrpt::aligned_realloc
void * aligned_realloc(void *ptr, size_t size, size_t alignment)
Definition: aligned_malloc.cpp:39
mrpt::aligned_allocator_cpp11::difference_type
std::ptrdiff_t difference_type
Definition: aligned_allocator.h:42
mrpt::aligned_allocator_cpp11::aligned_allocator_cpp11
aligned_allocator_cpp11(const aligned_allocator_cpp11< U > &other)
Definition: aligned_allocator.h:61
mrpt::aligned_calloc
void * aligned_calloc(size_t bytes, size_t alignment)
Identical to aligned_malloc, but it zeroes the reserved memory block.
Definition: aligned_malloc.cpp:15
mrpt::aligned_allocator_cpp11::allocate
pointer allocate(size_type num, const void *=nullptr)
Definition: aligned_allocator.h:66
mrpt::aligned_allocator_cpp11::aligned_allocator_cpp11
aligned_allocator_cpp11(const aligned_allocator_cpp11 &other)
Definition: aligned_allocator.h:56
mrpt::aligned_allocator_cpp11::~aligned_allocator_cpp11
~aligned_allocator_cpp11()
Definition: aligned_allocator.h:65
mrpt::aligned_allocator_cpp11::reference
T & reference
Definition: aligned_allocator.h:45
mrpt::aligned_allocator_cpp11::pointer
T * pointer
Definition: aligned_allocator.h:43
ptrdiff_t
_W64 int ptrdiff_t
Definition: glew.h:137
size
GLsizeiptr size
Definition: glext.h:3923
mrpt::aligned_allocator_cpp11::deallocate
void deallocate(pointer p, size_type)
Definition: aligned_allocator.h:71
num
GLuint GLuint num
Definition: glext.h:7278



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST