news 2026/1/31 11:42:04

模拟内存分配器2

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
模拟内存分配器2

1. 要求

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

2. C程序

#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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/29 15:28:41

深入解析Grin交易内核:5大核心机制实现Mimblewimble隐私保护

深入解析Grin交易内核&#xff1a;5大核心机制实现Mimblewimble隐私保护 【免费下载链接】grin Minimal implementation of the Mimblewimble protocol. 项目地址: https://gitcode.com/gh_mirrors/gr/grin Grin作为Mimblewimble协议的精简实现&#xff0c;其交易内核机…

作者头像 李华
网站建设 2026/1/28 22:48:57

Autocut视频剪辑革命:用文字编辑器轻松剪出专业视频

Autocut视频剪辑革命&#xff1a;用文字编辑器轻松剪出专业视频 【免费下载链接】autocut 用文本编辑器剪视频 项目地址: https://gitcode.com/GitHub_Trending/au/autocut 在视频内容创作爆发的时代&#xff0c;繁琐的剪辑流程往往成为创作者最大的痛点。传统视频剪辑软…

作者头像 李华
网站建设 2026/1/30 16:03:31

开源笔记系统终极排障指南:8个实战场景深度解析

开源笔记系统终极排障指南&#xff1a;8个实战场景深度解析 【免费下载链接】memos An open source, lightweight note-taking service. Easily capture and share your great thoughts. 项目地址: https://gitcode.com/GitHub_Trending/me/memos 你是否在使用开源笔记系…

作者头像 李华
网站建设 2026/1/26 10:31:57

开源大模型落地趋势分析:轻量级推理模型+弹性GPU部署实战

开源大模型落地趋势分析&#xff1a;轻量级推理模型弹性GPU部署实战 1. 轻量级大模型为何成为落地首选&#xff1f; 在大模型技术快速演进的今天&#xff0c;我们正经历从“参数军备竞赛”向“实用效率优先”的转变。过去动辄百亿、千亿参数的模型虽然能力强大&#xff0c;但…

作者头像 李华
网站建设 2026/1/31 9:13:13

通义千问3-14B实战案例:跨境电商多语言翻译系统搭建

通义千问3-14B实战案例&#xff1a;跨境电商多语言翻译系统搭建 你有没有遇到过这样的问题&#xff1f;跨境电商平台上&#xff0c;同一款商品要面向几十个国家销售&#xff0c;每种语言的描述都得人工翻译一遍&#xff0c;耗时又容易出错。更头疼的是&#xff0c;不同国家的语…

作者头像 李华
网站建设 2026/1/27 20:41:05

用Qwen3-0.6B搭建聊天机器人,响应速度令人惊喜

用Qwen3-0.6B搭建聊天机器人&#xff0c;响应速度令人惊喜 1. 引言&#xff1a;为什么选择Qwen3-0.6B做聊天机器人&#xff1f; 你有没有试过自己动手搭一个能“秒回”的AI聊天机器人&#xff1f;不是那种卡顿半天才蹦出一句话的模型&#xff0c;而是真正能做到输入即响应、对…

作者头像 李华