-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.cpp
157 lines (140 loc) · 4.39 KB
/
Memory.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Author: Shania Dhani
* Date Modified May 12, 2020
*
* Memory controls all memory logic in the simulation including the allocation
* of and releasing of process memory. It also addresses memory holes. It follows
* a "First-Fit" approach to handling process memory allocation in "continguous memory."
*/
#include "Memory.hpp"
Memory::Memory() {}
Memory::Memory(const long long int &memory_size)
{
memory_size_ = memory_size;
free_memory_ = memory_size;
}
// sets size of RAM memory
void Memory::setMemorySize(const long long int &memory_size)
{
memory_size_ = memory_size;
}
// @return size of RAM memory
long long int Memory::getMemorySize() const
{
return memory_size_;
}
// consolidate consecutive memory holes into one
void Memory::consolidateMemoryChunks()
{
bool seenHole = false; /* tracks if seen a hole */
unsigned long long int hole = -1; /* tracks index of last hole */
for (unsigned long long int i = 0; i < continguous_memory_.size(); i++)
{
MemBlock &m = continguous_memory_[i];
if (!m.active() && !seenHole)
{
seenHole = true;
hole = i;
}
else if (seenHole && !m.active())
{
if (hole == i - 1)
{
continguous_memory_[hole].setEnd(m.end());
continguous_memory_[hole].setMemoryChunk(continguous_memory_[hole].memoryChunk() + m.memoryChunk());
continguous_memory_.erase(continguous_memory_.begin() + i);
}
else
{
hole = i;
}
}
if (m.start() >= m.end())
{
continguous_memory_.erase(continguous_memory_.begin() + i);
}
}
}
// @return true if add process to memory is successful
bool Memory::addProcessToMemory(const Process &p)
{
consolidateMemoryChunks(); // consolidate memory chunks
long long int p_size = p.getMemorySize();
if (free_memory_ >= p_size)
{
for (unsigned long long int i = 0; i < continguous_memory_.size(); i++)
{
MemBlock &m = continguous_memory_[i];
if (!m.active() && m.memoryChunk() >= p_size)
{
long long int new_end = 0;
if (i == 0)
{
new_end = (m.start() + p_size) - 1;
}
else
{
new_end = (m.start() + p_size) - 1;
}
MemBlock new_m = {m.start(), new_end, p.getPID(), p_size, true};
continguous_memory_[i].setStart(new_end + 1); // Shift start of memory hole to end of new process
continguous_memory_[i].setMemoryChunk(continguous_memory_[i].end() - new_end);
continguous_memory_.insert(continguous_memory_.begin() + i, new_m);
free_memory_ -= p_size; // allocate memory
return true;
}
}
/* Append to front of memory */
if (continguous_memory_.empty())
{
MemBlock new_m = {0, (p_size - 1), p.getPID(), p_size, true};
continguous_memory_.push_back(new_m);
free_memory_ -= p_size; // allocate memory
return true;
}
/* Append to the end of memory */
else if ((memory_size_ - continguous_memory_.back().end()) >= p_size)
{
long long int end = continguous_memory_.back().end(); // end of last process in memory
MemBlock new_m = {end + 1, (end + p_size), p.getPID(), p_size, true};
continguous_memory_.push_back(new_m);
free_memory_ -= p_size; // allocate memory
return true;
}
}
std::cout << "Not enough memory to add new process." << std::endl;
return false;
}
// @return true if remove process from memory is successful
bool Memory::removeProcessFromMemory(const Process &p)
{
for (unsigned long long int i = 0; i < continguous_memory_.size(); i++)
{
if (continguous_memory_[i].pid() == p.getPID())
{
continguous_memory_[i].setActive(false); /* Process Terminated */
free_memory_ += continguous_memory_[i].memoryChunk();
consolidateMemoryChunks(); // consolidate memory chunks
return true;
}
}
return false; /* process does not exist */
}
// helper print PID function
std::string printPIDM(const long long int &pid)
{
return "P" + std::to_string(pid) + " ";
}
// print out memory snapshot
void Memory::printMemory()
{
consolidateMemoryChunks(); // consolidate memory chunks
if (continguous_memory_.empty())
{
std::cout << " 0-" << std::to_string(memory_size_) << " EMPTY" << std::endl;
}
for (auto &m : continguous_memory_)
{
std::cout << " " << m.start() << "-" << m.end() << " " << (m.active() ? printPIDM(m.pid()) : "EMPTY") << std::endl;
}
}