MRPT  2.0.4
memory.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 #pragma once
10 
11 #include <cstring>
12 #include <type_traits>
13 
14 namespace mrpt::system
15 {
16 /** \addtogroup mrpt_memory Memory utilities
17  * Header: `#include <mrpt/system/memory.h>`.
18  * Library: \ref mrpt_system_grp
19  * \ingroup mrpt_system_grp
20  * @{ */
21 
22 /** Returns the memory occupied by this process, in bytes */
23 unsigned long getMemoryUsage();
24 
25 /** In platforms and compilers with support to "alloca", allocate a memory block
26  * on the stack; if alloca is not supported, it is emulated as a normal "malloc"
27  * - NOTICE: Since in some platforms alloca will be emulated with malloc,
28  * alloca_free MUST BE ALWAYS CALLED to avoid memory leaks.
29  * This method MUST BE a macro rather than a function in order to operate on
30  * the caller's stack.
31  * \sa mrpt_alloca_free
32  */
33 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
34 // Visual Studio 2005, 2008
35 #define mrpt_alloca(nBytes) _malloca(nBytes)
36 #elif defined(HAVE_ALLOCA)
37 // GCC
38 #define mrpt_alloca(nBytes) ::alloca(nBytes)
39 #else
40 // Default: Emulate with memory in the heap:
41 #define mrpt_alloca(nBytes) ::malloc(nBytes)
42 #endif
43 
44 /** This method must be called to "free" each memory block allocated with
45  * "system::alloca": If the block was really allocated in the stack, no
46  * operation is actually performed, otherwise it will be freed from the heap.
47  * This method MUST BE a macro rather than a function in order to operate on
48  * the caller's stack.
49  * \sa mrpt_alloca
50  */
51 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
52 // Visual Studio 2005, 2008
53 #define mrpt_alloca_free(mem_block) _freea(mem_block)
54 #elif defined(HAVE_ALLOCA)
55 // GCC
56 #define mrpt_alloca_free(mem_block)
57 #else
58 // Default: Emulate with memory in the heap:
59 #define mrpt_alloca_free(mem_block) free(mem_block)
60 #endif
61 
62 /** @} */
63 
64 /** \addtogroup mrpt_memory Memory utilities
65  * @{ */
66 template <
67  std::size_t alignment, typename T,
68  typename = std::enable_if_t<std::is_pointer<T>::value>>
69 bool is_aligned(T ptr)
70 {
71  return alignment == 0 ||
72  reinterpret_cast<std::size_t>(ptr) % alignment == 0;
73 }
74 /** @} */
75 
76 } // namespace mrpt::system
bool is_aligned(T ptr)
Definition: memory.h:69
unsigned long getMemoryUsage()
Returns the memory occupied by this process, in bytes.
Definition: memory.cpp:114



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020