【深入理解计算机系统csapp lab】malloc lab

做malloclab首先要熟悉课本9.9的内容,尤其是9.9.12,如果不知道从哪里入手,可以和我一样,从实现课本介绍的简单分配器开始,然后在这个基础上改编。
试验后最终采取的是隐式空闲链表+分离的显式空闲链表,分离存储的块大小为{16-32},{33-64},{65-128}···,空闲块在链表中按从小到大排列,这样首次匹配的结果接近最佳匹配。

mm.h

#include <stdio.h>

extern int mm_init (void);
extern void *mm_malloc (size_t size);
extern void mm_free (void *ptr);
extern void *mm_realloc(void *ptr, size_t size);


/* 
 * Students work in teams of one or two.  Teams enter their team name, 
 * personal names and login IDs in a struct of this
 * type in their bits.c file.
 */
typedef struct {
   
    char *teamname; /* ID1+ID2 or ID1 */
    char *name1;    /* full name of first member */
    char *id1;      /* login ID of first member */
    char *name2;    /* full name of second member (if any) */
    char *id2;      /* login ID of second member */
} team_t;

extern team_t team;

#define WSIZE 4 
#define DSIZE 8

#define MAX(x,y) ((x)>(y)?(x):(y))

#define PACK(size,alloc) ((size)|(alloc))

#define GET(p) (*(unsigned int*) (p))
#define PUT(p,val) (*(unsigned int *)(p)=(unsigned int)(val))

#define GET_SIZE(p) (GET(p)&~0x7)
#define GET_ALLOC(p) (GET(p)&0x1)

#define HDRP(bp) ((char*)(bp)-WSIZE)
#define FTRP(bp) ((char*)(bp)+GET_SIZE(HDRP(bp))-DSIZE)

#define NEXT_BLKP(bp) ((char*)(bp)+GET_SIZE(HDRP(bp)))
#define PREV_BLKP(bp) ((char*)(bp)-GET_SIZE((char*)(bp)-DSIZE))

#define NEXT_PTR(bp) ((char*)(bp))//空闲链表下一个地址的地址
#define PREV_PTR(bp) ((char*)(bp)+WSIZE)//空闲链表上一个地址的地址

#define NEXT(bp) (*(char**)(NEXT_PTR(bp)))//空闲链表下一个块地址
#define PREV(bp) (*(char**)(PREV_PTR(bp)))//空闲链表上一个块地址

mm.c

/*
 * mm-naive.c - The fastest, least memory-efficient malloc package.
 * 
 * In this naive approach, a block is allocated by simply incrementing
 * the brk pointer.  A block is pure payload. There are no headers or
 * footers.  Blocks are never coalesced or reused. Realloc is
 * implemented directly using mm_malloc and mm_free.
 *
 * NOTE TO STUDENTS: Replace this header comment with your own header
 * comment that gives a high level description of your solution.
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>

#include "mm.h"
#include "memlib.h"

/*********************************************************
 * NOTE TO STUDENTS: Before you do anything else, please
 * provide your team information in the following struct.
 ********************************************************/
team_t team = {
   
    /* Team name */
    "ateam",
    /* First member's full name */
    "Shuyu",
    /* First member's email address */
    "liushuyuwww@163.com",
    /* Second member's full name (leave blank if none) */
    "",
    /* Second member's email address (leave blank if none) */
    ""
};

/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8

#define CHUNKSIZE 1<<12 //CHUNKSIZE一定要>=2*DSIZE

/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)


#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define LISTLEN 20 //分离的空闲链表个数

static void* heap_listp; 
static void* segregated_header[LISTLEN]; //分离空闲链表


static void* extend_heap(size_t words); //拓展堆words个字
static void* coalesce(void* bp); //如果bp前后相邻空闲块,合并之
static void* find_fit(size_t asize); //寻找能够容纳asize字节的空闲块(初次匹配)
static void place(void* bp,size_t asize); //在bp指向的空闲块中分配asize空间,如果空闲块剩余空间>=2*DSIZE,将其再插入合适的空闲链表中
static int list_index(size_t asize); //返回asize大小的空闲块所在空闲链表的索引
static void insert_free(void* bp,size_t asize);  //插入bp指向的asize大小的空闲块到空闲链表中
static void delete_free(void* bp); //在空闲链表中删除bp指向的空闲块

/* 
 * mm_init - initialize the malloc package.
 */
int mm_init(void)
{
   
	//初始化空堆
	if((heap_listp=mem_sbrk(4*WSIZE))==(void*)-1)
		return -1;
	PUT(heap_listp,0);
	PUT(heap_listp+(1*WSIZE),PACK(DSIZE,1));//序言块
	PUT(heap_listp
  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值