-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
343 lines (302 loc) · 9.01 KB
/
mainwindow.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->la->setText("Address \t Size \n");
//显示内存分配情况图
scene =new QGraphicsScene();
ui->view->setScene(scene);
showhit();
}
MainWindow::~MainWindow()
{
delete ui;
}
int MainWindow::id=0; //进程id初始化为0
//设置内存区块提示颜色
void MainWindow::showhit()
{
QBrush a(Qt::blue);
QBrush b(Qt::green);
QBrush c(Qt::red);
QGraphicsRectItem *l1 = new QGraphicsRectItem();
QGraphicsRectItem *l2= new QGraphicsRectItem();
QGraphicsRectItem *l3 = new QGraphicsRectItem();
QGraphicsTextItem *p1 = new QGraphicsTextItem();
QGraphicsTextItem *p2 = new QGraphicsTextItem();
QGraphicsTextItem *p3 = new QGraphicsTextItem();
p1->setPos(15,0-5);
p2->setPos(15,15-5);
p3->setPos(15,30-5);
p1->setPlainText("blocked");
p2->setPlainText("process");
p3->setPlainText("free holes");
l1->setRect(0,0,10,10);
l1->setBrush(a);
l2->setRect(0,15,10,10);
l2->setBrush(b);
l3->setRect(0,30,10,10);
l3->setBrush(c);
QGraphicsScene *s=new QGraphicsScene();
ui->hit->setScene(s);
s->addItem(l1);
s->addItem(l2);
s->addItem(l3);
s->addItem(p1);
s->addItem(p2);
s->addItem(p3);
}
void MainWindow::on_Show_clicked()
{
updateview();
ui->view->show();
}
void MainWindow::on_Insert_clicked()
{
//设置内存地址和大小
double add= ui->memoryAddress->text().toDouble();
double siz = ui->memorySize->text().toDouble();
//内存加入空闲区
FreeSpace.push_back(qMakePair(add,siz));
//设置文字
QString temp = ui->la->text();
temp.append(ui->memoryAddress->text()+"\t"+ui->memorySize->text()+"\n");
ui->la->setText(temp);
ui->memoryAddress->setText("");
ui->memorySize->setText("");
}
void MainWindow::updateview()
{
//删除之前的图
QList<QGraphicsItem*> items = scene->items();
for (int i = 0; i < items.size(); i++) {
scene->removeItem(items[i]);
delete items[i];
}
qSort(FreeSpace.begin(),FreeSpace.end());
for(QVector < QPair<double, double> >::iterator i = FreeSpace.begin(); i != FreeSpace.end(); i++)
{
//空闲区上色
QGraphicsRectItem *fre = new QGraphicsRectItem();
fre->setRect(i->first,0,i->second,100);
QBrush fr(Qt::red);
fre->setBrush(fr);
scene->addItem(fre);
//锁定区上色
QVector <QPair<double, double> >::iterator k = i;
k--;
if(i != FreeSpace.begin() && (i->first > (k->first + k->second)))
{
//选择蓝色
QBrush a(Qt::blue);
QGraphicsRectItem * blo = new QGraphicsRectItem();
blo->setRect(k->first + k->second,0,i->first - k->first - k->second,100);
blo->setBrush(a);
scene->addItem((blo));
}
//设置文字显示地址
QGraphicsTextItem * ft = new QGraphicsTextItem();
QGraphicsTextItem * bt = new QGraphicsTextItem();
ft->setRotation(-90);
bt->setRotation(-90);
//计算位置
QString addt = QString::number(i->first);
QString st = QString::number(i->second);
if(i->second != 0)
{
ft->setPos(i->first + i->second/2 - 10,65);
ft->setPlainText(st + "k");
scene->addItem(ft);
bt->setPos(i->first,150);
bt->setPlainText(addt + "k");
scene->addItem(bt);
}
}
//设置进程占用地址的颜色和文字
for(QVector<process>::iterator i = AllocateSpace.begin();i != AllocateSpace.end();i++)
{
if(i->getSize() != 0)
{
QGraphicsRectItem * pro = new QGraphicsRectItem();
QBrush pr(Qt::green);
QGraphicsTextItem * processName = new QGraphicsTextItem();
QGraphicsTextItem * processSize = new QGraphicsTextItem();
QGraphicsTextItem * processadd = new QGraphicsTextItem();
//文字转向
processName->setRotation(-90);
processSize->setRotation(-90);
processadd->setRotation(-90);
pro->setRect(i->getAddress(),0,i->getSize(),100);
double mid = i->getAddress() + i->getSize()/2 -10;
processName->setPos(mid,70);
processSize->setPos(mid,40);
processadd->setPos(i->getAddress(),150);
QString s1 = QString::number(i->getId());
processName->setPlainText("p" + s1);
QString s2 = QString::number(i->getSize());
QString s3 = QString::number(i->getAddress());
processSize->setPlainText(s2 + "k");
processadd->setPlainText(s3 + "k");
pro->setBrush(pr);
scene->addItem(pro);
scene->addItem(processName);
scene->addItem(processSize);
scene->addItem(processadd);
}
}
}
void MainWindow::firstfit(double pro_mem)
{
double tempAdd;
double siz;
int flag=0; //分配成功标志
for(QVector < QPair<double,double> >::iterator i=FreeSpace.begin();i!=FreeSpace.end();i++)
{
if(i->second >= pro_mem)
{
tempAdd = i->first;
siz = pro_mem;
i->first += pro_mem;
i->second -=pro_mem;
flag=1;
break;
}
}
if(flag==1){
//分配成功,设置进程
process p(tempAdd,siz,id);
id++;
AllocateSpace.push_back(p);
updateview();
}
else
{
//分配失败显示提示框
QMessageBox m ;
m.setText("memoery doesnot have space");
m.exec();
}
}
//从小到大排序
bool compare(const QPair<double, double>&i, const QPair<double, double>&j)
{
return i.second < j.second;
}
//从大到小排序
bool compare_2(const QPair<double, double>&i, const QPair<double, double>&j)
{
return i.second > j.second;
}
void MainWindow::bestfit(double s)
{
qSort(FreeSpace.begin(),FreeSpace.end(),compare);
firstfit(s);
}
void MainWindow::worstfit(double s)
{
qSort(FreeSpace.begin(),FreeSpace.end(),compare_2);
firstfit(s);
}
void MainWindow::on_Add_clicked()
{
double s = ui->processSize->text().toDouble();
ui->processSize->setText("");
if(ui->firstfit->isChecked())
{
qSort(FreeSpace.begin(),FreeSpace.end());
firstfit(s);
}
else if(ui->bestfit->isChecked())
{
bestfit(s);
}
else if(ui->worstfit->isChecked())
{
worstfit(s);
}
else
{
QMessageBox m ;
m.setText("You should determine algorithm");
m.exec();
}
}
void MainWindow::on_Delete_clicked()
{
//进程id
int proid = ui->deallocation_id->text().toInt();
ui->deallocation_id->setText("");
//进程号索引
int x = 0;
//进程号是否存在标志
bool check;
//查找进程号是否存在
for(QVector<process>::iterator i = AllocateSpace.begin();i != AllocateSpace.end();i++)
{
if(i->getId() == proid)
{
check = TRUE;
break;
}
else
{
check = FALSE;
x++;
}
}
//如果不存在,弹出消息框提示
if(!check)
{
QMessageBox m;
m.setText("ID doesn't exist");
m.exec();
return;
}
//删除内存时确定是否需要加入新空闲内存
int flag = 0;
for(QVector < QPair<double,double> >::iterator i = FreeSpace.begin();i != FreeSpace.end();i++)
{
//如果进程内存空间之前和之后都是空闲空间,则不需要建立新空间
if(i != FreeSpace.end())
{
QVector<QPair<double, double> >::iterator temp = i;
temp++;
if(((i->first + i->second) == AllocateSpace[x].getAddress()) && (temp->first == AllocateSpace[x].getAddress() + AllocateSpace[x].getSize()))
{
flag = 1;
i->second += temp->second + AllocateSpace[x].getSize();
FreeSpace.erase(temp);
break;
}
}
//如果进程内存之后是空闲空间
if(i->first == AllocateSpace[x].getAddress() + AllocateSpace[x].getSize())
{
flag = 1;
i->first = AllocateSpace[x].getAddress();
i->second += AllocateSpace[x].getSize();
break;
}
//如果进程内存前后是空闲空间
if((i->first + i->second) == AllocateSpace[x].getAddress())
{
flag = 1;
i->second += AllocateSpace[x].getSize();
break;
}
}
//重新建立内存空间
if(flag == 0)
{
double newAddress = AllocateSpace[x].getAddress();
double newSize = AllocateSpace[x].getSize();
FreeSpace.push_back(qMakePair(newAddress,newSize));
qSort(FreeSpace.begin(),FreeSpace.end());
}
//删除占用的内存空间
AllocateSpace.remove(x);
updateview();
}