Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

memory.h
Go to the documentation of this file.
1 
33 #pragma once
34 
37 
38 #include <new>
39 
40 namespace ogdf {
41 
44 
52 #define OGDF_MM(Alloc) \
53 public: \
54  static void* operator new(size_t nBytes) { \
55  if (OGDF_LIKELY(Alloc::checkSize(nBytes))) \
56  return Alloc::allocate(nBytes); \
57  else \
58  return ogdf::MallocMemoryAllocator::allocate(nBytes); \
59  } \
60  \
61  static void operator delete(void* p, size_t nBytes) { \
62  if (OGDF_LIKELY(p != 0)) { \
63  if (OGDF_LIKELY(Alloc::checkSize(nBytes))) \
64  Alloc::deallocate(nBytes, p); \
65  else \
66  ogdf::MallocMemoryAllocator::deallocate(nBytes, p); \
67  } \
68  } \
69  static void* operator new(size_t, void* p) { return p; } \
70  static void operator delete(void*, void*) { }
71 
72 #ifdef OGDF_MEMORY_MALLOC_TS
73 # define OGDF_ALLOCATOR ogdf::MallocMemoryAllocator
74 #else
75 # define OGDF_ALLOCATOR ogdf::PoolMemoryAllocator
77 #endif
78 
84 #define OGDF_NEW_DELETE OGDF_MM(OGDF_ALLOCATOR)
85 
91 #define OGDF_MALLOC_NEW_DELETE OGDF_MM(ogdf::MallocMemoryAllocator)
92 
97 template<class T>
98 struct OGDFAllocator {
99  using value_type = T;
100 
101  OGDFAllocator() noexcept = default;
102 
103  template<class U>
104  OGDFAllocator(const OGDFAllocator<U>&) noexcept { }
105 
106  template<class U>
107  bool operator==(const OGDFAllocator<U>&) const noexcept {
108  return true;
109  }
110 
111  template<class U>
112  bool operator!=(const OGDFAllocator<U>&) const noexcept {
113  return false;
114  }
115 
116  T* allocate(const size_t n) const {
117  const size_t s = n * sizeof(T);
118  if (OGDF_LIKELY(OGDF_ALLOCATOR::checkSize(s))) {
119  return static_cast<T*>(OGDF_ALLOCATOR::allocate(s));
120  } else {
121  return static_cast<T*>(ogdf::MallocMemoryAllocator::allocate(s));
122  }
123  }
124 
125  void deallocate(T* const p, size_t n) const noexcept {
126  if (OGDF_LIKELY(p != nullptr)) {
127  const size_t s = n * sizeof(T);
128  if (OGDF_LIKELY(OGDF_ALLOCATOR::checkSize(s))) {
129  OGDF_ALLOCATOR::deallocate(s, p);
130  } else {
132  }
133  }
134  }
135 };
136 
138 
139 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::OGDFAllocator::value_type
T value_type
Definition: memory.h:99
ogdf::OGDFAllocator::allocate
T * allocate(const size_t n) const
Definition: memory.h:116
ogdf::OGDFAllocator
Encapsulates OGDF_ALLOCATOR in a class that can be used as the Allocator for containers of the C++ st...
Definition: memory.h:98
OGDF_LIKELY
#define OGDF_LIKELY(x)
Specify the likely branch in a condition.
Definition: config.h:223
ogdf::OGDFAllocator::deallocate
void deallocate(T *const p, size_t n) const noexcept
Definition: memory.h:125
ogdf::OGDFAllocator::operator==
bool operator==(const OGDFAllocator< U > &) const noexcept
Definition: memory.h:107
ogdf::MallocMemoryAllocator::deallocate
static void deallocate(size_t, void *p)
Deallocates memory at address p. We do not keep track of the size of the deallocated object.
Definition: MallocMemoryAllocator.h:69
ogdf::MallocMemoryAllocator::allocate
static void * allocate(size_t nBytes, const char *, int)
Allocates memory of size nBytes.
Definition: MallocMemoryAllocator.h:56
ogdf::OGDFAllocator::OGDFAllocator
OGDFAllocator() noexcept=default
MallocMemoryAllocator.h
Declaration of memory manager for allocating small pieces of memory.
ogdf::OGDFAllocator::operator!=
bool operator!=(const OGDFAllocator< U > &) const noexcept
Definition: memory.h:112
PoolMemoryAllocator.h
Declaration of memory manager for allocating small pieces of memory.