Repository navigation
Expand file tree
/
Copy pathMemoryManager.cpp
More file actions
113 lines (90 loc) · 3.1 KB
/
Copy pathMemoryManager.cpp
File metadata and controls
113 lines (90 loc) · 3.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "MemoryManager.h"
#include <fstream>
#include <iostream>
MemoryManager::MemoryManager(int maxOverallMem, int memPerFrame, int memPerProc)
: maxOverallMem(maxOverallMem), memPerFrame(memPerFrame), memPerProc(memPerProc) {
// Initialize the new PagingAllocator here instead of using the old flat logic
allocator = std::make_unique<PagingAllocator>(maxOverallMem, memPerFrame);
}
int MemoryManager::allocate(int pid, size_t processSize) {
std::lock_guard<std::mutex> lock(mutex);
// Idempotent check: return success if already allocated
if (processBlocks.find(pid) != processBlocks.end()) {
return 1;
}
// Call the PagingAllocator to handle the memory distribution
void* handle = allocator->allocate(processSize);
if (!handle) {
return -1; // Allocation failed (memory full)
}
processBlocks[pid] = handle;
return 1;
}
void MemoryManager::deallocate(int pid) {
std::lock_guard<std::mutex> lock(mutex);
auto it = processBlocks.find(pid);
if (it != processBlocks.end()) {
allocator->deallocate(it->second);
processBlocks.erase(it);
}
}
bool MemoryManager::isAllocated(int pid) {
std::lock_guard<std::mutex> lock(mutex);
return processBlocks.find(pid) != processBlocks.end();
}
int MemoryManager::processCount() {
std::lock_guard<std::mutex> lock(mutex);
return processBlocks.size();
}
int MemoryManager::externalFragmentationBytes() {
return getMaximumSize() - getCurrentAllocatedSize();
}
void MemoryManager::writeSnapshot(const std::string& path) {
std::lock_guard<std::mutex> lock(mutex);
std::ofstream file(path);
if (file.is_open()) {
file << allocator->visualizeMemory();
}
}
size_t MemoryManager::getMaximumSize() const {
if (allocator) return allocator->getMaximumSize();
return 0;
}
size_t MemoryManager::getCurrentAllocatedSize() const {
if (allocator) return allocator->getCurrentAllocatedSize();
return 0;
}
size_t MemoryManager::getNumPagedIn() const {
if (allocator) return allocator->getNumPagedIn();
return 0;
}
size_t MemoryManager::getNumPagedOut() const {
if (allocator) return allocator->getNumPagedOut();
return 0;
}
size_t MemoryManager::getFrameCount() const {
return allocator ? allocator->getFrameCount() : 0;
}
bool MemoryManager::accessPage(int pid, size_t virtualAddress) {
std::lock_guard<std::mutex> lock(mutex);
auto it = processBlocks.find(pid);
if (it == processBlocks.end()) {
return false; // Process memory not allocated
}
// Convert the raw byte address into a page index
size_t pageIndex = virtualAddress / memPerFrame;
// Call it directly - no dynamic_cast needed!
if (allocator) {
return allocator->accessPage(it->second, pageIndex);
}
return true;
}
size_t MemoryManager::getProcessResidentMemory(int pid) const {
std::lock_guard<std::mutex> lock(mutex);
auto it = processBlocks.find(pid);
if (it == processBlocks.end()) return 0;
if (allocator) {
return allocator->getProcessResidentMemory(it->second);
}
return 0;
}