-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManagment.cpp
More file actions
247 lines (218 loc) · 7.99 KB
/
MemoryManagment.cpp
File metadata and controls
247 lines (218 loc) · 7.99 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "MemoryManagment.h"
#include "Queue.h"
#include "MemoryModule.h"
#include "part.h"
MemoryManagmentUnit::~MemoryManagmentUnit() {
}
PartitionMemoryManagmentUnit::PartitionMemoryManagmentUnit(unsigned long _numOfClustersInPartition, MemoryInterface *_memoryModule) : memoryModule(_memoryModule), numOfClustersInPartition(_numOfClustersInPartition), isChanged(false) {
numOfClusters = (numOfClustersInPartition + ClusterSize - 1) / ClusterSize;
inMemory = -1;
bitVectorBuffer = nullptr;
InitializeCriticalSection(&criticalSection);
}
void PartitionMemoryManagmentUnit::flush() {
EnterCriticalSection(&criticalSection);
if (isChanged && bitVectorBuffer != nullptr) {
memoryModule->write(inMemory, bitVectorBuffer);
}
LeaveCriticalSection(&criticalSection);
}
PartitionMemoryManagmentUnit::~PartitionMemoryManagmentUnit() {
flush();
if (bitVectorBuffer != nullptr) {
delete[] bitVectorBuffer;
}
DeleteCriticalSection(&criticalSection);
}
void PartitionMemoryManagmentUnit::read(unsigned long clusterNo) {
if (inMemory != clusterNo) {
if (bitVectorBuffer == nullptr) {
bitVectorBuffer = new char[ClusterSize];
}
if (isChanged && bitVectorBuffer != nullptr) {
memoryModule->write(inMemory, bitVectorBuffer);
isChanged = false;
}
inMemory = clusterNo;
memoryModule->read(inMemory, bitVectorBuffer);
}
}
unsigned long PartitionMemoryManagmentUnit::findFreeCluster() {
unsigned long clusterNo = 0;
if (inMemory == -1) {
read(0);
}
for (int i = 0; i < numOfClusters; i++) {
int boundary = 0;
if (inMemory < numOfClusters || numOfClustersInPartition % ClusterSize == 0) {
boundary = ClusterSize;
} else {
boundary = numOfClustersInPartition % ClusterSize;
}
for (int j = 0; j < boundary; j++) {
if (inMemory == 0 && j < numOfClusters) continue;
if (utilities::getBitFromBitvector(bitVectorBuffer, j) == 0) {
clusterNo = inMemory * ClusterSize + j;
break;
}
}
if (clusterNo != 0) {
break;
}
read((inMemory + 1) % numOfClusters);
}
return clusterNo;
}
unsigned long PartitionMemoryManagmentUnit::allocate() {
EnterCriticalSection(&criticalSection);
unsigned long clusterNo = 0;
clusterNo = findFreeCluster();
if (clusterNo != 0) {
utilities::setBitInBitvector(bitVectorBuffer, clusterNo % ClusterSize, 1);
isChanged = true;
}
LeaveCriticalSection(&criticalSection);
return clusterNo;
}
void PartitionMemoryManagmentUnit::free(unsigned long clusterNo) {
EnterCriticalSection(&criticalSection);
read(clusterNo / ClusterSize);
utilities::setBitInBitvector(bitVectorBuffer, clusterNo % ClusterSize, 0);
LeaveCriticalSection(&criticalSection);
}
void PartitionMemoryManagmentUnit::format() {
if (bitVectorBuffer == nullptr) {
bitVectorBuffer = new char[ClusterSize];
}
memset(bitVectorBuffer, 0, ClusterSize);
for (int i = 1; i < numOfClusters; i++) {
memoryModule->write(i, bitVectorBuffer);
}
for (int i = 0; i < numOfClusters; i++) {
utilities::setBitInBitvector(bitVectorBuffer, i, 1);
}
memoryModule->write(0, bitVectorBuffer);
inMemory = -1;
isChanged = false;
}
const unsigned long FileMemoryManagmentUnit::MAX_FILE_CLUSTERS = ClusterSize / 8 + ClusterSize / 8 * ClusterSize / 4;
FileMemoryManagmentUnit::FileMemoryManagmentUnit(CacheMemory *_indexCache, MemoryManagmentUnit *_partitionMemoryManagment, unsigned long _numOfDataClusters, unsigned long _indexCluster) : indexCache(_indexCache), partitionMemoryManagmentUnit(_partitionMemoryManagment), numOfDataClusters(_numOfDataClusters), indexCluster(_indexCluster) {
numOfIndexClusters = numOfDataClusters == 0 ? 0 : numOfDataClusters <= 256 ? 1 : 2 + (numOfDataClusters - 256 - 1) / 512;//ako se fajl kreira prazan onda _indexCluster = 0
}
FileMemoryManagmentUnit::~FileMemoryManagmentUnit() {
}
void FileMemoryManagmentUnit::write(int clusterNo, int entry, unsigned long value) {
unsigned long indexClusterNo = 0;
if (clusterNo == 0) {
indexClusterNo = indexCluster;
} else {
char *indexClusterNoPointer = reinterpret_cast<char*>(&indexClusterNo);
for (int i = 0; i < sizeof(unsigned long); i++) {
indexCache->read(indexCluster * ClusterSize + (256 + clusterNo - 1) * sizeof(unsigned long) + i, indexClusterNoPointer + i);
}
}
unsigned long offset = indexClusterNo * ClusterSize + entry * sizeof(unsigned long);
char *valuePointer = reinterpret_cast<char*>(&value);
for (int i = 0; i < sizeof(unsigned long); i++) {
indexCache->write(offset + i, valuePointer + i);
}
}
unsigned long FileMemoryManagmentUnit::read(int clusterNo, int entry) {
unsigned long indexClusterNo = 0;
if (clusterNo == 0) {
indexClusterNo = indexCluster;
}
else {
char *indexClusterNoPointer = reinterpret_cast<char*>(&indexClusterNo);
for (int i = 0; i < sizeof(unsigned long); i++) {
indexCache->read(indexCluster * ClusterSize + (256 + clusterNo - 2) * sizeof(unsigned long) + i, indexClusterNoPointer + i);
}
}
unsigned long offset = indexClusterNo * ClusterSize + entry * sizeof(unsigned long);
unsigned long value = 0;
char *valuePointer = reinterpret_cast<char*>(&value);
for (int i = 0; i < sizeof(unsigned long); i++) {
indexCache->read(offset + i, valuePointer + i);
}
return value;
}
unsigned long FileMemoryManagmentUnit::allocateNewIndexCluster() {
unsigned long clusterNo = partitionMemoryManagmentUnit->allocate();
int ret = 0;
if (clusterNo != 0) {
if (numOfIndexClusters == 0) {//this first leve index cluster
indexCluster = clusterNo;
ret = 2;
} else {//this is second level index cluster and we need ot write that in index cluster
write(0, 256 + numOfIndexClusters - 1, clusterNo);
ret = 1;
}
char fillChar = 0;
for (int i = 0; i < ClusterSize; i++) {
indexCache->write(clusterNo * ClusterSize + i, &fillChar);
}
numOfIndexClusters++;
}
return ret;
}
void FileMemoryManagmentUnit::freeIndexCluster() {//frees last index cluster
unsigned long clusterNo = 0;
if (numOfIndexClusters > 1) {
clusterNo = read(0, 256 + numOfIndexClusters - 2);
write(0, 256 + numOfIndexClusters - 2, 0);
} else {
clusterNo = indexCluster;
indexCluster = 0;
}
numOfIndexClusters--;
partitionMemoryManagmentUnit->free(clusterNo);
}
unsigned long FileMemoryManagmentUnit::allocate() {
if (numOfDataClusters > MAX_FILE_CLUSTERS) {// file is at max size
return 0;
}
int ret = 0;
unsigned long newNumberOfDataClusters = numOfDataClusters + 1;
int currentNumberOfIndexCluster = newNumberOfDataClusters <= 256 ? 1 : 2 + (newNumberOfDataClusters - 256 - 1) / 512;
if (numOfIndexClusters < currentNumberOfIndexCluster) {
if (allocateNewIndexCluster() == 0) {
freeIndexCluster();
return 0;
}
ret = 2;
}
unsigned long clusterNo = partitionMemoryManagmentUnit->allocate();
if (clusterNo == 0) {
if (ret == 2) {
freeIndexCluster();
return 0;
}
} else if (ret == 0) {
ret = 1;
}
//numOfDataClusters++;
int indexClusterNo = numOfDataClusters < 256 ? 0 : (numOfDataClusters - 256) / 512 + 1;
int entry = numOfDataClusters < 256 ? numOfDataClusters : (numOfDataClusters - 256) % 512;
write(indexClusterNo, entry, clusterNo);
numOfDataClusters++;
return ret;
}
void FileMemoryManagmentUnit::free(unsigned long logClusterNo) {
if (numOfDataClusters == 0) {
return;
}
int indexClusterNo = logClusterNo < 256 ? 0 : (logClusterNo - 256) / 512 + 1;
int entry = logClusterNo < 256 ? logClusterNo : (logClusterNo - 256) % 512;
unsigned long phClusterNo = read(indexClusterNo, entry);
if (phClusterNo == 0) return;//directory
write(indexClusterNo, entry, 0);
partitionMemoryManagmentUnit->free(phClusterNo);
numOfDataClusters--;
int currentNumOfIndexClusters = numOfDataClusters == 0 ? 0 : numOfDataClusters <= 256 ? 1 : 2 + (numOfDataClusters - 256 - 1) / 512;
if (currentNumOfIndexClusters < numOfIndexClusters) {
freeIndexCluster();
}
}
unsigned long FileMemoryManagmentUnit::getIndexCluster() const {
return indexCluster;
}