📄 mm_0_no_use.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 <string.h>#include "mm.h"#include "memlib.h"/* Basic constants and macros */#define WSIZE 4 /* word size (bytes) */ #define DSIZE 8 /* doubleword size (bytes) */#define CHUNKSIZE (1<<12) /* initial heap size (bytes) */#define OVERHEAD 8 /* overhead of header and footer (bytes) */#define MAX(x, y) ((x) > (y)? (x) : (y)) /* Pack a size and allocated bit into a word */#define PACK(size, alloc) ((size) | (alloc))/* Read and write a word at address p */#define GET(p) (*(size_t *)(p))#define PUT(p, val) (*(size_t *)(p) = (val)) /* Read the size and allocated fields from address p */#define GET_SIZE(p) (GET(p) & ~0x7)#define GET_ALLOC(p) (GET(p) & 0x1)/* Given block ptr bp, compute address of its header and footer */#define HDRP(bp) ((char *)(bp) - WSIZE) #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)/* Given block ptr bp, compute address of next and previous blocks */#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))/* $end mallocmacros *//* The only global variable is a pointer to the first block */static char *heap_listp; /* function prototypes for internal helper routines */static void *extend_heap(size_t words);static void place(void *bp, size_t asize);static void *find_fit(size_t asize);static void *coalesce(void *bp);static void printblock(void *bp); /* 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)#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))//****************************variables***************int *seg4,*seg8,*seg10,*seg20,*seg40,*seg80,*seg100,*seg200,*seg400,*seg800,*seg1000,*seg2000, *seg4000,*seg8000,*seg10000,*seg20000,*seg40000,*seg80000,*seg100000,*seg200000,*seg400000, *seg800000,*seg1000000,*seg2000000,*seg4000000,*seg8000000,*seg10000000,*seg20000000, *seg40000000,*seg80000000;int seg_num4,seg_num8,seg_num10,seg_num20,seg_num40,seg_num80,seg_num100,seg_num200,seg_num400, seg_num800,seg_num1000,seg_num2000, seg_num4000,seg_num8000,seg_num10000,seg_num20000, seg_num40000,seg_num80000,seg_num100000,seg_num200000,seg_num400000,seg_num800000,seg_num1000000, seg_num2000000,seg_num4000000,seg_num8000000,seg_num10000000,seg_num20000000; seg_num40000000,seg_num80000000;int largest_size,largest_exist;/* * mm_init - initialize the malloc package. */int mm_init(void){ /* create the initial empty heap */ if ((heap_listp = mem_sbrk(4*WSIZE)) == NULL) return -1; PUT(heap_listp, 0); /* alignment padding */ PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1)); /* prologue header */ PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); /* prologue footer */ PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); /* epilogue header */ heap_listp += DSIZE; /* Extend the empty heap with a free block of CHUNKSIZE bytes */ if (extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1; 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); } size_t word_num; /* the number of words needed for this malloc requirement */ size_t seg_off; /* the offset of the free table*/ size_t asize; /* adjusted block size */ size_t extendsize; /* amount to extend heap if no fit */ char *bp; /* Ignore spurious requests */ if (size <= 0) return NULL; /* Adjust block size to include overhead and alignment reqs. */ if (size <= DSIZE) asize = DSIZE + OVERHEAD; else asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) / DSIZE); word_num = asize / WSIZE; //get the number of words needed for this mallor operation seg_off = floor(log(word_num)); //get the associated table entry for this //judge whether the corresponding table entry is null or not,i.e.,existing a good one address if ( (bp = *(seg4 + seg_off * WSIZE)) != NULL ) { //find the address place(bp,asize); //place the allocated memory *(seg4 + seg_off * WSIZE) = NULL; //set the table entry to be null *(seg_num4 + seg_off * WSIZE)--; //decrease the number of the size of the allocated place return; } /* Search the free list for a fit */ if ((bp = find_fit(asize)) != NULL) { place(bp, asize); return bp; } /* No fit found. Get more memory and place the block */ extendsize = MAX(asize,CHUNKSIZE); if ((bp = extend_heap(extendsize/WSIZE)) == NULL) return NULL; place(bp, asize); return bp;}/* * 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 + -