-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory_allocator.cpp
More file actions
225 lines (192 loc) · 7.56 KB
/
Memory_allocator.cpp
File metadata and controls
225 lines (192 loc) · 7.56 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
class Memory_Allocator{
public:
Memory_Allocator(int size):memorySize(size), allocator_on(true) { //constructor function
//memory.resize(memorySize,0);
freeBlocks.push_back({0, memorySize});
}
// function to initialize the allocator
void run(){
while(allocator_on){
displayMenu();
processMenuChoice();
}
}
private:
struct MemoryBlock {
int start; // start address of the block
int size;
};
bool allocator_on;
int memorySize; // total memory size for allocating blocks
//std::vector<char>memory;
std::vector<MemoryBlock>freeBlocks; // vector to store free blocks
std::vector<MemoryBlock>allocatedBlocks; //vector to store allocated blocks
void displayMenu() {
cout<<"\n//-----Memory allocator:-----\\ \n";
cout<<"a) Allocate a memory block\n";
cout<<"b) Deallocate a memory block\n";
cout<<"c) Display the allocation status\n";
//cout<<"d) Perform defragmentation\n";
cout<<"e) Exit the allocator\n";
cout<<"Enter your choice(in terms of a,b,c,e): ";
}
void processMenuChoice() {
char choice;
cin>>choice;
clearInputBuffer();
switch(choice){
case 'a':
allocateMemory();
break;
case 'b':
deallocateMemory();
break;
case 'c':
displayMemoryStatus();
break;
/*case 'd':
defragmentMemory();
break;*/
case 'e':
allocator_on = false;
cout<<"\nExiting the program.\n";
break;
default:
cout<<"Invalid choice. Please try again.\n";
break;
}
}
void allocateMemory() {
defragmentMemory();
cout<<"\nAllocation of memory:\n";
int blockSize; //block size
cout<<"Enter block size: ";
cin>>blockSize;
clearInputBuffer();
if(blockSize!= 0){
int blockIndex=findFreeBlock(blockSize); //returns the index of free block
if(blockIndex!=-1){
allocateBlock(blockIndex, blockSize); // allocate the block based on memory and index of free block
cout<<"Memory block allocated successfully.\n";
}
else
cout<<"Cannot allocate memory block of size "<< blockSize <<". Not enough free memory.\n"; //free memory not found
}
}
int findFreeBlock(int blockSize) { //returns the index of the free block
for (int i=0;i<freeBlocks.size();i++) {
if (freeBlocks[i].size>=blockSize) {
return i;
}
}
return -1;
}
void allocateBlock(int blockIndex, int blockSize) {
MemoryBlock& block = freeBlocks[blockIndex]; //copy the free block details
MemoryBlock allocatedBlock; //creating a block to allocate
allocatedBlock.start=block.start; //address intialization
allocatedBlock.size=blockSize; //allocating required memory
allocatedBlocks.push_back(allocatedBlock); //pushing this block to the vector of allocated blocks
if(block.size==blockSize) // free block size is equal to required memory
freeBlocks.erase(freeBlocks.begin()+blockIndex);
else // freeblock size > required block size
{
block.start+=blockSize;
block.size-=blockSize;
}
}
void deallocateMemory() {
cout<<"\nDeallocate memory selected.\n";
if(allocatedBlocks.empty()){ //if no memory is allocated
cout<<"No memory blocks to deallocate.\n";
return;
}
int blockIndex;
cout<<"Enter index of the block to deallocate(zero based indexing): "; //index of the block to deallocate
cin>>blockIndex;
clearInputBuffer(); // Clear input buffer
if(blockIndex>= 0 && blockIndex<allocatedBlocks.size()) { // valid block index condition
deallocateBlock(blockIndex); //deallocation function called
cout<<"Memory block deallocated successfully.\n";
}
else
cout<<"Invalid block index.\n";
}
void deallocateBlock(int blockIndex) {
MemoryBlock& block=allocatedBlocks[blockIndex]; //copying the block to be deallocated
freeBlocks.push_back(block);
allocatedBlocks.erase(allocatedBlocks.begin() + blockIndex); //deallocate the block
mergeFreeBlocks(); //merging the contiguous free blocks
}
void mergeFreeBlocks() {
std::sort(freeBlocks.begin(), freeBlocks.end(), [](const MemoryBlock& a, const MemoryBlock& b) {
return a.start<b.start;
}); //sorting as blocks are pushed to the back of the vector
for (int i=1;i<freeBlocks.size();i++) {
MemoryBlock& currentBlock=freeBlocks[i];
MemoryBlock& previousBlock=freeBlocks[i - 1];
if (previousBlock.start+previousBlock.size==currentBlock.start) {
previousBlock.size+=currentBlock.size;
freeBlocks.erase(freeBlocks.begin()+i);
i--;
}
}
}
void displayMemoryStatus() {
cout<<"\nMemory allocation status selected.\n";
if(memorySize==0) //no allocation
cout<<"No memory has been allocated.\n";
else
{
cout<<"Memory Size: "<<memorySize<<"\n";
cout<<"Number of Allocated Blocks: "<<allocatedBlocks.size()<<"\n";
for (int i=0;i<allocatedBlocks.size();i++) //print allocated blocks
{
const MemoryBlock& block=allocatedBlocks[i];
cout<<"Block "<<(i+1)<< " Size: "<<block.size<<", Start Address: "<<block.start<<", Index: "<< i <<"\n";
}
cout<<"Number of Free Blocks/holes: "<<freeBlocks.size()<<"\n";
if(freeBlocks.size()!=0){
for(int i=0;i<freeBlocks.size();i++){
const MemoryBlock& block=freeBlocks[i];
cout<<"Free Block "<<(i + 1)<<" Size: "<<block.size<<", Start Address: "<<block.start<<"\n";
}
}
}
}
void defragmentMemory(){
// Sorting allocated blocks by the means of their start address
std::sort(allocatedBlocks.begin(),allocatedBlocks.end(),[](const MemoryBlock& a, const MemoryBlock& b) {
return a.start < b.start;
});
//Adjusting/PLACING the allocated blocks based on their start address
int currentAddress=0;
for(auto& block:allocatedBlocks){
block.start=currentAddress;
currentAddress+=block.size;
}
// updating the freeblock status
freeBlocks.clear();
MemoryBlock freeBlock;
freeBlock.start=currentAddress;
freeBlock.size=memorySize-currentAddress;
freeBlocks.push_back(freeBlock);
}
// function to clear input buffer
void clearInputBuffer() {
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
};
int main() {
int memorySize;
cout<<"Enter the total memory size: ";
cin>>memorySize;
fflush(stdin); // Clear input buffer
Memory_Allocator Memory_Allocator(memorySize); //creating an object and specifying the total memory size
Memory_Allocator.run(); //calling or running the allocator.
return 0;
}