-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitMap.cc
More file actions
43 lines (32 loc) · 990 Bytes
/
bitMap.cc
File metadata and controls
43 lines (32 loc) · 990 Bytes
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
#include "bitMap.h"
#include "Exception.h"
BitMap::BitMap(int size): size{size}, free{size} {
bitmap = vector<bool>(size, false);
}
// Returns a list of memory of size n that are now occupied.
vector<int> BitMap::getBlock(int blocks){
// If there are no more room on the disk, then throw exception to caller.
if (blocks > free) throw NoRoomException{};
// Iterate through the bitmap to find free memory.
vector<int> allocatedMem; // Contains the addresses of the blocks.
for (int i = 0; i < size; ++i){
if (!bitmap[i]){
bitmap[i] = true;
allocatedMem.push_back(i);
if (allocatedMem.size() == blocks) break;
}
}
// Subtract the memory used from the total available memory.
free -= blocks;
return allocatedMem;
}
void BitMap::freeBlock(vector<int> blocks){
free += blocks.size();
// Free each of the elements in the array as indices
for (auto n : blocks){
bitmap[n] = false;
}
}
int BitMap::availableSpace() noexcept{
return free;
}