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>
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
void aligned_free(void *ptr)
pointer allocate(size_type num, const void *=nullptr)
STL namespace.
aligned_allocator_cpp11(const aligned_allocator_cpp11< U > &other)
void * aligned_malloc(size_t size, size_t alignment)
GLuint GLuint num
Definition: glext.h:7278
Aligned allocator that is compatible with C++11.
void * aligned_realloc(void *ptr, size_t size, size_t alignment)
_W64 int ptrdiff_t
Definition: glew.h:137
void * aligned_calloc(size_t bytes, size_t alignment)
Identical to aligned_malloc, but it zeroes the reserved memory block.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::shared_ptr< T > make_aligned_shared(Args &&... args)
Creates a shared_ptr with aligned memory via aligned_allocator_cpp11<>.
aligned_allocator_cpp11(const aligned_allocator_cpp11 &other)
GLsizeiptr size
Definition: glext.h:3923
void deallocate(pointer p, size_type)
GLfloat GLfloat p
Definition: glext.h:6305
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020