📄 firstfit.h
字号:
//first-fit策略函数
void* mm_request(int n); //分配指定大小的内存
//first fit
void* mm_request(int n)
{
memState *temp;
// memState *p;
temp = head;
int find = 0;
while(temp!=NULL)
{
if(temp->state==1&&(temp->end-temp->begin+1)>n) //如果该块空闲且大小足够
{
memState *addBlock = (memState*)malloc(sizeof(memState)); //将该增加块插入到temp后面
addBlock->before = temp;
addBlock->next = temp->next;
if(temp->next!=NULL)
{
temp->next->before = addBlock;
}
temp->next = addBlock;
addBlock->end = temp->end;
temp->end =(temp->begin+n-1);
temp->state=0; //该块已经分配
addBlock->begin = temp->end +1;
addBlock->state = 1;//分割剩余块状态为可用
find = 1;
//////////////////////////////////////
blockaddr[count]=&mem[temp->begin];
upDate();
count++;
//////////////////////////////////////
return &mem[temp->begin];
}
else if(temp->state==1&&(temp->end-temp->begin+1) == n) //大小恰好相等,直接返回
{
temp->state = 0; //将该块置为不可用状态
find = 1;
//////////////////////////////////////
//该注释块内的内容是为测试内存分配效率做准备
blockaddr[count]=&mem[temp->begin];
upDate(); //更新块对应的内存地址数组
count++; //块数加一
//////////////////////////////////////
return &mem[temp->begin];
}
else
{
temp=temp->next;
}
}
if(!find)
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -