模拟内存分配器2

1. 要求

建立空闲块构成的链表,每个块包含该块的字节数和指向链表中下个块的指针。从每个块起始处存放它们。要分配指定大小的块时,采用最先适应算法,取出所需要的块,接着更新链表。该块被释放时,把该块添加到链表的尾部。

2. C程序

cpp 复制代码
#include <stdlib.h>
#include <sys/mman.h>
#include <stdio.h>
#define WSIZE 4   /* word size (bytes) */
#define DSIZE 8    /* double word size (bytes) */
#define OVERHEAD 12  /* overhead of header and footer (bytes) */
struct segment
{
   int  length;
   struct segment  *link;
} ;   
struct segment *head;
 
/* 声明结构函数allocblock()和无值型函数dealloc() */
struct segment *allocblock(int x);
void print(struct segment *phead);
struct segment *find_fit(int x);
void dealloc(struct segment *blo);
int main()
{
  int size;
  struct segment *freelist,*p,*q,*block1;
  size=100;
  head=NULL;
  freelist=(struct segment *)mmap( NULL,4096,PROT_READ|PROT_WRITE,
                                   MAP_PRIVATE|MAP_ANONYMOUS,-1,0 );
  if(freelist==MAP_FAILED) {
    printf("mmap faile");
  }
    /* 建立链表 */
  freelist->length=size;
  freelist->link=NULL;
  head=freelist;

  p=head+size+OVERHEAD;
  p->length=200;
  freelist->link=p;
  p->link=NULL;

  q=p+p->length+OVERHEAD;
  q->length=300;
  p->link=q;
  q->link=NULL;

  printf("before allocblock:\n");
  print(head);
  size=120;
  block1=allocblock(size);   // 分配一个size大小块block1
  if (block1 == NULL) {
      perror("allocblock faile");
      exit(1);
    }
  printf("The length of allocated block is %d\n",block1->length);
  printf("after allocblock:\n");
  print(head);
  dealloc(block1);    // 释放block1
  printf("after dealloc:\n");
  print(head);
  
  return 0;
}

struct segment *allocblock(int x)
{
  int i,temp;
  struct segment *bp;  
  if (x<=0)
    return NULL;
  else if (x<=DSIZE) 
    x=OVERHEAD+ WSIZE;
  else
    x=((x+(OVERHEAD)+(DSIZE-1))/DSIZE)*DSIZE;
  if((bp=find_fit(x))!=NULL){
    return bp;
  }
  else
  return NULL;
}

struct segment *find_fit(int x){
  struct segment *m,*n,*r;
  int temp;
  m=head;
  if(x<=head->length){
    if(((head->length)-x)<2*DSIZE)
      head=head->link;
    else{
      temp=m->length;
      m->length=x;
      head=m+x+12;
      head->length=temp-x-12;
      head->link=m->link;
    }
        
  }
  else{
      while(x>m->length){
        n=m; m=m->link;
        if (m==NULL) break; 
      }
      if (m!=NULL){
        if(((m->length)-x)<2*DSIZE)
          n->link=m->link;
        else{
          temp=m->length;
          m->length=x;
          r=m+x+12;
          r->length=temp-x-12;
          n->link=r;
          r->link=m->link;
        }
      }
      else
       return NULL;
  }
  return m;
 }

void print(struct segment *phead)
{   
  struct segment *p;
  p=phead;
  while (p!=NULL){
    printf("%d\n",p->length);
    p=p->link ;
  }
}

void dealloc(struct segment *blo)    
{
   struct segment *p;
   p=head;
   while(p->link!=NULL)
     p=p->link;
   p->link = blo;
   blo->link = NULL;
}   

3. 运行结果

before allocblock:

100

200

300

The length of allocated block is 136

after allocblock:

100

52

300

after dealloc:

100

52

300

136

相关推荐
啊阿狸不会拉杆14 分钟前
《机器学习导论》第 5 章-多元方法
人工智能·python·算法·机器学习·numpy·matplotlib·多元方法
R1nG8631 小时前
CANN资源泄漏检测工具源码深度解读 实战设备内存泄漏排查
数据库·算法·cann
_OP_CHEN1 小时前
【算法基础篇】(五十六)容斥原理指南:从集合计数到算法实战,解决组合数学的 “重叠难题”!
算法·蓝桥杯·c/c++·组合数学·容斥原理·算法竞赛·acm/icpc
TracyCoder1231 小时前
LeetCode Hot100(27/100)——94. 二叉树的中序遍历
算法·leetcode
九.九1 小时前
CANN HCOMM 底层机制深度解析:集合通信算法实现、RoCE 网络协议栈优化与多级同步原语
网络·网络协议·算法
C++ 老炮儿的技术栈2 小时前
Qt Creator中不写代如何设置 QLabel的颜色
c语言·开发语言·c++·qt·算法
子春一2 小时前
Flutter for OpenHarmony:构建一个 Flutter 数字消消乐游戏,深入解析网格状态管理、合并算法与重力系统
算法·flutter·游戏
草履虫建模8 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq10 小时前
分布式系统安全通信
开发语言·c++·算法
Jasmine_llq10 小时前
《P3157 [CQOI2011] 动态逆序对》
算法·cdq 分治·动态问题静态化+双向偏序统计·树状数组(高效统计元素大小关系·排序算法(预处理偏序和时间戳)·前缀和(合并单个贡献为总逆序对·动态问题静态化