00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 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 /** Returns the memory occupied by this process, in bytes */ 00038 unsigned long BASE_IMPEXP getMemoryUsage(); 00039 00040 /** 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. 00041 * This method MUST BE a macro rather than a function in order to operate on the caller's stack. 00042 * \sa mrpt_alloca_free 00043 */ 00044 #if defined(_MSC_VER) && (_MSC_VER>=1400) 00045 // Visual Studio 2005, 2008 00046 # define mrpt_alloca( nBytes ) _malloca(nBytes) 00047 #elif defined(HAVE_ALLOCA) 00048 // GCC 00049 # define mrpt_alloca( nBytes ) ::alloca(nBytes) 00050 #else 00051 // Default: Emulate with memory in the heap: 00052 # define mrpt_alloca( nBytes ) ::malloc( nBytes ); 00053 #endif 00054 00055 /** 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. 00056 * This method MUST BE a macro rather than a function in order to operate on the caller's stack. 00057 * \sa mrpt_alloca 00058 */ 00059 #if defined(_MSC_VER) && (_MSC_VER>=1400) 00060 // Visual Studio 2005, 2008 00061 # define mrpt_alloca_free( mem_block ) _freea(mem_block) 00062 #elif defined(HAVE_ALLOCA) 00063 // GCC 00064 # define mrpt_alloca_free( mem_block ) 00065 #else 00066 // Default: Emulate with memory in the heap: 00067 # define mrpt_alloca_free( mem_block ) free(mem_block) 00068 #endif 00069 00070 namespace os 00071 { 00072 /** @name Aligned memory management 00073 @{ */ 00074 00075 /** Returns an aligned memory block. 00076 * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. It must be a power of two. 00077 * \sa aligned_free, aligned_realloc, aligned_calloc 00078 * \note Based on code by William Chan 00079 */ 00080 void BASE_IMPEXP *aligned_malloc(size_t bytes, size_t alignment); 00081 00082 /** Identical to aligned_malloc, but it zeroes the reserved memory block. */ 00083 inline void *aligned_calloc(size_t bytes, size_t alignment) 00084 { 00085 void *ptr = mrpt::system::os::aligned_malloc(bytes, alignment); 00086 if (ptr) ::memset(ptr,0,bytes); 00087 return ptr; 00088 } 00089 00090 /** Frees a memory block reserved by aligned_malloc. 00091 * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. 00092 * If old_ptr is NULL, a new block will be reserved from scratch. 00093 * \sa aligned_malloc, aligned_free 00094 */ 00095 void BASE_IMPEXP *aligned_realloc(void* old_ptr, size_t bytes, size_t alignment); 00096 00097 /** Frees a memory block reserved by aligned_malloc 00098 * \sa aligned_malloc 00099 */ 00100 void BASE_IMPEXP aligned_free(void* p); 00101 00102 /** Returns a pointer a bit forward in memory so it's aligned for the given boundary size 00103 * \note Function copied from OpenCV with a different name to avoid conflicts. 00104 */ 00105 template<typename _Tp> inline _Tp* align_ptr(_Tp* ptr, int n=(int)sizeof(_Tp)) 00106 { 00107 return (_Tp*)(((size_t)ptr + n-1) & -n); 00108 } 00109 00110 /** @} */ 00111 00112 } // end namespace "os" 00113 } // End of namespace 00114 } // End of namespace 00115 00116 #endif
Page generated by Doxygen 1.7.2 for MRPT 0.9.4 SVN: at Mon Jan 10 22:46:17 UTC 2011 |