#include <iostream>
#include <cstddef>
using namespace std;
// 定义分区描述器
struct Node {
int addr; // 分区首地址
int size; // 分区大小
Node* next; // 指向下一个分区的指针
};
// 定义空闲区队列
struct FreeList {
Node* head; // 空闲区队列首指针
Node* assign; // 指向申请的内存分区node结构的指针
};
// 检查指定的释放块是否合法
bool check(int addr, int size) {
return true;
}
// 实现First Fit Algorithm
Node* firstFitAssignment(FreeList& freeList, int size) {
Node* p = freeList.head;
while (p != NULL) {
if (p->size >= size) {
if (p->size > size) {
Node* newNode = new Node{p->addr + size, p->size - size, p->next};
p->size = size;
p->next = newNode;
}
freeList.assign = p;
return p;
}
p = p->next;
}
return NULL;
}
// 实现Best Fit Algorithm
Node* bestFitAssignment(FreeList& freeList, int size) {
Node* bestFit = NULL;
Node* p = freeList.head;
while (p != NULL) {
if (p->size >= size && (bestFit == NULL || p->size < bestFit->size)) {
bestFit = p;
}
p = p->next;
}
if (bestFit != NULL) {
if (bestFit->size > size) {
Node* newNode = new Node{bestFit->addr + size, bestFit->size - size, bestFit->next};
bestFit->size = size;
bestFit->next = newNode;
}
freeList.assign = bestFit;
}
return bestFit;
}
// 实现空闲区回收算法
void freeBlock(FreeList& freeList, Node* node) {
// 合并相邻的空闲区
Node** pp = &freeList.head;
while (*pp != NULL) {
if ((*pp)->addr + (*pp)->size == node->addr) {
node->addr = (*pp)->addr;
node->size += (*pp)->size;
Node* temp = *pp;
*pp = (*pp)->next;
delete temp;
} else if (node->addr + node->size == (*pp)->addr) {
node->size += (*pp)->size;
Node* temp = *pp;
*pp = (*pp)->next;
delete temp;
} else {
pp = &((*pp)->next);
}
}
// 插入到空闲区队列中
node->next = freeList.head;
freeList.head = node;
}
// 打印空闲区队列
void printFreeList(FreeList& freeList) {
int count = 0;
for (Node* p = freeList.head; p != NULL; p = p->next) {
cout << "编号: " << ++count << " 首地址: " << p->addr << " 终地址: " << p->addr + p->size << " 大小: " << p->size << endl;
}
}
int main() {
FreeList freeList;
freeList.head = new Node{0, 32767, NULL};
int choice;
int size;
int addr;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. First Fit 分配" << endl;
cout << "2. Best Fit 分配" << endl;
cout << "3. 回收内存" << endl;
cout << "4. 打印空闲区队列" << endl;
cout << "5. 退出" << endl;
cin >> choice;
if (choice == 1) {
cout << "请输入申请的内存大小:";
cin >> size;
Node* assignedNode1 = firstFitAssignment(freeList, size);
if (assignedNode1 != NULL) {
cout << "分配成功,首地址: " << assignedNode1->addr << " 大小: " << size << endl;
} else {
cout << "分配失败,没有足够的空闲内存。" << endl;
}
printFreeList(freeList);
} else if (choice == 2) {
cout << "请输入申请的内存大小:";
cin >> size;
Node* assignedNode2 = bestFitAssignment(freeList, size);
if (assignedNode2 != NULL) {
cout << "分配成功,首地址: " << assignedNode2->addr << " 大小: " << size << endl;
} else {
cout << "分配失败,没有足够的空闲内存。" << endl;
}
printFreeList(freeList);
} else if (choice == 3) {
cout << "请输入释放的内存首地址和大小:";
cin >> addr >> size;
Node* node = new Node{addr, size, NULL};
freeBlock(freeList, node);
cout << "内存回收成功。" << endl;
printFreeList(freeList);
} else if (choice == 4) {
printFreeList(freeList);
} else if (choice == 5) {
return 0;
} else {
cout << "无效的选择,请重新输入。" << endl;
}
}
return 0;
}
浙公网安备 33010602011771号