Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

memory.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/basic.h>
38 
39 #include <cstddef>
40 
41 namespace ogdf {
42 
45 
53 #define OGDF_MM(Alloc) \
54 public: \
55  static void* operator new(size_t nBytes) { \
56  if (OGDF_LIKELY(Alloc::checkSize(nBytes))) \
57  return Alloc::allocate(nBytes); \
58  else \
59  return ogdf::MallocMemoryAllocator::allocate(nBytes); \
60  } \
61  \
62  static void operator delete(void* p, size_t nBytes) { \
63  if (OGDF_LIKELY(p != 0)) { \
64  if (OGDF_LIKELY(Alloc::checkSize(nBytes))) \
65  Alloc::deallocate(nBytes, p); \
66  else \
67  ogdf::MallocMemoryAllocator::deallocate(nBytes, p); \
68  } \
69  } \
70  static void* operator new(size_t, void* p) { return p; } \
71  static void operator delete(void*, void*) { }
72 
73 #ifdef OGDF_MEMORY_MALLOC_TS
74 # define OGDF_ALLOCATOR ogdf::MallocMemoryAllocator
75 #else
76 # define OGDF_ALLOCATOR ogdf::PoolMemoryAllocator
78 #endif
79 
85 #define OGDF_NEW_DELETE OGDF_MM(OGDF_ALLOCATOR)
86 
92 #define OGDF_MALLOC_NEW_DELETE OGDF_MM(ogdf::MallocMemoryAllocator)
93 
98 template<class T>
99 struct OGDFAllocator {
100  using value_type = T;
101 
102  OGDFAllocator() noexcept = default;
103 
104  template<class U>
105  OGDFAllocator(const OGDFAllocator<U>&) noexcept { }
106 
107  template<class U>
108  bool operator==(const OGDFAllocator<U>&) const noexcept {
109  return true;
110  }
111 
112  template<class U>
113  bool operator!=(const OGDFAllocator<U>&) const noexcept {
114  return false;
115  }
116 
117  T* allocate(const size_t n) const {
118  const size_t s = n * sizeof(T);
119  if (OGDF_LIKELY(OGDF_ALLOCATOR::checkSize(s))) {
120  return static_cast<T*>(OGDF_ALLOCATOR::allocate(s));
121  } else {
122  return static_cast<T*>(ogdf::MallocMemoryAllocator::allocate(s));
123  }
124  }
125 
126  void deallocate(T* const p, size_t n) const noexcept {
127  if (OGDF_LIKELY(p != nullptr)) {
128  const size_t s = n * sizeof(T);
129  if (OGDF_LIKELY(OGDF_ALLOCATOR::checkSize(s))) {
130  OGDF_ALLOCATOR::deallocate(s, p);
131  } else {
133  }
134  }
135  }
136 };
137 
139 
140 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::OGDFAllocator::value_type
T value_type
Definition: memory.h:100
ogdf::OGDFAllocator::allocate
T * allocate(const size_t n) const
Definition: memory.h:117
ogdf::OGDFAllocator
Encapsulates OGDF_ALLOCATOR in a class that can be used as the Allocator for containers of the C++ st...
Definition: memory.h:99
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:126
ogdf::OGDFAllocator::operator==
bool operator==(const OGDFAllocator< U > &) const noexcept
Definition: memory.h:108
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:72
ogdf::MallocMemoryAllocator::allocate
static void * allocate(size_t nBytes, const char *, int)
Allocates memory of size nBytes.
Definition: MallocMemoryAllocator.h:59
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:113
basic.h
Basic declarations, included by all source files.
PoolMemoryAllocator.h
Declaration of memory manager for allocating small pieces of memory.