/* * 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 */ "Vite Fuck", /* First member's full name */ "zhz_vite", /* First member's email address */ "2811215248@qq.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
/* rounds up to the nearest multiple of ALIGNMENT */ #define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)//会得到大于等于size的最小整数 //(size) + (ALIGNMENT-1)会得到最接近但不大于其alignment的倍数
/* * 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 */ "Vite Fuck", /* First member's full name */ "zhz_vite", /* First member's email address */ "2811215248@qq.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
/* rounds up to the nearest multiple of ALIGNMENT */ #define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)//会得到大于等于size的最小整数 //(size) + (ALIGNMENT-1)会得到最接近但不大于其alignment的倍数
#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) /* $begin mallocmacros */ /* Basic constants and macros */ #define WSIZE 4 /* Word and header/footer size (bytes) */ #define DSIZE 8 /* Double word size (bytes) */ #define CHUNKSIZE (1<<12) /* Extend heap by this amount (bytes) */ #define MAX(x, y) ((x) > (y)? (x) : (y)) /* Pack a size and allocated bit into a word */ #define PACK(size, alloc) ((size) | (alloc)) //打包头部的值,再用PUT(p,PACK(size,alloc)),之类的函数把他丢进header/footer /* Read and write a word at address p */ #define GET(p) (*(unsigned int *)(p)) //获得p指向的值 #define PUT(p, val) (*(unsigned int *)(p) = (val)) //写入val与p指向地址 /* Read the size and allocated fields from address p */ #define GET_SIZE(p) (GET(p) & ~0x7) //由于双字对齐条件约束,故释放最低三位,即得到的unsigned int 值为多少倍的DSIZE #define GET_ALLOC(p) (GET(p) & 0x1) //有无分配 /* Given block ptr bp, compute address of its header and footer */ #define HDRP(bp) ((char *)(bp) - WSIZE) //the address of the header #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE) //the address of the footer /* Given block ptr bp, compute address of next and previous blocks */ #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE))) //next blocks pointer #define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE))) //prev blocks pointer #define MINBLOCK (DSIZE+2*WSIZE) staticvoid *heap_listp;
/* * 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 */ "Vite Fuck", /* First member's full name */ "zhz_vite", /* First member's email address */ "2811215248@qq.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 /* rounds up to the nearest multiple of ALIGNMENT */ #define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)//会得到大于等于size的最小整数 //(size) + (ALIGNMENT-1)会得到最接近但不大于其alignment的倍数
#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) /* $begin mallocmacros */ /* Basic constants and macros */ #define WSIZE 4 /* Word and header/footer size (bytes) */ #define DSIZE 8 /* Double word size (bytes) */ #define CHUNKSIZE (1<<12) /* Extend heap by this amount (bytes) */ #define MAX(x, y) ((x) > (y)? (x) : (y)) /* Pack a size and allocated bit into a word */ #define PACK(size, alloc) ((size) | (alloc)) //打包头部的值,再用PUT(p,PACK(size,alloc)),之类的函数把他丢进header/footer /* Read and write a word at address p */ #define GET(p) (*(unsigned int *)(p)) //获得p指向的值 #define PUT(p, val) (*(unsigned int *)(p) = (val)) //写入val与p指向地址
#define GETADDR(p) (*(unsigned int **)(p)) //读地址p处的一个指针 #define PUTADDR(p,addr) (*(unsigned int **)(p) = (unsigned int *)(addr)) //向地址p处写一个指针 /* Read the size and allocated fields from address p */ #define GET_SIZE(p) (GET(p) & ~0x7) //由于双字对齐条件约束,故释放最低三位,即得到的unsigned int 值为多少倍的DSIZE #define GET_ALLOC(p) (GET(p) & 0x1) //有无分配 /* Given block ptr bp, compute address of its header and footer */ #define HDRP(bp) ((char *)(bp) - WSIZE) //the address of the header #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE) //the address of the footer /* Given block ptr bp, compute address of next and previous blocks */ #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE))) //next blocks pointer #define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE))) //prev blocks pointer