📄 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. */#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <unistd.h>#include "mm.h"#include "memlib.h"/* declare mm_check as static */static int mm_check(void);/* 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)team_t team = { /* Team name, whatever you like */ "naive", /* First member's full name */ "Cao Ying", /* First member's BBS ID */ "caoy", /* Second member's full name (leave blank if none) */ "", /* Second member's email address (leave blank if none) */ ""};#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))/* * mm_init - initialize the malloc package. */int mm_init(void){ return 0;}/* * 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){ int newsize = ALIGN(size + SIZE_T_SIZE); void *p = mem_sbrk(newsize); if ((int)p < 0) return NULL; else { *(size_t *)p = size; return (void *)((char *)p + SIZE_T_SIZE); }}/* * mm_free - Freeing a block does nothing. */void mm_free(void *ptr){}/* * mm_realloc - Implemented simply in terms of mm_malloc and mm_free */void *mm_realloc(void *ptr, size_t size){ void *oldptr = ptr; void *newptr; size_t copySize; newptr = mm_malloc(size); if (newptr == NULL) return NULL; copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE); if (size < copySize) copySize = size; memcpy(newptr, oldptr, copySize); mm_free(oldptr); return newptr;}/* * mm_check - Does not currently check anything */static int mm_check(void){ return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -