-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManager.java
More file actions
146 lines (132 loc) · 4.86 KB
/
MemoryManager.java
File metadata and controls
146 lines (132 loc) · 4.86 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
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
import java.util.ArrayList;
import java.util.List;
public class MemoryManager {
private final int totalSize;
private final List<MemoryBlock> allocatedBlocks = new ArrayList<>();
private final List<MemoryBlock> freeBlocks = new ArrayList<>();
public MemoryManager(int size) {
this.totalSize = size;
this.freeBlocks.add(new MemoryBlock(-1, size, 0)); // Initial free block
}
public boolean allocateMemory(int processId, int size, String strategy) {
if (size <= 0 || processId <= 0) {
return false; // Invalid input
}
// Check if there's enough free memory available
for (MemoryBlock block : freeBlocks) {
if (block.size >= size) {
// Allocate memory based on strategy
switch (strategy.toLowerCase()) {
case "first":
return allocateFirstFit(processId, size, block);
case "best":
return allocateBestFit(processId, size);
case "worst":
return allocateWorstFit(processId, size);
default:
return false; // Invalid strategy
}
}
}
return false; // Not enough free memory
}
private boolean allocateFirstFit(int processId, int size, MemoryBlock block) {
int start = block.address;
if (size <= block.size) {
allocatedBlocks.add(new MemoryBlock(processId, size, start));
block.size -= size;
block.address += size;
if (block.size == 0) {
freeBlocks.remove(block);
}
return true;
}
return false;
}
private boolean allocateBestFit(int processId, int size) {
MemoryBlock bestBlock = null;
for (MemoryBlock block : freeBlocks) {
if (block.size >= size && (bestBlock == null || block.size < bestBlock.size)) {
bestBlock = block;
}
}
if (bestBlock != null) {
return allocateFirstFit(processId, size, bestBlock);
}
return false;
}
private boolean allocateWorstFit(int processId, int size) {
MemoryBlock worstBlock = null;
for (MemoryBlock block : freeBlocks) {
if (block.size >= size && (worstBlock == null || block.size > worstBlock.size)) {
worstBlock = block;
}
}
if (worstBlock != null) {
return allocateFirstFit(processId, size, worstBlock);
}
return false;
}
public void deallocateMemory(int processId) {
List<MemoryBlock> toRemove = new ArrayList<>();
for (MemoryBlock block : allocatedBlocks) {
if (block.processId == processId) {
freeBlocks.add(new MemoryBlock(-1, block.size, block.address));
toRemove.add(block);
}
}
allocatedBlocks.removeAll(toRemove);
mergeFreeBlocks();
}
private void mergeFreeBlocks() {
freeBlocks.sort((a, b) -> Integer.compare(a.address, b.address));
List<MemoryBlock> mergedBlocks = new ArrayList<>();
MemoryBlock current = null;
for (MemoryBlock block : freeBlocks) {
if (current == null) {
current = block;
} else if (current.address + current.size == block.address) {
current.size += block.size;
} else {
mergedBlocks.add(current);
current = block;
}
}
if (current != null) {
mergedBlocks.add(current);
}
freeBlocks.clear();
freeBlocks.addAll(mergedBlocks);
}
public String getMemoryStatus() {
int allocatedSpace = 0;
int freeSpace = totalSize;
// Calculate allocated space
for (MemoryBlock block : allocatedBlocks) {
allocatedSpace += block.size;
freeSpace -= block.size;
}
// Format output
StringBuilder sb = new StringBuilder();
sb.append("<html><body>");
sb.append("<b>Initialized Space:</b> ").append(totalSize).append(" units<br>");
sb.append("<b>Allocated Blocks:</b><br>");
for (MemoryBlock block : allocatedBlocks) {
sb.append(String.format("Process ID %d: %d units at address %d<br>",
block.processId, block.size, block.address));
}
sb.append("<br><b>Free Space:</b> ").append(freeSpace).append(" units<br>");
sb.append("</body></html>");
return sb.toString();
}
private static class MemoryBlock {
int processId;
int size;
int address;
MemoryBlock(int processId, int size, int address) {
this.processId = processId;
this.size = size;
this.address = address;
}
}
}