MRPT  2.0.3
aligned_allocator.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
13 #include <memory>
14 #include <type_traits>
15 
16 namespace mrpt
17 {
18 void* aligned_malloc(size_t size, size_t alignment);
19 void* aligned_realloc(void* ptr, size_t size, size_t alignment);
20 void aligned_free(void* ptr);
21 /** Identical to aligned_malloc, but it zeroes the reserved memory block. */
22 inline void* aligned_calloc(size_t bytes, size_t alignment);
23 
24 /** Aligned allocator that is compatible with C++11.
25  * Default alignment can be 16 (default), 32 (if __AVX__ is defined) or 64
26  * (if __AVX2__ is defined).
27  * See: https://bitbucket.org/eigen/eigen/commits/f5b7700
28  *
29  * This was used (before May-2019) to provide custom STL aligned containers,
30  * but the new(n,m) addition to C++17 rendered this needless (at last!)
31  * See: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1409
32  *
33  * Anyway, this allocator class is left here just in case it is needed for
34  * something else.
35  */
36 template <class T, size_t AligmentBytes = MRPT_MAX_ALIGN_BYTES>
37 class aligned_allocator_cpp11 : public std::allocator<T>
38 {
39  public:
40  using size_type = std::size_t;
41  using difference_type = std::ptrdiff_t;
42  using pointer = T*;
43  using const_pointer = const T*;
44  using reference = T&;
45  using const_reference = const T&;
46  using value_type = T;
47 
48  template <class U>
49  struct rebind
50  {
52  };
53 
54  aligned_allocator_cpp11() : std::allocator<T>() {}
56  : std::allocator<T>(other)
57  {
58  }
59  template <class U>
61  : std::allocator<T>(other)
62  {
63  }
64  ~aligned_allocator_cpp11() = default;
65  pointer allocate(size_type num, const void* /*hint*/ = nullptr)
66  {
67  return static_cast<pointer>(
68  mrpt::aligned_malloc(num * sizeof(T), AligmentBytes));
69  }
71 };
72 
73 } // namespace mrpt
mrpt::aligned_free
void aligned_free(void *ptr)
Definition: aligned_malloc.cpp:40
mrpt::aligned_allocator_cpp11
Aligned allocator that is compatible with C++11.
Definition: aligned_allocator.h:37
mrpt::aligned_allocator_cpp11::value_type
T value_type
Definition: aligned_allocator.h:46
mrpt::aligned_allocator_cpp11::size_type
std::size_t size_type
Definition: aligned_allocator.h:40
mrpt::aligned_allocator_cpp11::aligned_allocator_cpp11
aligned_allocator_cpp11()
Definition: aligned_allocator.h:54
mrpt::aligned_allocator_cpp11::rebind
Definition: aligned_allocator.h:49
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
mrpt::aligned_malloc
void * aligned_malloc(size_t size, size_t alignment)
Definition: aligned_malloc.cpp:22
mrpt::aligned_allocator_cpp11::const_reference
const T & const_reference
Definition: aligned_allocator.h:45
mrpt::aligned_allocator_cpp11::const_pointer
const T * const_pointer
Definition: aligned_allocator.h:43
mrpt::aligned_realloc
void * aligned_realloc(void *ptr, size_t size, size_t alignment)
Definition: aligned_malloc.cpp:48
mrpt::aligned_allocator_cpp11::difference_type
std::ptrdiff_t difference_type
Definition: aligned_allocator.h:41
mrpt::aligned_allocator_cpp11::aligned_allocator_cpp11
aligned_allocator_cpp11(const aligned_allocator_cpp11< U > &other)
Definition: aligned_allocator.h:60
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:16
mrpt::aligned_allocator_cpp11::allocate
pointer allocate(size_type num, const void *=nullptr)
Definition: aligned_allocator.h:65
mrpt::aligned_allocator_cpp11::aligned_allocator_cpp11
aligned_allocator_cpp11(const aligned_allocator_cpp11 &other)
Definition: aligned_allocator.h:55
mrpt::math::size
size_t size(const MATRIXLIKE &m, const int dim)
Definition: math/include/mrpt/math/bits_math.h:21
mrpt::aligned_allocator_cpp11::~aligned_allocator_cpp11
~aligned_allocator_cpp11()=default
mrpt::aligned_allocator_cpp11::reference
T & reference
Definition: aligned_allocator.h:44
mrpt::aligned_allocator_cpp11::pointer
T * pointer
Definition: aligned_allocator.h:42
alignment_req.h
mrpt::aligned_allocator_cpp11::deallocate
void deallocate(pointer p, size_type)
Definition: aligned_allocator.h:70



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 15 15:49:54 UTC 2020