Open
Graph Drawing
Framework

 v. 2022.02 (Dogwood)
 

PoolMemoryAllocator.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/System.h>
36 
37 #ifndef OGDF_MEMORY_POOL_NTS
38 # include <mutex>
39 #endif
40 
41 namespace ogdf {
42 
44 
57  struct MemElem {
59  };
60 
61  using MemElemPtr = MemElem*;
62 
63  struct PoolElement;
64  struct BlockChain;
65 
66  static constexpr size_t MIN_BYTES = sizeof(MemElemPtr);
67  static constexpr size_t TABLE_SIZE = 256;
68  static constexpr size_t BLOCK_SIZE = 8192;
69 
70 public:
73 
75  static OGDF_EXPORT void cleanup();
76 
78  static OGDF_EXPORT bool checkSize(size_t nBytes) {
79  return nBytes < TABLE_SIZE;
80  }
81 
83  static OGDF_EXPORT void *allocate(size_t nBytes);
84 
86  static OGDF_EXPORT void deallocate(size_t nBytes, void *p);
87 
89 
94  static OGDF_EXPORT void deallocateList(size_t nBytes, void *pHead, void *pTail);
95 
97  static OGDF_EXPORT void flushPool();
98 
100  static OGDF_EXPORT size_t memoryAllocatedInBlocks();
101 
103  static OGDF_EXPORT size_t memoryInGlobalFreeList();
104 
106  static OGDF_EXPORT size_t memoryInThreadFreeList();
107 
115  static OGDF_EXPORT void defrag();
116 
117 private:
118  static inline void enterCS() {
119 #ifndef OGDF_MEMORY_POOL_NTS
120  s_mutex.lock();
121 #endif
122  }
123 
124  static inline void leaveCS() {
125 #ifndef OGDF_MEMORY_POOL_NTS
126  s_mutex.unlock();
127 #endif
128  }
129 
130  static int slicesPerBlock(uint16_t nBytes) {
131  int nWords;
132  return slicesPerBlock(nBytes,nWords);
133  }
134 
135  static int slicesPerBlock(uint16_t nBytes, int &nWords) {
136  nWords = (nBytes + OGDF_SIZEOF_POINTER - 1) / OGDF_SIZEOF_POINTER;
137  return (BLOCK_SIZE - OGDF_SIZEOF_POINTER) / (nWords * OGDF_SIZEOF_POINTER);
138  }
139 
140  static void *fillPool(MemElemPtr &pFreeBytes, uint16_t nBytes);
141 
142  static MemElemPtr allocateBlock();
143  static void makeSlices(MemElemPtr p, int nWords, int nSlices);
144 
145  static size_t unguardedMemGlobalFreelist();
146 
149  static PoolElement s_pool[TABLE_SIZE];
150 
152  static BlockChain *s_blocks;
153 
154 #ifdef OGDF_DEBUG
155  static long long s_globallyAllocatedBytes;
158  static thread_local long long s_locallyAllocatedBytes;
159 #endif
160 
161 #ifdef OGDF_MEMORY_POOL_NTS
162  static MemElemPtr s_tp[TABLE_SIZE];
163 #else
164  static std::mutex s_mutex;
166  static thread_local MemElemPtr s_tp[TABLE_SIZE];
167 #endif
168 
169 };
170 
171 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::PoolMemoryAllocator::deallocateList
static void deallocateList(size_t nBytes, void *pHead, void *pTail)
Deallocate a complete list starting at pHead and ending at pTail.
ogdf::PoolMemoryAllocator::PoolMemoryAllocator
PoolMemoryAllocator()
Definition: PoolMemoryAllocator.h:71
ogdf::PoolMemoryAllocator::slicesPerBlock
static int slicesPerBlock(uint16_t nBytes, int &nWords)
Definition: PoolMemoryAllocator.h:135
ogdf::PoolMemoryAllocator::MemElem
Basic memory element used to realize a linked list of deallocated memory segments.
Definition: PoolMemoryAllocator.h:57
ogdf::PoolMemoryAllocator::memoryInThreadFreeList
static size_t memoryInThreadFreeList()
Returns the total amount of memory (in bytes) available in the thread's free lists.
System.h
Decalration of System class which provides unified access to system information.
ogdf::PoolMemoryAllocator::defrag
static void defrag()
Defragments the global free lists.
ogdf::PoolMemoryAllocator::allocateBlock
static MemElemPtr allocateBlock()
ogdf::PoolMemoryAllocator::allocate
static void * allocate(size_t nBytes)
Allocates memory of size nBytes.
ogdf::PoolMemoryAllocator::memoryInGlobalFreeList
static size_t memoryInGlobalFreeList()
Returns the total amount of memory (in bytes) available in the global free lists.
ogdf::PoolMemoryAllocator::memoryAllocatedInBlocks
static size_t memoryAllocatedInBlocks()
Returns the total amount of memory (in bytes) allocated from the system.
ogdf::PoolMemoryAllocator::~PoolMemoryAllocator
~PoolMemoryAllocator()
Definition: PoolMemoryAllocator.h:72
ogdf::PoolMemoryAllocator::enterCS
static void enterCS()
Definition: PoolMemoryAllocator.h:118
ogdf::PoolMemoryAllocator::makeSlices
static void makeSlices(MemElemPtr p, int nWords, int nSlices)
ogdf::PoolMemoryAllocator::deallocate
static void deallocate(size_t nBytes, void *p)
Deallocates memory at address p which is of size nBytes.
OGDF_SIZEOF_POINTER
#define OGDF_SIZEOF_POINTER
The size of a pointer.
Definition: config_autogen.h:25
ogdf::PoolMemoryAllocator::cleanup
static void cleanup()
Frees all allocated memory.
ogdf::PoolMemoryAllocator::TABLE_SIZE
static constexpr size_t TABLE_SIZE
Definition: PoolMemoryAllocator.h:67
ogdf::PoolMemoryAllocator::leaveCS
static void leaveCS()
Definition: PoolMemoryAllocator.h:124
ogdf::PoolMemoryAllocator::unguardedMemGlobalFreelist
static size_t unguardedMemGlobalFreelist()
ogdf::PoolMemoryAllocator::slicesPerBlock
static int slicesPerBlock(uint16_t nBytes)
Definition: PoolMemoryAllocator.h:130
ogdf::PoolMemoryAllocator::s_mutex
static std::mutex s_mutex
Definition: PoolMemoryAllocator.h:164
ogdf::PoolMemoryAllocator
Allocates memory in large chunks for better runtime.
Definition: PoolMemoryAllocator.h:55
ogdf::PoolMemoryAllocator::flushPool
static void flushPool()
Flushes all free but allocated bytes (s_tp) to the thread-global list (s_pool) of allocated bytes.
ogdf::PoolMemoryAllocator::BLOCK_SIZE
static constexpr size_t BLOCK_SIZE
Definition: PoolMemoryAllocator.h:68
ogdf::PoolMemoryAllocator::fillPool
static void * fillPool(MemElemPtr &pFreeBytes, uint16_t nBytes)
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
ogdf::PoolMemoryAllocator::s_tp
static thread_local MemElemPtr s_tp[TABLE_SIZE]
Contains the allocated but free memory for a single thread.
Definition: PoolMemoryAllocator.h:166
ogdf::PoolMemoryAllocator::MemElem::m_next
MemElem * m_next
Definition: PoolMemoryAllocator.h:58
ogdf::PoolMemoryAllocator::MemElemPtr
MemElem * MemElemPtr
Definition: PoolMemoryAllocator.h:61
ogdf::PoolMemoryAllocator::s_blocks
static BlockChain * s_blocks
Holds all allocated memory independently of whether it is cleared in chunks of size BLOCK_SIZE.
Definition: PoolMemoryAllocator.h:152
ogdf::PoolMemoryAllocator::s_pool
static PoolElement s_pool[TABLE_SIZE]
Contains allocated but free memory that may be used by all threads. Filled upon exiting a thread that...
Definition: PoolMemoryAllocator.h:149
ogdf::PoolMemoryAllocator::checkSize
static bool checkSize(size_t nBytes)
Returns true iff allocate can be invoked with nBytes.
Definition: PoolMemoryAllocator.h:78
ogdf::PoolMemoryAllocator::MIN_BYTES
static constexpr size_t MIN_BYTES
Definition: PoolMemoryAllocator.h:66