Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
PoolMemoryAllocator.h
Go to the documentation of this file.
1
33#pragma once
34
35#include <ogdf/basic/basic.h>
36#include <ogdf/basic/internal/config_autogen.h>
37
38#include <cstddef>
39#include <cstdint>
40#include <vector>
41#ifndef OGDF_MEMORY_POOL_NTS
42# include <mutex>
43#endif
44
45namespace ogdf {
46
48
61 struct MemElem {
63 };
64
66
67 struct BlockChain;
68 struct PoolElement;
69
70 static constexpr size_t MIN_BYTES = sizeof(MemElemPtr);
71 static constexpr size_t TABLE_SIZE = 256;
72 static constexpr size_t BLOCK_SIZE = 8192;
73
74public:
76
78
80 static OGDF_EXPORT void cleanup();
81
83 static OGDF_EXPORT bool checkSize(size_t nBytes);
84
86 static OGDF_EXPORT void* allocate(size_t nBytes);
87
89 static OGDF_EXPORT void deallocate(size_t nBytes, void* p);
90
92
97 static OGDF_EXPORT void deallocateList(size_t nBytes, void* pHead, void* pTail);
98
100 static OGDF_EXPORT void flushPool();
101
104
107
110
119
128
130 static OGDF_EXPORT void getGlobalFreeListSizes(std::vector<size_t>& sizes);
131
133 static OGDF_EXPORT void getThreadFreeListSizes(std::vector<size_t>& sizes);
134
135private:
136 static inline void enterCS() {
137#ifndef OGDF_MEMORY_POOL_NTS
138 s_mutex.lock();
139#endif
140 }
141
142 static inline void leaveCS() {
143#ifndef OGDF_MEMORY_POOL_NTS
144 s_mutex.unlock();
145#endif
146 }
147
148 static int slicesPerBlock(uint16_t nBytes) {
149 int nWords;
150 return slicesPerBlock(nBytes, nWords);
151 }
152
153 static int slicesPerBlock(uint16_t nBytes, int& nWords) {
154 nWords = (nBytes + OGDF_SIZEOF_POINTER - 1) / OGDF_SIZEOF_POINTER;
155 return (BLOCK_SIZE - OGDF_SIZEOF_POINTER) / (nWords * OGDF_SIZEOF_POINTER);
156 }
157
158 static void* fillPool(MemElemPtr& pFreeBytes, uint16_t nBytes);
159
161 static void makeSlices(MemElemPtr p, int nWords, int nSlices);
162
164 static BlockChain* s_blocks;
165
166#ifdef OGDF_DEBUG
168 static long long s_globallyAllocatedBytes;
170 static thread_local long long s_locallyAllocatedBytes;
171#endif
172
173#ifdef OGDF_MEMORY_POOL_NTS
174 static MemElemPtr s_tp[TABLE_SIZE];
175#else
178 static PoolElement s_pool[TABLE_SIZE];
179
180 static std::mutex s_mutex;
182 static thread_local MemElemPtr s_tp[TABLE_SIZE];
183#endif
184};
185
186}
Basic declarations, included by all source files.
Allocates memory in large chunks for better runtime.
static size_t memoryAllocatedInBlocks()
Returns the total amount of memory (in bytes) allocated from the system.
static void getGlobalFreeListSizes(std::vector< size_t > &sizes)
Reports the number of pooled memory chunks in the global free lists for each possible size up to TABL...
static void deallocateList(size_t nBytes, void *pHead, void *pTail)
Deallocate a complete list starting at pHead and ending at pTail.
static constexpr size_t TABLE_SIZE
static thread_local long long s_locallyAllocatedBytes
Holds the number of thread-locally allocated bytes for debugging.
static size_t memoryInGlobalFreeList()
Returns the total amount of memory (in bytes) available in the global free lists.
static void * allocate(size_t nBytes)
Allocates memory of size nBytes.
static constexpr size_t MIN_BYTES
static void deallocate(size_t nBytes, void *p)
Deallocates memory at address p which is of size nBytes.
static void defragThread()
Defragments the thread's free lists.
static constexpr size_t BLOCK_SIZE
static void getThreadFreeListSizes(std::vector< size_t > &sizes)
Reports the number of pooled memory chunks in the thread's free lists for each possible size up to TA...
static long long s_globallyAllocatedBytes
Holds the number of globally allocated bytes for debugging.
static PoolElement s_pool[TABLE_SIZE]
Contains allocated but free memory that may be used by all threads. Filled upon exiting a thread that...
static thread_local MemElemPtr s_tp[TABLE_SIZE]
Contains the allocated but free memory for a single thread.
static void cleanup()
Frees all allocated memory.
static void * fillPool(MemElemPtr &pFreeBytes, uint16_t nBytes)
static void defragGlobal()
Defragments the global free lists.
static int slicesPerBlock(uint16_t nBytes, int &nWords)
static MemElemPtr allocateBlock()
static bool checkSize(size_t nBytes)
Returns true iff allocate can be invoked with nBytes.
static size_t memoryInThreadFreeList()
Returns the total amount of memory (in bytes) available in the thread's free lists.
static void makeSlices(MemElemPtr p, int nWords, int nSlices)
static void flushPool()
Flushes all free but allocated bytes (s_tp) to the thread-global list (s_pool) of allocated bytes.
static int slicesPerBlock(uint16_t nBytes)
static BlockChain * s_blocks
Holds all allocated memory independently of whether it is cleared in chunks of size BLOCK_SIZE.
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
The namespace for all OGDF objects.
Basic memory element used to realize a linked list of deallocated memory segments.