程序源代码:
#include <stdlib.h>
#include <conio.h>
#define UM_OUTPUT 1
#define UM_FIRSTFIT 2
#define UM_NEXTFIT 3
#define UM_BESTFIT 4
#define UM_WORSTFIT 5
#define dim(x) (sizeof(x)/sizeof(x[0]))
{
private:
int *space;
int last;
int MaxSize;
int cursor;
int G;
Status free(int); //内存回收
public:
M_manage(int size=1);
~M_manage();
void push_back(int x);
void out_put(); //打印出内存中的空闲分区
void sort_ascending(); //对内存中的分区按从小到大进行排序
void sort_descending(); // 对内存中的分区按从大到小进行排序
void first_fit(int x_size);
void next_fit(int x_size);
void best_fit(int x_size);
void worst_fit(int x_size);
//void quick_fit(int x_size);
};
{
sort_descending();
if(space[0]>=x_size)
{
space[0]-=x_size;
cout<< "内存分配成功" <<endl;
}
else
{
cout<< "内存中没有足够空间可供分配" <<endl;
}
}
{
sort_ascending();
int i = 0;
while(i!=MaxSize && space[i]<x_size)
{
++i;
}
if(i==MaxSize)
{
cout<< "内存中没有足够空间可供分配" <<endl;
}
else
{
space[i]-=x_size;
if(space[i]<=G)
{
space[i] = 0;
}
cout<<"内存分配成功"<<endl;
}
}
{
int i = cursor+1;
bool flag = true;
while( flag && space[i]<x_size)
{
++i;
if(i==MaxSize)
{
i = 0;
}
if(i == cursor+1)
{
flag = false;
}
}
if(flag)
{
cursor = i;
space[cursor] -= x_size;
cout<<"内存分配成功"<<endl;
}
else
{
cout<< "内存中没有足够空间可供分配" <<endl;
}
}
{
int i = 0;
while(i!=MaxSize && space[i]<x_size)
{
++i;
}
if(i==MaxSize)
{
cout<< "内存中没有足够空间可供分配" <<endl;
}
else
{
space[i]-=x_size;
cout<<"内存分配成功"<<endl;
}
}
void M_manage::sort_ascending()
{
int i, j;
int tem;
for(i=1; i!=MaxSize; ++i)
{
if(space[i]<space[i-1])
{
tem = space[i];
j = i-1;
do
{
space[j+1] = space[j];
--j;
} while (j!=-1 && space[j]>tem);
space[j+1] = tem;
}
}
}
{
int i, j;
int tem;
for(i=1; i!=MaxSize; ++i)
{
if(space[i]>space[i-1])
{
tem = space[i];
j = i-1;
do
{
space[j+1] = space[j];
--j;
} while (j!=-1 && space[j]<tem);
space[j+1] = tem;
}
}
}
{
int count = 0;
for(int i=0; i!=MaxSize; ++i)
{
if(space[i]!=0)
{
cout<<space[i]<<" ";
++count;
}
}
if(count == 0)
{
cout<< "内存使用率为100%,没有空闲分区供分配";
}
cout<<endl;
}
{
space[++last] = x;
}
{
space = new int[size];
MaxSize = size;
last = -1;
cursor = -1;
}
{
delete []space;
last = -1;
MaxSize = 0;
cursor = -1;
G = 0;
}
//----------------------- 主 存 回 收 --------------------
Status free(int ID)
{
DuLNode *p=block_first;
while(p)
{
if(p->data.ID==ID)
{
p->data.state=Free;
p->data.ID=Free;
if(p->prior->data.state==Free)//与前面的空闲块相连
{
p->prior->data.size+=p->data.size;
p->prior->next=p->next;
p->next->prior=p->prior;
}
if(p->next->data.state==Free)//与后面的空闲块相连
{
p->data.size+=p->next->data.size;
p->next->next->prior=p;
p->next=p->next->next;
}
break;
}
p=p->next;
}
return OK;
}
{
(*M).out_put();
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).first_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).next_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).best_fit(n_size);
}
{
int n_size;
cout<< "请输入作业大小:";
cin>>n_size;
while(n_size<=0)
{
cout<<"作业大小应为正,请重新分配:";
cin>>n_size;
}
(*M).worst_fit(n_size);
}
/////////////////////////////////////////////////////////////////////
struct MSGMAP_ENTRY
{
int nMessage;
void(*pfn)(M_manage* M);
};
{
UM_OUTPUT, print_M,
UM_FIRSTFIT, Mfirst_fit,
UM_NEXTFIT, Mnext_fit,
UM_BESTFIT, Mbest_fit,
UM_WORSTFIT, Mworst_fit
};
void main()
{
M_manage *my_M;
int select=1;
int n=0;
cout<<"输入内存空闲分区个数:";
cin>>n;
while(n<=0)
{
cout<< "内存空闲分区个数应为正数,请重新输入:";
cin>>n;
}
my_M = new M_manage(n);
for(int i=0; i!=n; ++i)
{
int s=0;
cout<<"输入第"<<i+1<<"分区大小:";
cin>>s;
while(s<=0)
{
cout<< "分区大小应为正,请重新输入" <<endl;
cout<<"输入第"<<i+1<<"分区大小:";
cin>>s;
}
(*my_M).push_back(s);
}
while(select!=0)
{
cout<<" 1.显示空闲分区 "<<endl;
cout<<" 2.首次适应算法 "<<endl;
cout<<" 3.循环首次适应算法 "<<endl;
cout<<" 4.最佳适应算法 "<<endl;
cout<<" 5.最坏适应算法 "<<endl;
cout<<" 0.退出 "<<endl;
cin>>select;
for(int i=0; i<dim(_messageEntres); ++i)
{
if((_messageEntres[i]).nMessage == select)
{
(*_messageEntres[i].pfn)(my_M);
}
}
}
}