$ ./mdriver -t ./traces -v Team Name:ateam Member 1 :Harry Bovik:[email protected] Using default tracefiles in ./traces/ Measuring performance with gettimeofday(). ERROR: mem_sbrk failed. Ran out of memory... ERROR [trace 4, line 7673]: mm_malloc failed. ERROR: mem_sbrk failed. Ran out of memory... ERROR [trace 5, line 1662]: mm_malloc failed. ERROR: mem_sbrk failed. Ran out of memory... ERROR [trace 6, line 1780]: mm_malloc failed. ERROR: mem_sbrk failed. Ran out of memory... ERROR [trace 9, line 1705]: mm_realloc failed. ERROR: mem_sbrk failed. Ran out of memory... ERROR [trace 10, line 6562]: mm_realloc failed.
Results for mm malloc: trace valid util ops secs Kops 0 yes 23% 5694 0.000021268585 1 yes 19% 5848 0.000018332273 2 yes 30% 6648 0.000020339184 3 yes 40% 5380 0.000015349351 4 no - - - - 5 no - - - - 6 no - - - - 7 yes 55% 12000 0.000031382166 8 yes 51% 24000 0.000070340909 9 no - - - - 10 no - - - - Total - - - -
/* 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) // ~0x7是0xFFFFFFF8,这里向上舍入到 8 的倍数
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
/* Basic constants and macros */ #define WSIZE 4 /* Word and header/footer size (bytes) */ #define DSIZE 8 /* Double word size (bytes) 或者,header+footer的大小*/ #define CHUNKSIZE (1 << 12) /* Extend heap by this amount (bytes) 4096字节*/
#define MAX(x, y) ((x) > (y) ? (x) : (y))
/* Pack a size and allocated bit into a word */ #define PACK(size, alloc) ((size) | (alloc))
/* 获取p地址处保存的unsigned int的值 Read and write a word at address p */ #define GET(p) (*(unsigned int *)(p)) /* 将val的值以unsigned int存储到p地址处*/ #define PUT(p, val) (*(unsigned int *)(p) = (val))
/* 读取p地址处保存的unsigned int的值,忽略最后三位 Read the size and allocated fields from address p */ #define GET_SIZE(p) (GET(p) & ~0x7) /* 获取p地址处保存的unsigned int的值的最后一位,也即该块是否分配*/ #define GET_ALLOC(p) (GET(p) & 0x1)
/* 获取bp地址处对应的块的头部的指针,char *类型,bp:块指针 减:往头部走 加:往脚部走 Given block ptr bp, compute address of its header and footer */ #define HDRP(bp) ((char *)(bp) - WSIZE) /* 获取bp地址处对应的块的脚部的指针,char *类型,要注意,头/脚部保存的大小是一整个块,包含头脚部和payload的所有的大小 */ #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
/* 获取bp地址处对应的块的下一个块的块指针 Given block ptr bp, compute address of next and previous blocks */ #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE))) /* 获取bp地址处对应的块的前一个块的块指针 */ #define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))
/* Extend the empty heap with a free block of CHUNKSIZE bytes */ if (extend_heap(CHUNKSIZE / WSIZE) == NULL) { return-1; } return0; }
mm_init函数负责堆的初始化工作
mm_init函数先调用mem_sbrk函数分配堆的初始空间,大小为4个字,依次是
padding
序言块 头部 8/1 Prologue header
序言块 脚部 8/1 Prologue footer
尾声块 头部 0/1 Epilogue header
下面看一下mem_sbrk函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* * mem_sbrk - simple model of the sbrk function. Extends the heap * by incr bytes and returns the start address of the new area. In * this model, the heap cannot be shrunk. */ void *mem_sbrk(int incr) { char *old_brk = mem_brk;
if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) { errno = ENOMEM; fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\n"); return (void *)-1; } mem_brk += incr; return (void *)old_brk; }
/* 为了8字节对齐,传入的words应该是2的整数倍,比如2,4,8 以满足8字节倍数的要求 Allocate an even number of words to maintain alignment */ size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE; // 调用mem_sbrk 申请size字节的内存,如果失败,返回NULL,bp是指向新申请的内存的底部的指针 if ((long)(bp = mem_sbrk(size)) == -1) returnNULL;
/* * mm_malloc - Allocate a block by incrementing the brk pointer. * Always allocate a block whose size is a multiple of the alignment. */ void *mm_malloc(size_t size) { size_t asize; /* Adjust block size */// 调整后的块大小 size_t extendsize; /* Amount to extend heap if no fit */// 如果没有合适的块,则需要扩展堆的大小 char *ptr;