Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef _MITK_MEMORY_UTILITIES_H_
14 #define _MITK_MEMORY_UTILITIES_H_
15 
16 #include <MitkCoreExports.h>
17 #include <itkMacro.h>
18 
19 namespace mitk
20 {
22  {
23  public:
31  static size_t GetProcessMemoryUsage();
32 
36  static size_t GetTotalSizeOfPhysicalRam();
37 
48  template <typename ElementType>
49  static ElementType *AllocateElements(size_t numberOfElements, bool noThrow = false)
50  {
51  // Encapsulate all image memory allocation here to throw an
52  // exception when memory allocation fails even when the compiler
53  // does not do this by default.
54  ElementType *data = nullptr;
55  try
56  {
57  data = new ElementType[numberOfElements];
58  }
59  catch (...)
60  {
61  data = nullptr;
62  }
63  if ((data == nullptr) && (noThrow == false))
64  {
65  throw itk::MemoryAllocationError(__FILE__, __LINE__, "Failed to allocate memory.", ITK_LOCATION);
66  }
67  return data;
68  }
69 
74  template <typename ElementType>
75  static void DeleteElements(ElementType *elements)
76  {
77  if (elements != nullptr)
78  {
79  delete[] elements;
80  }
81  }
82 
83  protected:
84 #ifndef _MSC_VER
85  static int ReadStatmFromProcFS(
86  int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages);
87 #endif
88  };
89 } // end of namespace mitk
90 
91 #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)