Main MRPT website > C++ reference
MRPT logo
memory.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef MRPT_MEMORY_H
29 #define MRPT_MEMORY_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 
33 namespace mrpt
34 {
35  namespace system
36  {
37  /** \addtogroup mrpt_memory Memory utilities (in #include <mrpt/system/memory.h>)
38  * \ingroup mrpt_base_grp
39  * @{ */
40 
41  /** Returns the memory occupied by this process, in bytes */
42  unsigned long BASE_IMPEXP getMemoryUsage();
43 
44  /** 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.
45  * This method MUST BE a macro rather than a function in order to operate on the caller's stack.
46  * \sa mrpt_alloca_free
47  */
48 #if defined(_MSC_VER) && (_MSC_VER>=1400)
49  // Visual Studio 2005, 2008
50 # define mrpt_alloca( nBytes ) _malloca(nBytes)
51 #elif defined(HAVE_ALLOCA)
52  // GCC
53 # define mrpt_alloca( nBytes ) ::alloca(nBytes)
54 #else
55  // Default: Emulate with memory in the heap:
56 # define mrpt_alloca( nBytes ) ::malloc( nBytes )
57 #endif
58 
59  /** 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.
60  * This method MUST BE a macro rather than a function in order to operate on the caller's stack.
61  * \sa mrpt_alloca
62  */
63 #if defined(_MSC_VER) && (_MSC_VER>=1400)
64  // Visual Studio 2005, 2008
65 # define mrpt_alloca_free( mem_block ) _freea(mem_block)
66 #elif defined(HAVE_ALLOCA)
67  // GCC
68 # define mrpt_alloca_free( mem_block )
69 #else
70  // Default: Emulate with memory in the heap:
71 # define mrpt_alloca_free( mem_block ) free(mem_block)
72 #endif
73 
74  /** @} */
75 
76  namespace os
77  {
78  /** \addtogroup mrpt_memory Memory utilities
79  * @{ */
80 
81  /** Returns an aligned memory block.
82  * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. It must be a power of two.
83  * \sa aligned_free, aligned_realloc, aligned_calloc
84  * \note Based on code by William Chan
85  */
86  void BASE_IMPEXP *aligned_malloc(size_t bytes, size_t alignment);
87 
88  /** Identical to aligned_malloc, but it zeroes the reserved memory block. */
89  inline void *aligned_calloc(size_t bytes, size_t alignment)
90  {
91  void *ptr = mrpt::system::os::aligned_malloc(bytes, alignment);
92  if (ptr) ::memset(ptr,0,bytes);
93  return ptr;
94  }
95 
96  /** Frees a memory block reserved by aligned_malloc.
97  * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required.
98  * If old_ptr is NULL, a new block will be reserved from scratch.
99  * \sa aligned_malloc, aligned_free
100  */
101  void BASE_IMPEXP *aligned_realloc(void* old_ptr, size_t bytes, size_t alignment);
102 
103  /** Frees a memory block reserved by aligned_malloc
104  * \sa aligned_malloc
105  */
106  void BASE_IMPEXP aligned_free(void* p);
107 
108  /** Returns a pointer a bit forward in memory so it's aligned for the given boundary size
109  * \note Function copied from OpenCV with a different name to avoid conflicts.
110  */
111  template<typename _Tp> inline _Tp* align_ptr(_Tp* ptr, int n=(int)sizeof(_Tp))
112  {
113  return (_Tp*)(((size_t)ptr + n-1) & -n);
114  }
115 
116  /** @} */
117  } // end namespace "os"
118 
119  /** \addtogroup mrpt_memory Memory utilities
120  * @{ */
121  // The following templates are taken from libcvd (LGPL). See http://mi.eng.cam.ac.uk/~er258/cvd/
122  // Check if the pointer is aligned to the specified byte granularity
123  template<int bytes> bool is_aligned(const void* ptr);
124  template<> inline bool is_aligned<8>(const void* ptr) { return ((reinterpret_cast<size_t>(ptr)) & 0x7) == 0; }
125  template<> inline bool is_aligned<16>(const void* ptr) { return ((reinterpret_cast<size_t>(ptr)) & 0xF) == 0; }
126  /** @} */
127 
128  } // End of namespace
129 } // End of namespace
130 
131 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013