-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.cpp
More file actions
64 lines (55 loc) · 2 KB
/
Copy pathmanager.cpp
File metadata and controls
64 lines (55 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @file manager.cpp
* @brief Base MemoryManager implementation providing block coalescing / deallocation.
*
* CONCEPT:
* Manages a dynamic linked list / vector of contiguous memory blocks.
* Deallocation must handle COALESCING (merging adjacent free memory blocks):
* - If block i is freed and block i-1 is also free -> Merge (i-1) and i.
* - If block i is freed and block i+1 is also free -> Merge i and (i+1).
* - This prevents External Fragmentation from accumulating unnecessarily.
*/
#include "memory_manager/manager.h"
MemoryManager::MemoryManager(int totalMemory) : totalMemory(totalMemory)
{
// Initialize system memory as one single large unallocated block
blocks.emplace_back(0, totalMemory);
}
const std::vector<Memory> &MemoryManager::getBlocks() const
{
return blocks;
}
/**
* @brief Deallocates memory owned by process PID and merges adjacent free blocks.
*
* @param pid Process ID to deallocate.
* @return true if process was found and freed, false otherwise.
*/
bool MemoryManager::deallocate(int pid)
{
for (size_t i = 0; i < blocks.size(); i++)
{
Memory &block = blocks[i];
if (block.getIsAllocated() && block.getPid() == pid)
{
// Mark block as free
block.setIsAllocated(false);
block.setPid(-1);
// COALESCING STEP 1: Merge with previous block if it is free
if (i > 0 && !blocks[i - 1].getIsAllocated())
{
blocks[i - 1].setSize(blocks[i - 1].getSize() + block.getSize());
blocks.erase(blocks.begin() + i);
i--; // Adjust index after erase
}
// COALESCING STEP 2: Merge with next block if it is free
if (i + 1 < blocks.size() && !blocks[i + 1].getIsAllocated())
{
blocks[i].setSize(blocks[i].getSize() + blocks[i + 1].getSize());
blocks.erase(blocks.begin() + i + 1);
}
return true;
}
}
return false;
}