24#define REQUEST_MEMORY_VIA_MMAP(size) \
25 mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
33#define RELEASE_MEMORY_VIA_MUNMAP(ptr, size) munmap(ptr, size)
72#define MEMORY_NODE_SIZE sizeof(MemoryNode)
95 std::size_t get_actual_value(std::size_t value)
const;
102 void mark_as_used(std::size_t& value)
const;
109 void mark_as_free(std::size_t& value)
const;
116 bool is_free(
const std::size_t& value)
const;
130 void shrink_then_align(
MemoryNode* node, std::size_t bytes);
163 explicit Block(std::size_t bytes);
238 void deallocate(
void* ptr, std::size_t bytes);
259 logfile <<
"Block State:\n";
260 logfile <<
"Address: " <<
this <<
"\n";
261 logfile <<
"Total Size: " << size <<
" bytes\n";
264 std::size_t free_space = 0;
265 std::size_t actual_used_space = 0;
266 std::size_t num_free_nodes = 0;
267 std::size_t num_used_nodes = 0;
268 std::size_t headers_used_space = 0;
272 logfile <<
"---------------- Node " << ++i <<
" ----------------\n";
275 logfile <<
"Node Address: " << current <<
" | Size: " << node_size <<
" bytes"
276 <<
" | Status: " << (is_free(current->value) ?
"Free" :
"Used") <<
"\n";
277 if (is_free(current->value)) {
278 free_space += node_size;
281 actual_used_space += node_size;
284 current = current->next;
287 logfile <<
"-------------------------------------------------------\n";
288 logfile <<
"Free Space: " << free_space <<
" bytes\n";
289 logfile <<
"Actual Used Space: " << actual_used_space <<
" bytes\n";
290 logfile <<
"Headers Used Space: " << headers_used_space <<
" bytes\n";
291 logfile <<
"Number of Free Nodes: " << num_free_nodes <<
"\n";
292 logfile <<
"Number of Used Nodes: " << num_used_nodes <<
"\n";
#define MEMORY_NODE_SIZE
Size of the MemoryNode metadata structure.
Definition Block.hpp:72
Template wrapper class for Red-Black tree operations.
Manages a contiguous memory block with RB-tree based allocation.
Definition Block.hpp:86
void * allocate(std::size_t bytes, MemoryNode *node)
Allocates memory from a specific node.
Definition Block.cpp:110
void log_block_state(std::ofstream &logfile) const
Logs the current state of the block to a file.
Definition Block.hpp:257
~Block()
Destructor - releases memory back to OS.
Definition Block.cpp:302
Block & operator=(Block &&other)
Move assignment operator.
Definition Block.cpp:72
std::size_t get_size() const
Gets total block size.
Definition Block.hpp:202
void * get_head() const
Gets pointer to the head node.
Definition Block.hpp:208
Block()
Default constructor - creates invalid block.
Definition Block.cpp:38
void deallocate(void *ptr, std::size_t bytes)
Deallocates previously allocated memory.
Definition Block.cpp:144
MemoryNode * best_fit(std::size_t bytes)
Finds best-fit free node for allocation.
Definition Block.cpp:84
Template wrapper for managing a Red-Black tree.
Definition RBTreeDriver.hpp:28
std::size_t get_actual_value(std::size_t value)
Helper function to extract actual size from encoded value.
Definition BlocksContainer.hpp:123
Node structure for both Red-Black tree and doubly-linked list.
Definition Block.hpp:49
MemoryNode * parent
Parent node in Red-Black tree.
Definition Block.hpp:52
MemoryNode * next
Next node in memory sequence (doubly-linked list)
Definition Block.hpp:64
MemoryNode * right
Right child in Red-Black tree.
Definition Block.hpp:51
MemoryNode * prev
Previous node in memory sequence (doubly-linked list)
Definition Block.hpp:65
MemoryNode * left
Left child in Red-Black tree.
Definition Block.hpp:50
std::size_t value
Encoded value containing size, status, and color.
Definition Block.hpp:62