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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019