libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
mempool.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstddef>
11#include <algorithm>
13#include <promeki/core/mutex.h>
14#include <promeki/core/string.h>
15#include <promeki/core/map.h>
16#include <promeki/core/set.h>
17
19
30class MemPool {
31 public:
35 struct Stats {
36 size_t totalFree;
37 size_t totalUsed;
41 };
42
46 class Block {
47 public:
48 bool allocated = false;
50 size_t size = 0;
51 size_t alignment = 1;
52
54 bool operator<(const Block &other) const {
55 return address < other.address;
56 }
57
63 uintptr_t alignedAddress(size_t align) const {
64 uintptr_t _align = align - 1;
65 return (address + _align) & ~(_align);
66 }
67
75
81 size_t padding(size_t align) const {
82 return align > 1 ? alignedAddress(align) - address : 0;
83 }
84
89 size_t padding() const {
90 return padding(alignment);
91 }
92
98 bool follows(const Block &block) const {
99 return block.address + block.size == address;
100 }
101
102 };
103
109 static bool isValidAlignment(size_t val) {
110 return (val > 0) && !(val & (val - 1));
111 }
112
115
118
125
131 void addRegion(void *startingAddress, size_t size) {
132 addRegion(reinterpret_cast<uintptr_t>(startingAddress), size);
133 return;
134 }
135
137 const String &name() const { return _name; }
138
143 void setName(const String &val) {
144 _name = val;
145 return;
146 }
147
152 Stats stats() const;
153
159
161 void dump() const;
162
169 void *allocate(size_t size, size_t alignment = 1);
170
178 void free(void* ptr);
179
180 private:
181 String _name;
182 mutable Mutex _mutex;
183 BlockSet _freeBlocks;
184 BlockMap _allocatedBlocks;
185};
186
188
Dynamic array container wrapping std::vector.
Definition list.h:40
size_t size() const noexcept
Returns the number of elements in the list.
Definition list.h:301
Represents a contiguous region of memory in the pool.
Definition mempool.h:46
size_t size
Size in bytes.
Definition mempool.h:50
bool operator<(const Block &other) const
Orders blocks by address for sorted storage.
Definition mempool.h:54
uintptr_t alignedAddress(size_t align) const
Returns the address aligned to the given alignment.
Definition mempool.h:63
size_t padding(size_t align) const
Returns padding bytes needed to reach the given alignment.
Definition mempool.h:81
bool allocated
true if this block is allocated.
Definition mempool.h:48
size_t alignment
Alignment requirement.
Definition mempool.h:51
uintptr_t alignedAddress() const
Returns the address aligned to this block's alignment.
Definition mempool.h:72
bool follows(const Block &block) const
Returns true if this block immediately follows another.
Definition mempool.h:98
intptr_t address
Starting address of the block.
Definition mempool.h:49
size_t padding() const
Returns padding bytes needed for this block's alignment.
Definition mempool.h:89
Thread-safe memory pool allocator with external metadata.
Definition mempool.h:30
void addRegion(void *startingAddress, size_t size)
Adds a contiguous memory region to the pool.
Definition mempool.h:131
void free(void *ptr)
Frees a previously allocated block back to the pool.
const String & name() const
Returns the name of this memory pool.
Definition mempool.h:137
void dump() const
Dumps the memory map to the logger for debugging.
static bool isValidAlignment(size_t val)
Returns true if the given value is a valid alignment (power of two).
Definition mempool.h:109
BlockSet memoryMap() const
Returns a combined set of all free and allocated blocks.
void setName(const String &val)
Sets the name of this memory pool.
Definition mempool.h:143
MemPool()
Constructs an empty memory pool with a default hex name.
void * allocate(size_t size, size_t alignment=1)
Allocates a block from the pool.
void addRegion(uintptr_t startingAddress, size_t size)
Adds a contiguous memory region to the pool.
Stats stats() const
Returns current pool statistics.
Mutual exclusion lock wrapping std::mutex.
Definition mutex.h:33
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
Statistics about the current state of the memory pool.
Definition mempool.h:35
size_t totalUsed
Total bytes currently allocated.
Definition mempool.h:37
size_t totalFree
Total bytes available for allocation.
Definition mempool.h:36
size_t numFreeBlocks
Number of free block regions.
Definition mempool.h:38
size_t largestFreeBlock
Size in bytes of the largest free block.
Definition mempool.h:40
size_t numAllocatedBlocks
Number of allocated block regions.
Definition mempool.h:39