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-2017, 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 #include <mrpt/system/memory.h>
15 
16 namespace mrpt
17 {
18 /** Aligned allocator that is compatible with C++11
19  * See: https://bitbucket.org/eigen/eigen/commits/f5b7700
20  */
21 template <class T, size_t AligmentBytes = 16>
22 class aligned_allocator_cpp11 : public std::allocator<T>
23 {
24  public:
25  typedef std::size_t size_type;
27  typedef T* pointer;
28  typedef const T* const_pointer;
29  typedef T& reference;
30  typedef const T& const_reference;
31  typedef T value_type;
32 
33  template <class U>
34  struct rebind
35  {
37  };
38 
39  aligned_allocator_cpp11() : std::allocator<T>() {}
41  : std::allocator<T>(other)
42  {
43  }
44 
45  template <class U>
47  : std::allocator<T>(other)
48  {
49  }
50 
52  pointer allocate(size_type num, const void* /*hint*/ = nullptr)
53  {
54  return static_cast<pointer>(
55  mrpt::system::os::aligned_malloc(num * sizeof(T), AligmentBytes));
56  }
57 
58  void deallocate(pointer p, size_type /*num*/)
59  {
61  }
62 };
63 
64 /** Creates a `shared_ptr` with 16-byte aligned memory.
65  * \ingroup mrpt_base_grp
66  */
67 template <typename T, class... Args>
68 std::shared_ptr<T> make_aligned_shared(Args&&... args)
69 {
70  typedef typename std::remove_const<T>::type T_nc;
71  return std::allocate_shared<T>(
72  mrpt::aligned_allocator_cpp11<T_nc>(), std::forward<Args>(args)...);
73 }
74 }
aligned_allocator_cpp11< U > other
pointer allocate(size_type num, const void *=nullptr)
STL namespace.
aligned_allocator_cpp11(const aligned_allocator_cpp11< U > &other)
GLuint GLuint num
Definition: glext.h:7278
Aligned allocator that is compatible with C++11 See: https://bitbucket.org/eigen/eigen/commits/f5b770...
_W64 int ptrdiff_t
Definition: glew.h:137
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void * aligned_malloc(size_t bytes, size_t alignment)
Returns an aligned memory block.
Definition: memory.cpp:40
std::shared_ptr< T > make_aligned_shared(Args &&... args)
Creates a shared_ptr with 16-byte aligned memory.
aligned_allocator_cpp11(const aligned_allocator_cpp11 &other)
void aligned_free(void *p)
Frees a memory block reserved by aligned_malloc.
Definition: memory.cpp:87
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: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019