Point Cloud Library (PCL)  1.14.1-dev
memory.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2019-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #pragma once
38 
39 /**
40  * \file pcl/memory.h
41  *
42  * \brief Defines functions, macros and traits for allocating and using memory.
43  * \ingroup common
44  */
45 
46 #include <pcl/type_traits.h> // for has_custom_allocator
47 #include <pcl/pcl_config.h> // for PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
48 
49 #include <Eigen/Core> // for EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 
51 #include <memory> // for std::allocate_shared, std::dynamic_pointer_cast, std::make_shared, std::shared_ptr, std::static_pointer_cast, std::weak_ptr
52 #include <type_traits> // for std::enable_if_t, std::false_type, std::true_type
53 #include <utility> // for std::forward
54 
55 #if !defined(PCL_SILENCE_MALLOC_WARNING)
56 #if PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
57 // EIGEN_DEFAULT_ALIGN_BYTES and EIGEN_MALLOC_ALREADY_ALIGNED will be set after including Eigen/Core
58 // this condition is the same as in the function aligned_malloc in Memory.h in the Eigen code
59 #if (defined(EIGEN_DEFAULT_ALIGN_BYTES) && EIGEN_DEFAULT_ALIGN_BYTES==0) || (defined(EIGEN_MALLOC_ALREADY_ALIGNED) && EIGEN_MALLOC_ALREADY_ALIGNED)
60 #if defined(_MSC_VER)
61 #error "Potential runtime error due to aligned malloc mismatch! You likely have to compile your code with AVX enabled or define EIGEN_MAX_ALIGN_BYTES=32 (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
62 #else // defined(_MSC_VER)
63 #warning "Potential runtime error due to aligned malloc mismatch! You likely have to compile your code with AVX enabled or define EIGEN_MAX_ALIGN_BYTES=32 (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
64 #endif // defined(_MSC_VER)
65 #endif
66 #else // PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
67 #if (defined(EIGEN_DEFAULT_ALIGN_BYTES) && EIGEN_DEFAULT_ALIGN_BYTES!=0) && (defined(EIGEN_MALLOC_ALREADY_ALIGNED) && !EIGEN_MALLOC_ALREADY_ALIGNED)
68 #if defined(_MSC_VER)
69 #error "Potential runtime error due to aligned malloc mismatch! PCL was likely compiled without AVX support but you enabled AVX for your code (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
70 #else // defined(_MSC_VER)
71 #warning "Potential runtime error due to aligned malloc mismatch! PCL was likely compiled without AVX support but you enabled AVX for your code (to silence this message at your own risk, define PCL_SILENCE_MALLOC_WARNING=1)"
72 #endif // defined(_MSC_VER)
73 #endif
74 #endif // PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC
75 #endif // !defined(PCL_SILENCE_MALLOC_WARNING)
76 
77 /**
78  * \brief Macro to signal a class requires a custom allocator
79  *
80  * It's an implementation detail to have pcl::has_custom_allocator work, a
81  * thin wrapper over Eigen's own macro
82  *
83  * \see pcl::has_custom_allocator, pcl::make_shared
84  * \ingroup common
85  */
86 #define PCL_MAKE_ALIGNED_OPERATOR_NEW \
87  EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
88  using _custom_allocator_type_trait = void;
89 
90 
91 namespace pcl
92 {
93 /**
94  * \brief Force ADL for `shared_ptr`
95  *
96  * For ease of switching from boost::shared_ptr to std::shared_ptr
97  *
98  * \see pcl::make_shared
99  */
100 using std::shared_ptr;
101 
102 /**
103  * \brief Force ADL for `weak_ptr`
104  *
105  * For ease of switching from boost::weak_ptr to std::weak_ptr
106  */
107 using std::weak_ptr;
108 
109 /** ADL doesn't work until C++20 for dynamic_pointer_cast since it requires an explicit Tparam */
110 using std::dynamic_pointer_cast;
111 
112 /** ADL doesn't work until C++20 for static_pointer_cast since it requires an explicit Tparam */
113 using std::static_pointer_cast;
114 
115 #ifdef DOXYGEN_ONLY
116 
117 /**
118  * \brief Returns a pcl::shared_ptr compliant with type T's allocation policy.
119  *
120  * std::allocate_shared or std::make_shared will be invoked in case T has or
121  * doesn't have a custom allocator, respectively.
122  *
123  * \note In MSVC < 1915 (before version 15.8) alignment was incorrectly set at
124  * most at alignof(max_align_t). This bug was fixed in said version and is
125  * acknowledged by defining _ENABLE_EXTENDED_ALIGNED_STORAGE. See #3752.
126  *
127  * \see pcl::has_custom_allocator, PCL_MAKE_ALIGNED_OPERATOR_NEW
128  * \tparam T Type of the object to create a pcl::shared_ptr of
129  * \tparam Args Types for the arguments to pcl::make_shared
130  * \param args List of arguments with which an instance of T will be constructed
131  * \return pcl::shared_ptr of an instance of type T
132  */
133 template<typename T, typename ... Args>
134 shared_ptr<T> make_shared(Args&&... args);
135 
136 #else
137 
138 template<typename T, typename ... Args>
139 std::enable_if_t<has_custom_allocator<T>::value, shared_ptr<T>> make_shared(Args&&... args)
140 {
141  return std::allocate_shared<T>(Eigen::aligned_allocator<T>(), std::forward<Args> (args)...);
142 }
143 
144 template<typename T, typename ... Args>
145 std::enable_if_t<!has_custom_allocator<T>::value, shared_ptr<T>> make_shared(Args&&... args)
146 {
147  return std::make_shared<T>(std::forward<Args> (args)...);
148 }
149 
150 #endif
151 }
shared_ptr< T > make_shared(Args &&... args)
Returns a pcl::shared_ptr compliant with type T's allocation policy.