00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef MRPT_MEMORY_H 00029 #define MRPT_MEMORY_H 00030 00031 #include <mrpt/utils/utils_defs.h> 00032 00033 namespace mrpt 00034 { 00035 namespace system 00036 { 00037 /** \addtogroup mrpt_memory Memory utilities 00038 * \ingroup mrpt_base_grp 00039 * @{ */ 00040 00041 /** Returns the memory occupied by this process, in bytes */ 00042 unsigned long BASE_IMPEXP getMemoryUsage(); 00043 00044 /** 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. 00045 * This method MUST BE a macro rather than a function in order to operate on the caller's stack. 00046 * \sa mrpt_alloca_free 00047 */ 00048 #if defined(_MSC_VER) && (_MSC_VER>=1400) 00049 // Visual Studio 2005, 2008 00050 # define mrpt_alloca( nBytes ) _malloca(nBytes) 00051 #elif defined(HAVE_ALLOCA) 00052 // GCC 00053 # define mrpt_alloca( nBytes ) ::alloca(nBytes) 00054 #else 00055 // Default: Emulate with memory in the heap: 00056 # define mrpt_alloca( nBytes ) ::malloc( nBytes ) 00057 #endif 00058 00059 /** 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. 00060 * This method MUST BE a macro rather than a function in order to operate on the caller's stack. 00061 * \sa mrpt_alloca 00062 */ 00063 #if defined(_MSC_VER) && (_MSC_VER>=1400) 00064 // Visual Studio 2005, 2008 00065 # define mrpt_alloca_free( mem_block ) _freea(mem_block) 00066 #elif defined(HAVE_ALLOCA) 00067 // GCC 00068 # define mrpt_alloca_free( mem_block ) 00069 #else 00070 // Default: Emulate with memory in the heap: 00071 # define mrpt_alloca_free( mem_block ) free(mem_block) 00072 #endif 00073 00074 /** @} */ 00075 00076 namespace os 00077 { 00078 /** \addtogroup mrpt_memory Memory utilities 00079 * @{ */ 00080 00081 /** Returns an aligned memory block. 00082 * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. It must be a power of two. 00083 * \sa aligned_free, aligned_realloc, aligned_calloc 00084 * \note Based on code by William Chan 00085 */ 00086 void BASE_IMPEXP *aligned_malloc(size_t bytes, size_t alignment); 00087 00088 /** Identical to aligned_malloc, but it zeroes the reserved memory block. */ 00089 inline void *aligned_calloc(size_t bytes, size_t alignment) 00090 { 00091 void *ptr = mrpt::system::os::aligned_malloc(bytes, alignment); 00092 if (ptr) ::memset(ptr,0,bytes); 00093 return ptr; 00094 } 00095 00096 /** Frees a memory block reserved by aligned_malloc. 00097 * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. 00098 * If old_ptr is NULL, a new block will be reserved from scratch. 00099 * \sa aligned_malloc, aligned_free 00100 */ 00101 void BASE_IMPEXP *aligned_realloc(void* old_ptr, size_t bytes, size_t alignment); 00102 00103 /** Frees a memory block reserved by aligned_malloc 00104 * \sa aligned_malloc 00105 */ 00106 void BASE_IMPEXP aligned_free(void* p); 00107 00108 /** Returns a pointer a bit forward in memory so it's aligned for the given boundary size 00109 * \note Function copied from OpenCV with a different name to avoid conflicts. 00110 */ 00111 template<typename _Tp> inline _Tp* align_ptr(_Tp* ptr, int n=(int)sizeof(_Tp)) 00112 { 00113 return (_Tp*)(((size_t)ptr + n-1) & -n); 00114 } 00115 00116 /** @} */ 00117 } // end namespace "os" 00118 00119 /** \addtogroup mrpt_memory Memory utilities 00120 * @{ */ 00121 // The following templates are taken from libcvd (LGPL). See http://mi.eng.cam.ac.uk/~er258/cvd/ 00122 // Check if the pointer is aligned to the specified byte granularity 00123 template<int bytes> bool is_aligned(const void* ptr); 00124 template<> inline bool is_aligned<8>(const void* ptr) { return ((reinterpret_cast<size_t>(ptr)) & 0x7) == 0; } 00125 template<> inline bool is_aligned<16>(const void* ptr) { return ((reinterpret_cast<size_t>(ptr)) & 0xF) == 0; } 00126 /** @} */ 00127 00128 } // End of namespace 00129 } // End of namespace 00130 00131 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |