HAllocator
A Simple C++ Memory Allocator
Loading...
Searching...
No Matches
Block.hpp
Go to the documentation of this file.
1
9#pragma once
10#include <sys/mman.h>
11
12#include <algorithm>
13#include <cstddef>
14#include <fstream>
15
16#include "RBTreeDriver.hpp"
17
24#define REQUEST_MEMORY_VIA_MMAP(size) \
25 mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
26
33#define RELEASE_MEMORY_VIA_MUNMAP(ptr, size) munmap(ptr, size)
34
35namespace hh::halloc {
67
72#define MEMORY_NODE_SIZE sizeof(MemoryNode)
73
86class Block {
87 std::size_t size;
88 MemoryNode* head;
95 std::size_t get_actual_value(std::size_t value) const;
96
102 void mark_as_used(std::size_t& value) const;
103
109 void mark_as_free(std::size_t& value) const;
110
116 bool is_free(const std::size_t& value) const;
117
130 void shrink_then_align(MemoryNode* node, std::size_t bytes);
131
144 void coalesce_nodes(MemoryNode* node);
145
146public:
151 Block();
152
163 explicit Block(std::size_t bytes);
164
170 Block(Block&& other);
171
178 Block& operator=(Block&& other);
179
190 MemoryNode* best_fit(std::size_t bytes);
191
196 ~Block();
197
202 std::size_t get_size() const { return size; }
203
208 void* get_head() const { return head; }
209
223 void* allocate(std::size_t bytes, MemoryNode* node);
224
238 void deallocate(void* ptr, std::size_t bytes);
239
257 void log_block_state(std::ofstream& logfile) const {
258 ;
259 logfile << "Block State:\n";
260 logfile << "Address: " << this << "\n";
261 logfile << "Total Size: " << size << " bytes\n";
262
263 auto current = head;
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;
269
270 std::size_t i = 0;
271 while (current) {
272 logfile << "---------------- Node " << ++i << " ----------------\n";
273 std::size_t node_size = get_actual_value(current->value);
274 headers_used_space += MEMORY_NODE_SIZE;
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;
279 num_free_nodes++;
280 } else {
281 actual_used_space += node_size;
282 num_used_nodes++;
283 }
284 current = current->next;
285 }
286
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";
293 }
294};
295} // namespace hh::halloc
#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
Definition Block.hpp:35
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