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/System.h>
36
37#ifndef OGDF_MEMORY_POOL_NTS
38# include <mutex>
39#endif
40
41namespace ogdf {
42
44
57 struct MemElem {
59 };
60
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
70public:
72
74
76 static OGDF_EXPORT void cleanup();
77
79 static OGDF_EXPORT bool checkSize(size_t nBytes) { return nBytes < TABLE_SIZE; }
80
82 static OGDF_EXPORT void* allocate(size_t nBytes);
83
85 static OGDF_EXPORT void deallocate(size_t nBytes, void* p);
86
88
93 static OGDF_EXPORT void deallocateList(size_t nBytes, void* pHead, void* pTail);
94
96 static OGDF_EXPORT void flushPool();
97
100
103
106
114 static OGDF_EXPORT void defrag();
115
116private:
117 static inline void enterCS() {
118#ifndef OGDF_MEMORY_POOL_NTS
119 s_mutex.lock();
120#endif
121 }
122
123 static inline void leaveCS() {
124#ifndef OGDF_MEMORY_POOL_NTS
125 s_mutex.unlock();
126#endif
127 }
128
130 int nWords;
132 }
133
138
140
142 static void makeSlices(MemElemPtr p, int nWords, int nSlices);
143
145
149
152
153#ifdef OGDF_DEBUG
155 static long long s_globallyAllocatedBytes;
157 static thread_local long long s_locallyAllocatedBytes;
158#endif
159
160#ifdef OGDF_MEMORY_POOL_NTS
161 static MemElemPtr s_tp[TABLE_SIZE];
162#else
163 static std::mutex s_mutex;
165 static thread_local MemElemPtr s_tp[TABLE_SIZE];
166#endif
167};
168
169}
Decalration of System class which provides unified access to system information.
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 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 size_t unguardedMemGlobalFreelist()
static size_t memoryInGlobalFreeList()
Returns the total amount of memory (in bytes) available in the global free lists.
static void defrag()
Defragments 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 constexpr size_t BLOCK_SIZE
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 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 DLL.
Definition config.h:101
#define OGDF_SIZEOF_POINTER
The size of a pointer.
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.
Basic memory element used to realize a linked list of deallocated memory segments.