Main MRPT website > C++ reference for MRPT 1.5.9
memory.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <cstdio> // for size_t, fclose, fopen, fscanf, FILE
13 #include <cstdlib> // for free, posix_memalign, realloc
14 #include <exception> // for exception
15 #include <mrpt/config.h> // for HAVE_POSIX_MEMALIGN, MRPT_OS_LINUX
16 #include <mrpt/utils/mrpt_macros.h> // for MRPT_END, MRPT_START, MRPT_UNUSE...
17 #include <mrpt/system/memory.h>
18 
19 #ifdef MRPT_OS_APPLE
20 #include <mach/mach_init.h>
21 #include <mach/task.h>
22 #endif
23 
24 using namespace mrpt;
25 using namespace mrpt::system;
26 using namespace std;
27 
28 // Management of aligned memory for efficiency:
29 // If we have "posix_memalign", use it, then realloc / free as usual. (GCC/Linux)
30 // If we have "_aligned_malloc", use it, then _aligned_realloc/_aligned_free (MSVC, MinGW)
31 
32 /** Returns an aligned memory block.
33  * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. It must be a power of two.
34  * \sa aligned_free, aligned_realloc
35  * \note Based on code by William Chan
36 */
37 void* mrpt::system::os::aligned_malloc(size_t bytes, size_t alignment)
38 {
39 #if defined(HAVE_ALIGNED_MALLOC)
40 # if defined(__GNUC__) && !defined(__MINGW32__)
41  return ::aligned_malloc(bytes,alignment);
42 # else
43  return _aligned_malloc(bytes,alignment);
44 # endif
45 #elif defined(HAVE_POSIX_MEMALIGN)
46  void *ptr=NULL;
47  int ret = posix_memalign(&ptr,alignment,bytes);
48  if (ret) THROW_EXCEPTION("posix_memalign returned an error.");
49  return ptr;
50 #else
51  // We don't have aligned memory:
52  return ::malloc(bytes);
53 #endif
54 }
55 
56 /** Frees a memory block reserved by aligned_malloc.
57  * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required.
58  * If old_ptr is NULL, a new block will be reserved from scratch.
59  * \sa aligned_malloc, aligned_free
60  */
61 void* mrpt::system::os::aligned_realloc(void* old_ptr, size_t bytes, size_t alignment)
62 {
63 #if defined(HAVE_ALIGNED_MALLOC)
64 # if defined(__GNUC__) && !defined(__MINGW32__)
65  return ::aligned_realloc(old_ptr,bytes,alignment);
66 # else
67  return _aligned_realloc(old_ptr,bytes,alignment);
68 # endif
69 #elif defined(HAVE_POSIX_MEMALIGN)
70  MRPT_UNUSED_PARAM(alignment);
71  return ::realloc(old_ptr,bytes);
72 #else
73  MRPT_UNUSED_PARAM(alignment);
74  // We don't have aligned memory:
75  return ::realloc(old_ptr,bytes);
76 #endif
77 }
78 
79 /** Frees a memory block reserved by aligned_malloc
80  * \sa aligned_malloc
81  */
83 {
84 #if defined(HAVE_ALIGNED_MALLOC)
85 # if defined(__GNUC__) && !defined(__MINGW32__)
87 # else
88  _aligned_free(p);
89 # endif
90 #elif defined(HAVE_POSIX_MEMALIGN)
91  free(p);
92 #else
93  // We don't have aligned memory:
94  free(p);
95 #endif
96 }
97 
98 
99 #ifdef MRPT_OS_WINDOWS
100 #include <windows.h>
101  // Windows:
102  typedef struct _PROCESS_MEMORY_COUNTERS {
103  DWORD cb;
104  DWORD PageFaultCount;
105  SIZE_T PeakWorkingSetSize;
106  SIZE_T WorkingSetSize;
107  SIZE_T QuotaPeakPagedPoolUsage;
108  SIZE_T QuotaPagedPoolUsage;
109  SIZE_T QuotaPeakNonPagedPoolUsage;
110  SIZE_T QuotaNonPagedPoolUsage;
111  SIZE_T PagefileUsage;
112  SIZE_T PeakPagefileUsage;
113  } PROCESS_MEMORY_COUNTERS,
114  *PPROCESS_MEMORY_COUNTERS;
115 
116  namespace mrpt
117  {
118  namespace system
119  {
120  /** This is an auxiliary class for mrpt::system::getMemoryUsage() under Windows.
121  * It loads in runtime PSAPI.DLL. This is to avoid problems in some platforms, i.e Windows 2000,
122  * where this DLL must not be present.
123  */
124  class CAuxPSAPI_Loader
125  {
126  protected:
127  typedef BOOL (WINAPI *TGetProcessMemoryInfo)(
128  HANDLE Process,
129  PPROCESS_MEMORY_COUNTERS ppsmemCounters,
130  DWORD cb );
131 
132  TGetProcessMemoryInfo m_ptr;
133 
134  public:
135  HMODULE m_dll;
136 
137  CAuxPSAPI_Loader()
138  {
139  m_ptr = NULL;
140 
141  m_dll = LoadLibraryA("PSAPI.DLL");
142  if (m_dll)
143  {
144  m_ptr = (TGetProcessMemoryInfo) GetProcAddress(m_dll,"GetProcessMemoryInfo");
145  }
146  }
147  ~CAuxPSAPI_Loader()
148  {
149  if (m_dll)
150  {
151  FreeLibrary(m_dll);
152  m_dll = NULL;
153  m_ptr = NULL;
154  }
155  }
156 
157  BOOL WINAPI GetProcessMemoryInfo(
158  HANDLE Process,
159  PPROCESS_MEMORY_COUNTERS ppsmemCounters,
160  DWORD cb )
161  {
162  try
163  {
164  if (!m_ptr)
165  return false;
166  else return (*m_ptr )(Process,ppsmemCounters,cb);
167  }
168  catch(...)
169  {
170  return false;
171  }
172  }
173  };
174  }
175  }
176 
177 #endif
178 
179 /*---------------------------------------------------------------
180  getMemoryUsage
181  ---------------------------------------------------------------*/
183 {
184  MRPT_START
185  unsigned long MEM = 0;
186 
187 #ifdef MRPT_OS_WINDOWS
188  // Windows:
189  static CAuxPSAPI_Loader PSAPI_LOADER;
190 
191  PROCESS_MEMORY_COUNTERS pmc;
192  pmc.cb = sizeof(pmc);
193 
194  if ( PSAPI_LOADER.GetProcessMemoryInfo( GetCurrentProcess(),&pmc,sizeof(pmc) ) )
195  {
196  MEM = (long)pmc.PagefileUsage;
197  }
198 #endif
199 
200 #ifdef MRPT_OS_LINUX
201  // Linux:
202  //int page_size = getpagesize();
203 
204  FILE *f = ::fopen ("/proc/self/stat", "r");
205  if (!f) return 0;
206 
207  // Note: some of these scanf specifiers would normally be 'long' versions if
208  // not for the fact that we are using suppression (gcc warns). see 'man
209  // proc' for scanf specifiers and meanings.
210  if (!::fscanf(f,
211  "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %lu", &MEM))
212  {
213  // Error parsing:
214  MEM=0;
215  }
216  ::fclose (f);
217  //::system("cat /proc/self/statm");
218 #endif
219 
220 #ifdef MRPT_OS_APPLE
221  mach_task_basic_info info;
222  mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
223  if(task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)==0)
224  {
225  MEM=info.virtual_size;
226  }
227 #endif
228  return MEM;
229  MRPT_END
230 }
GLuint GLuint GLsizei count
Definition: glext.h:3512
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
Definition: os.cpp:255
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:272
#define THROW_EXCEPTION(msg)
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
STL namespace.
int BOOL
Definition: xstypedefs.h:62
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_START
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
backing_store_ptr info
Definition: jmemsys.h:170
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
Definition: inflate.h:53



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020