/* implement a memory allocation scheme by using algorithms first-fit, next-fit, and best-fit
* freelist为空闲区链表的头,它的下一个节点才指向空闲缓冲区
* freelist是按首地址来排序的,而不是按size来排序。按首地址排序便于相邻空闲内存合并;按size大小来排序,便于分配内存
*/
#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef enum {FALSE, TRUE} bool;
typedef struct node
{
char *ptr; //start addr 指向mem处开始的地址
int size; //size of the free block
struct node *next; //next free block
}node;
char mem[N]; //total memory pool
node freelist; //head of free list, no value
/* init freelist to contain the whole mem */
void init()
{
node *ptr = (node *)malloc(sizeof(node));
ptr->ptr = mem;
ptr->size = N;
ptr->next = NULL;
freelist.next = ptr;
}
/* remove a node ptr from the list whose previous node is prev */
void removenode(node *ptr, node *prev)
{
prev->next = ptr->next;
free(ptr);
}
/* 首次适配法:从自由空闲区中选取第一个合适空闲区来分配
* 返回分配内存区首地址
内存分配算法-(首次分配法和最佳分配法)
最新推荐文章于 2024-06-30 10:00:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)