Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMemoryUtilities.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef _MITK_MEMORY_UTILITIES_H_
18 #define _MITK_MEMORY_UTILITIES_H_
19 
20 #include <MitkCoreExports.h>
21 #include <itkMacro.h>
22 
23 namespace mitk
24 {
26  {
27  public:
35  static size_t GetProcessMemoryUsage();
36 
40  static size_t GetTotalSizeOfPhysicalRam();
41 
52  template <typename ElementType>
53  static ElementType *AllocateElements(size_t numberOfElements, bool noThrow = false)
54  {
55  // Encapsulate all image memory allocation here to throw an
56  // exception when memory allocation fails even when the compiler
57  // does not do this by default.
58  ElementType *data = nullptr;
59  try
60  {
61  data = new ElementType[numberOfElements];
62  }
63  catch (...)
64  {
65  data = nullptr;
66  }
67  if ((data == nullptr) && (noThrow == false))
68  {
69  throw itk::MemoryAllocationError(__FILE__, __LINE__, "Failed to allocate memory.", ITK_LOCATION);
70  }
71  return data;
72  }
73 
78  template <typename ElementType>
79  static void DeleteElements(ElementType *elements)
80  {
81  if (elements != NULL)
82  {
83  delete[] elements;
84  }
85  }
86 
87  protected:
88 #ifndef _MSC_VER
89  static int ReadStatmFromProcFS(
90  int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages);
91 #endif
92  };
93 } // end of namespace mitk
94 
95 #endif
static void DeleteElements(ElementType *elements)
#define MITKCORE_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
static ElementType * AllocateElements(vcl_size_t numberOfElements, bool noThrow=false)