Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

PoolMemoryAllocator.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/basic.h>
37 
38 #include <cstddef>
39 #include <cstdint>
40 #ifndef OGDF_MEMORY_POOL_NTS
41 # include <mutex>
42 #endif
43 
44 namespace ogdf {
45 
47 
60  struct MemElem {
62  };
63 
64  using MemElemPtr = MemElem*;
65 
66  struct BlockChain;
67  struct PoolElement;
68 
69  static constexpr size_t MIN_BYTES = sizeof(MemElemPtr);
70  static constexpr size_t TABLE_SIZE = 256;
71  static constexpr size_t BLOCK_SIZE = 8192;
72 
73 public:
75 
77 
79  static OGDF_EXPORT void cleanup();
80 
82  static OGDF_EXPORT bool checkSize(size_t nBytes);
83 
85  static OGDF_EXPORT void* allocate(size_t nBytes);
86 
88  static OGDF_EXPORT void deallocate(size_t nBytes, void* p);
89 
91 
96  static OGDF_EXPORT void deallocateList(size_t nBytes, void* pHead, void* pTail);
97 
99  static OGDF_EXPORT void flushPool();
100 
102  static OGDF_EXPORT size_t memoryAllocatedInBlocks();
103 
105  static OGDF_EXPORT size_t memoryInGlobalFreeList();
106 
108  static OGDF_EXPORT size_t memoryInThreadFreeList();
109 
117  static OGDF_EXPORT void defrag();
118 
119 private:
120  static inline void enterCS() {
121 #ifndef OGDF_MEMORY_POOL_NTS
122  s_mutex.lock();
123 #endif
124  }
125 
126  static inline void leaveCS() {
127 #ifndef OGDF_MEMORY_POOL_NTS
128  s_mutex.unlock();
129 #endif
130  }
131 
132  static int slicesPerBlock(uint16_t nBytes) {
133  int nWords;
134  return slicesPerBlock(nBytes, nWords);
135  }
136 
137  static int slicesPerBlock(uint16_t nBytes, int& nWords) {
138  nWords = (nBytes + OGDF_SIZEOF_POINTER - 1) / OGDF_SIZEOF_POINTER;
139  return (BLOCK_SIZE - OGDF_SIZEOF_POINTER) / (nWords * OGDF_SIZEOF_POINTER);
140  }
141 
142  static void* fillPool(MemElemPtr& pFreeBytes, uint16_t nBytes);
143 
144  static MemElemPtr allocateBlock();
145  static void makeSlices(MemElemPtr p, int nWords, int nSlices);
146 
147  static size_t unguardedMemGlobalFreelist();
148 
151  static PoolElement s_pool[TABLE_SIZE];
152 
154  static BlockChain* s_blocks;
155 
156 #ifdef OGDF_DEBUG
157  static long long s_globallyAllocatedBytes;
160  static thread_local long long s_locallyAllocatedBytes;
161 #endif
162 
163 #ifdef OGDF_MEMORY_POOL_NTS
164  static MemElemPtr s_tp[TABLE_SIZE];
165 #else
166  static std::mutex s_mutex;
168  static thread_local MemElemPtr s_tp[TABLE_SIZE];
169 #endif
170 };
171 
172 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
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:74
ogdf::PoolMemoryAllocator::slicesPerBlock
static int slicesPerBlock(uint16_t nBytes, int &nWords)
Definition: PoolMemoryAllocator.h:137
ogdf::PoolMemoryAllocator::MemElem
Basic memory element used to realize a linked list of deallocated memory segments.
Definition: PoolMemoryAllocator.h:60
ogdf::PoolMemoryAllocator::memoryInThreadFreeList
static size_t memoryInThreadFreeList()
Returns the total amount of memory (in bytes) available in the thread's free lists.
ogdf::PoolMemoryAllocator::defrag
static void defrag()
Defragments the global free lists.
ogdf::PoolMemoryAllocator::s_locallyAllocatedBytes
static thread_local long long s_locallyAllocatedBytes
Holds the number of thread-locally allocated bytes for debugging.
Definition: PoolMemoryAllocator.h:160
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:76
ogdf::PoolMemoryAllocator::enterCS
static void enterCS()
Definition: PoolMemoryAllocator.h:120
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:70
ogdf::PoolMemoryAllocator::leaveCS
static void leaveCS()
Definition: PoolMemoryAllocator.h:126
ogdf::PoolMemoryAllocator::unguardedMemGlobalFreelist
static size_t unguardedMemGlobalFreelist()
ogdf::PoolMemoryAllocator::s_globallyAllocatedBytes
static long long s_globallyAllocatedBytes
Holds the number of globally allocated bytes for debugging.
Definition: PoolMemoryAllocator.h:158
ogdf::PoolMemoryAllocator::slicesPerBlock
static int slicesPerBlock(uint16_t nBytes)
Definition: PoolMemoryAllocator.h:132
ogdf::PoolMemoryAllocator::s_mutex
static std::mutex s_mutex
Definition: PoolMemoryAllocator.h:166
config_autogen.h
ogdf::PoolMemoryAllocator
Allocates memory in large chunks for better runtime.
Definition: PoolMemoryAllocator.h:58
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:71
basic.h
Basic declarations, included by all source files.
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:101
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:168
ogdf::PoolMemoryAllocator::MemElem::m_next
MemElem * m_next
Definition: PoolMemoryAllocator.h:61
ogdf::PoolMemoryAllocator::MemElemPtr
MemElem * MemElemPtr
Definition: PoolMemoryAllocator.h:64
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:154
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:151
ogdf::PoolMemoryAllocator::checkSize
static bool checkSize(size_t nBytes)
Returns true iff allocate can be invoked with nBytes.
ogdf::PoolMemoryAllocator::MIN_BYTES
static constexpr size_t MIN_BYTES
Definition: PoolMemoryAllocator.h:69