Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
41namespace ogdf {
42
45
53#define OGDF_MM(Alloc) \
54public: \
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
77# 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
98template<class T>
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}
Declaration of memory manager for allocating small pieces of memory.
Declaration of memory manager for allocating small pieces of memory.
Basic declarations, included by all source files.
static void * allocate(size_t nBytes, const char *, int)
Allocates memory of size nBytes.
static void deallocate(size_t, void *p)
Deallocates memory at address p. We do not keep track of the size of the deallocated object.
#define OGDF_LIKELY(x)
Specify the likely branch in a condition.
Definition config.h:309
The namespace for all OGDF objects.
Encapsulates OGDF_ALLOCATOR in a class that can be used as the Allocator for containers of the C++ st...
Definition memory.h:99
T * allocate(const size_t n) const
Definition memory.h:117
void deallocate(T *const p, size_t n) const noexcept
Definition memory.h:126
bool operator==(const OGDFAllocator< U > &) const noexcept
Definition memory.h:108
OGDFAllocator() noexcept=default
bool operator!=(const OGDFAllocator< U > &) const noexcept
Definition memory.h:113