CSAPP 六个重要的实验 lab5
CSAPP && lab5
实验指导书:
http://download.csdn.net/detail/u011368821/7951657
实验材料:
http://download.csdn.net/detail/u011368821/8019293
搞定这个实验还是要看一下曾经的笔记,再复习一下block的组织方式。仅仅看link里面第11节,动态内存分配的部分就能够了
http://blog.csdn.net/cinmyheart/article/details/38136375
然后看看对block的探測性学习
http://blog.csdn.net/cinmyheart/article/details/38174421
The only file you will modify and turn in is mm.c.
Your dynamic storage allocator will consist of the following three functions (and several helper functions), which are declared in mm.h and defined in mm.c:
int mm_init(void);
void* mm_malloc(size_t size);
void mm_free(void* ptr);
Your job is to complete this implementation by filling out mm_malloc() and mm_free().
任务就是利用下面现有的API去补全mm_malloc()和mm_free()
Provided Code
We define a BlockInfo struct designed to be used as a node in a doublylinked explicit free list, and the following functions for manipulating free lists:
BlockInfo* searchFreeList(int reqSize): returns a block of at least the requested size if one exists (and NULL otherwise).
void insertFreeBlock(BlockInfo* blockInfo): inserts the given block in the free list in LIFO manner.
void removeFreeBlock(BlockInfo* blockInfo): removes the given block from the free list.
In addition, we implement mm_init and provide two helper functions implementing important parts of the allocator:
void requestMoreSpace(int incr): enlarges the heap by incr bytes (if enough memory is available on the machine to do so).
void coalesceFreeBlock(BlockInfo* oldBlock): coalesces any other free blocks adjacent in memory to oldBlock into a
single new large block and updates the free list accordingly.
这里free list的实现介绍: 这里须要说明一下的就是header处于低地址,footer处于高地址. 这里说明的原因是由于实现的时候会有指针的加减,注意一下就是了 :-)
sizeAndTags中记录的size信息是整个block的大小。包含了struct BlockInfo结构体的大小
论证: 观察struct BlockInfo结构体在内存中的分布
正是依据内存布局的特点,这里blockCursor仅仅想当前block头的时候减去一个WORD_SIZE会得到什么?
前一个block的footer 即boundary tag,由此获知当前block的前一个block的大小
My answer:
/* Allocate a block of size size and return a pointer to it. */
void* mm_malloc (size_t size) {
size_t reqSize;
BlockInfo * ptrFreeBlock = NULL;
BlockInfo * ptrNextBlock = NULL;
size_t blockSize;
size_t precedingBlockUseTag;
size_t* ptrNextBlockFooter = NULL; // Zero-size requests get NULL.
if (size == 0) {
return NULL;
} // Add one word for the initial size header.
// Note that we don't need to boundary tag when the block is used!
size += WORD_SIZE;
if (size <= MIN_BLOCK_SIZE) {
// Make sure we allocate enough space for a blockInfo in case we
// free this block (when we free this block, we'll need to use the
// next pointer, the prev pointer, and the boundary tag).
reqSize = MIN_BLOCK_SIZE;
} else {
// Round up for correct alignment
reqSize = ALIGNMENT * ((size + ALIGNMENT - 1) / ALIGNMENT);
} // Implement mm_malloc. You can change or remove any of the above
// code. It is included as a suggestion of where to start.
// You will want to replace this return statement... ptrFreeBlock = searchFreeList(reqSize); if(!ptrFreeBlock)
{
requestMoreSpace(reqSize);
ptrFreeBlock = searchFreeList(reqSize);
} blockSize = SIZE(ptrFreeBlock->sizeAndTags); if(blockSize < reqSize + MIN_BLOCK_SIZE)
{
reqSize = blockSize;
} precedingBlockUseTag = ptrFreeBlock->sizeAndTags & TAG_PRECEDING_USED;
ptrFreeBlock->sizeAndTags = reqSize | precedingBlockUseTag | TAG_USED; ptrNextBlock = (BlockInfo*)UNSCALED_POINTER_ADD(ptrFreeBlock,reqSize); if(reqSize != blockSize)
{
ptrNextBlock->sizeAndTags = (blockSize - reqSize) | TAG_PRECEDING_USED; ptrNextBlockFooter = (size_t *)UNSCALED_POINTER_ADD(ptrFreeBlock,blockSize - WORD_SIZE); (*ptrNextBlockFooter) = ptrNextBlock->sizeAndTags; insertFreeBlock(ptrNextBlock);
}
else
{
ptrNextBlock->sizeAndTags = ptrNextBlock->sizeAndTags | TAG_PRECEDING_USED; ptrNextBlockFooter = (size_t*)UNSCALED_POINTER_ADD(ptrNextBlock,SIZE(ptrNextBlock->sizeAndTags) - WORD_SIZE); (*ptrNextBlockFooter) = ptrNextBlock->sizeAndTags;
}
removeFreeBlock(ptrFreeBlock); return (void*)UNSCALED_POINTER_ADD(ptrFreeBlock,WORD_SIZE);
} /* Free the block referenced by ptr. */
void mm_free (void *ptr) {
size_t payloadSize;
size_t blockSize;
BlockInfo * blockInfo;
BlockInfo * followingBlock; // Implement mm_free. You can change or remove the declaraions
// above. They are included as minor hints. BlockInfo* prev_block = NULL;
BlockInfo* next_block = NULL; blockInfo = (BlockInfo*)UNSCALED_POINTER_SUB(ptr,WORD_SIZE);
followingBlock = (BlockInfo *)UNSCALED_POINTER_ADD(blockInfo,blockInfo->sizeAndTags); blockSize = SIZE(blockInfo->sizeAndTags);
payloadSize = blockSize - WORD_SIZE;
blockInfo->sizeAndTags = blockInfo->sizeAndTags & ~TAG_USED;
*(size_t *)UNSCALED_POINTER_ADD(blockInfo,payloadSize) = blockInfo->sizeAndTags;
followingBlock->sizeAndTags = followingBlock->sizeAndTags & ~TAG_PRECEDING_USED; if(followingBlock->sizeAndTags & TAG_USED == 0)
{
*(size_t *)UNSCALED_POINTER_ADD(followingBlock,SIZE(followingBlock->sizeAndTags) - WORD_SIZE) = followingBlock->sizeAndTags;
} insertFreeBlock(blockInfo);
coalesceFreeBlock(blockInfo); }
最后执行./mdriver
会有一下输出
题外话,追究这个malloc实现的话....事实上它就是基于已经有的stdlib.h已经有的malloc而实现的,介绍的是实现原理,利用双向链表
后面的额外练习表示没有耐心了,对于realloc...
到此,CSAPP的lab临时告一段落,以后还会峰峰补补的去更新 :-)
假设对这六个lab感兴趣,欢迎邮箱联系交流讨论
jasonleaster@gmail.com
版权声明:本文博客原创文章,博客,未经同意,不得转载。
CSAPP 六个重要的实验 lab5的更多相关文章
- 第六周课程总结&实验报告(四)
实验报告(四) 一.实验目的 1.掌握类的继承 2.变量的继承和覆盖,方法的继承,重载和覆盖的实现 二.实验的内容 1.根据下面的要求实现圆类Circle. 圆类Circle的成员变量:radius表 ...
- 【iCore3 双核心板】例程二十六:MODBUS TCP实验——电源监控
实验指导书及代码包下载: http://pan.baidu.com/s/1pKhxKd9 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]
Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...
- 第六周课程总结&实验报告
一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...
- 第六周学习总结&实验报告四
一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...
- 【CSAPP】Shell Lab 实验笔记
shlab这节是要求写个支持任务(job)功能的简易shell,主要考察了linux信号机制的相关内容.难度上如果熟读了<CSAPP>的"异常控制流"一章,应该是可以不 ...
- 20145330第六周《Java学习笔记》
20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...
- CSAPP LAB: Buffer Overflow
这是CSAPP官网上的著名实验,通过注入汇编代码实现堆栈溢出攻击.实验材料可到我的github仓库 https://github.com/Cheukyin/CSAPP-LAB/ 选择buffer-ov ...
- ORACLE 实验二
实验二:数据操纵 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.掌握SQL数据查询语句: 2.掌握SQL聚集函数的使用. 3.掌握SQL插入.改动.删除语句的使用. 二.实验内 ...
随机推荐
- Android获得Manifest在<meta-data>元件的值
前段时间攻略完成游戏开发项目.其中用于包装散装. 目前市场上的网络不提交.但是,通过设置Manifest中的Meta_data>去获得相关參数,游戏ID号改变.游戏ID改变,然后游戏内容就改变. ...
- C++学习笔记10-面向对象
1. 面向对象的程序设计是基于三个基本概念:数据抽象.继承和动态绑定. 在C++ 在,凭借一流的数据抽象,随着一类从一个类派生还继承:派生类的成员继承基类.决定是使用基类中定义的函数还是派生类中定义 ...
- 使用国内源解决Qt在线更新慢的问题
Qt在线安装更新工具默认使用官方的源,国内访问比较慢,可以在setting中增加国内的源来提高更新速度,如下面的源: http://mirrors.ustc.edu.cn/qtproject/onli ...
- Android真机网络adb联机调试初探
新项目是一个基于android4.2设备.刚拿到demo板时就对联机互调感兴趣了.处于以前在S3c2440上对linux的移植使用经验.心里猜测对于android设备应该也这样.所以通过搜索资料整理如 ...
- hdu4389(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意:f(x)表示x各位的数字和. 给定1<=L<=R<=10^9, 求[L, ...
- DOM中的动态NodeList与静态NodeList
GitHub版本号: https://github.com/cncounter/translation/blob/master/tiemao_2014/NodeList/NodeList.md 副标题 ...
- Swift编程语言学习10—— 枚举属性监视器
属性监视器 属性监视器监控和响应属性值的变化,每次属性被设置值的时候都会调用属性监视器.甚至新的值和如今的值同样的时候也不例外. 能够为除了延迟存储属性之外的其它存储属性加入属性监视器,也能够通过重载 ...
- PHP:小数位计算
本文提供了两种方法,分数的方法成为字符串.然后,"."为了拦截.跟.子长后.另一个是关于小数*10的N钍.实例10的8再次钍8取余次.然后继续10余.取决于10结果的余数是不0. ...
- Debian7.6安装过程中遇到的问题
一 sudo命令不能用 1 使用su切换到root用户,命令: su 2 使用名:vim /etc/sudoers加入sudoer用户,命令: vim /etc/sudoers 找到root=(ALL ...
- 【转向Javascript系列】深入理解Generators
随着Javascript语言的发展,ES6规范为我们带来了许多新的内容,其中生成器Generators是一项重要的特性.利用这一特性,我们可以简化迭代器的创建,更加令人兴奋的,是Generators允 ...