Main MRPT website > C++ reference for MRPT 1.9.9
memory.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 #ifndef MRPT_MEMORY_H
10 #define MRPT_MEMORY_H
11 
12 #include <mrpt/utils/core_defs.h>
13 #include <cstring>
14 
15 namespace mrpt
16 {
17 namespace system
18 {
19 /** \addtogroup mrpt_memory Memory utilities (in #include
20  * <mrpt/system/memory.h>)
21  * \ingroup mrpt_base_grp
22  * @{ */
23 
24 /** Returns the memory occupied by this process, in bytes */
25 unsigned long getMemoryUsage();
26 
27 /** In platforms and compilers with support to "alloca", allocate a memory block
28  * on the stack; if alloca is not supported, it is emulated as a normal "malloc"
29  * - NOTICE: Since in some platforms alloca will be emulated with malloc,
30  * alloca_free MUST BE ALWAYS CALLED to avoid memory leaks.
31  * This method MUST BE a macro rather than a function in order to operate on
32  * the caller's stack.
33  * \sa mrpt_alloca_free
34  */
35 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
36 // Visual Studio 2005, 2008
37 #define mrpt_alloca(nBytes) _malloca(nBytes)
38 #elif defined(HAVE_ALLOCA)
39 // GCC
40 #define mrpt_alloca(nBytes) ::alloca(nBytes)
41 #else
42 // Default: Emulate with memory in the heap:
43 #define mrpt_alloca(nBytes) ::malloc(nBytes)
44 #endif
45 
46 /** This method must be called to "free" each memory block allocated with
47  * "system::alloca": If the block was really allocated in the stack, no
48  * operation is actually performed, otherwise it will be freed from the heap.
49  * This method MUST BE a macro rather than a function in order to operate on
50  * the caller's stack.
51  * \sa mrpt_alloca
52  */
53 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
54 // Visual Studio 2005, 2008
55 #define mrpt_alloca_free(mem_block) _freea(mem_block)
56 #elif defined(HAVE_ALLOCA)
57 // GCC
58 #define mrpt_alloca_free(mem_block)
59 #else
60 // Default: Emulate with memory in the heap:
61 #define mrpt_alloca_free(mem_block) free(mem_block)
62 #endif
63 
64 /** @} */
65 
66 namespace os
67 {
68 /** \addtogroup mrpt_memory Memory utilities
69  * @{ */
70 
71 /** Returns an aligned memory block.
72  * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no
73  * alignment required. It must be a power of two.
74  * \sa aligned_free, aligned_realloc, aligned_calloc
75  * \note Based on code by William Chan
76 */
77 void* aligned_malloc(size_t bytes, size_t alignment);
78 
79 /** Identical to aligned_malloc, but it zeroes the reserved memory block. */
80 inline void* aligned_calloc(size_t bytes, size_t alignment)
81 {
82  void* ptr = mrpt::system::os::aligned_malloc(bytes, alignment);
83  if (ptr) ::memset(ptr, 0, bytes);
84  return ptr;
85 }
86 
87 /** Frees a memory block reserved by aligned_malloc.
88  * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no
89  * alignment required.
90  * If old_ptr is nullptr, a new block will be reserved from scratch.
91  * \sa aligned_malloc, aligned_free
92  */
93 void* aligned_realloc(void* old_ptr, size_t bytes, size_t alignment);
94 
95 /** Frees a memory block reserved by aligned_malloc
96  * \sa aligned_malloc
97  */
98 void aligned_free(void* p);
99 
100 /** Returns a pointer a bit forward in memory so it's aligned for the given
101  * boundary size
102  * \note Function copied from OpenCV with a different name to avoid conflicts.
103  */
104 template <typename _Tp>
105 inline _Tp* align_ptr(_Tp* ptr, int n = (int)sizeof(_Tp))
106 {
107  return (_Tp*)(((size_t)ptr + n - 1) & -n);
108 }
109 
110 /** @} */
111 } // end namespace "os"
112 
113 /** \addtogroup mrpt_memory Memory utilities
114  * @{ */
115 // The following templates are taken from libcvd (LGPL). See
116 // http://mi.eng.cam.ac.uk/~er258/cvd/
117 // Check if the pointer is aligned to the specified byte granularity
118 template <int bytes>
119 bool is_aligned(const void* ptr);
120 template <>
121 inline bool is_aligned<8>(const void* ptr)
122 {
123  return ((reinterpret_cast<size_t>(ptr)) & 0x7) == 0;
124 }
125 template <>
126 inline bool is_aligned<16>(const void* ptr)
127 {
128  return ((reinterpret_cast<size_t>(ptr)) & 0xF) == 0;
129 }
130 /** @} */
131 
132 // A version of MRPT_MAKE_ALIGNED_OPERATOR_NEW that doesn't force including the
133 // entire Eigen lib:
134 #define MRPT_MAKE_ALIGNED_OPERATOR_NEW \
135  void* operator new(size_t size) \
136  { \
137  return mrpt::system::os::aligned_malloc(size, 16); \
138  } \
139  void* operator new[](size_t size) \
140  { \
141  return mrpt::system::os::aligned_malloc(size, 16); \
142  } \
143  void operator delete(void* ptr) noexcept \
144  { \
145  mrpt::system::os::aligned_free(ptr); \
146  } \
147  void operator delete[](void* ptr) noexcept \
148  { \
149  mrpt::system::os::aligned_free(ptr); \
150  } \
151  /* in-place new and delete. since (at least afaik) there is no actual */ \
152  /* memory allocated we can safely let the default implementation handle */ \
153  /* this particular case. */ \
154  static void* operator new(size_t size, void* ptr) \
155  { \
156  return ::operator new(size, ptr); \
157  } \
158  void operator delete(void* memory, void* ptr) noexcept \
159  { \
160  return ::operator delete(memory, ptr); \
161  } \
162  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
163  void* operator new(size_t size, const std::nothrow_t&) noexcept \
164  { \
165  try \
166  { \
167  return mrpt::system::os::aligned_malloc(size, 16); \
168  } \
169  catch (...) \
170  { \
171  return 0; \
172  } \
173  } \
174  void operator delete(void* ptr, const std::nothrow_t&)noexcept \
175  { \
176  mrpt::system::os::aligned_free(ptr); \
177  }
178 
179 } // End of namespace
180 } // End of namespace
181 
182 #endif
bool is_aligned< 8 >(const void *ptr)
Definition: memory.h:121
bool is_aligned< 16 >(const void *ptr)
Definition: memory.h:126
void * aligned_realloc(void *old_ptr, size_t bytes, size_t alignment)
Frees a memory block reserved by aligned_malloc.
Definition: memory.cpp:65
GLenum GLsizei n
Definition: glext.h:5074
_Tp * align_ptr(_Tp *ptr, int n=(int) sizeof(_Tp))
Returns a pointer a bit forward in memory so it&#39;s aligned for the given boundary size.
Definition: memory.h:105
void * aligned_calloc(size_t bytes, size_t alignment)
Identical to aligned_malloc, but it zeroes the reserved memory block.
Definition: memory.h:80
bool is_aligned(const void *ptr)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned long getMemoryUsage()
Returns the memory occupied by this process, in bytes.
Definition: memory.cpp:185
void * aligned_malloc(size_t bytes, size_t alignment)
Returns an aligned memory block.
Definition: memory.cpp:40
void aligned_free(void *p)
Frees a memory block reserved by aligned_malloc.
Definition: memory.cpp:87
GLfloat GLfloat p
Definition: glext.h:6305



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