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 * WSIZE), PACK(DSIZE, 1)); PUT(heap_listp + (3 * WSIZE), PACK(0, 1)); heap_listp += (2 * WSIZE); return 0; }
/* * mm_break - Extend the last block. */ inline void *mm_break(void *ptr, size_t size, size_t need_size) { size_t brk_size = need_size - size; // historical data total_brk += brk_size; // extense the heap mem_sbrk(brk_size); // generate the block return mm_put_header(ptr, need_size); }
/* extend heap */ static Header *morecore(size_t req_size) { Header *extended_ptr; if (req_size < 1024) req_size = 1024; extended_ptr = (Header *)mem_sbrk(req_size * sizeof(Header)); if (extended_ptr == (Header *)-1) return NULL; extended_ptr->size = req_size; free((void *)(extended_ptr+1)); return freelist; }
/* * 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) { t_block b, last; size_t s; int newsize = ALIGN(size + SIZE_T_SIZE); void *p = mem_sbrk(newsize); if (p == (void *)-1) { return NULL; } else { *(size_t *)p = size; return (void *)((char *)p + SIZE_T_SIZE); } }
/* * mm_init - initialize the malloc package. */ int mm_init(void) { #if HEAP_CHECK Signal(SIGABRT, print_stack_trace); heap_head = NULL; heap_tail = NULL; alloc_list = NULL; #endif if (!(free_list_arr = mem_sbrk(LEVEL * WSIZE))) { return NULL; } if (!(index_arr = mem_sbrk(LEVEL * WSIZE))) { return NULL; } int i = 0; for (; i < LEVEL; ++i) { *(index_arr + i) = 1 << (i + 4); *(free_list_arr + i) = NULL; } // 1 WSIZE for heap-start padding // 1 WSIZE for heap-end padding void *temp = NULL; if ((temp = mem_sbrk(2 * WSIZE)) == (void*)(-1)) { return -1; } heap_head = (void*)free_list_arr; heap_tail = (char*)temp + 2 * WSIZE; write_word(temp, pack(0, CURR_ALLOC)); // heap-start 0 padding write_word((char*)temp + WSIZE, pack(0, PREV_ALLOC | CURR_ALLOC)); // heap-end 0 padding #if HEAP_CHECK assert(heap_size() == (2 * LEVEL) * WSIZE + 2 * WSIZE); #endif return extend_heap(CHUNKSIZE, 0) == NULL ? -1 : 0; }
/* * extend_heap - Extends the heap when the desired memory is not available * */ static void *extend_heap(size_t words) { char *bp; size_t size=words*WSIZE; size = ALIGN(size+SIZE_T_SIZE); /* Set the size of the block to follow double word Allignment */ \ if ((int)(bp = mem_sbrk(size)) == -1) return NULL; PUT(HDRP(bp), PACK(size, 0)); /* Sets the Header of the Block */ PUT(FTRP(bp), PACK(size, 0)); /* Sets the Footer of the Block */ PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* Epilogue has to be created at the end */ return coalesce(bp); /* Coalescing the Free blocks (Immediate Coalescing) */ }
/* * malloc - Allocate a block by incrementing the brk pointer. * Always allocate a block whose size is a multiple of the alignment. */ void *malloc(size_t size){ int newsize = ALIGN(size + SIZE_T_SIZE); unsigned char *p = mem_sbrk(newsize); //dbg_printf("malloc %u => %p\n", size, p); if ((long)p < 0) return NULL; else { p += SIZE_T_SIZE; *SIZE_PTR(p) = size; return p; } }
int mm_init(void) { htp=mem_sbrk(55*DSIZE); // 55 lists being created if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) return -1; PUT(heap_listp, 0); // Alignment padding PUT(heap_listp+WSIZE, PACK(DSIZE, 1)); //Prologue header PUT(heap_listp+DSIZE, PACK(DSIZE, 1)); // Prologue footer PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); // Epilogue header heap_listp += DSIZE; int i=0; head=htp; while(i<55){ SetNextFree(htp+i*DSIZE,0); // Initialize head to NULL ( yet no free block ) ++i; } if (extend_heap(CHUNKSIZE/WSIZE) == (void *)-1)/* Extend the empty heap with a free block of CHUNKSIZE bytes */ return -1; return 0; }
/* $begin mminit */ int mm_init(void) { /* create the initial empty heap */ if ((heap_listp = mem_sbrk(6*WSIZE)) == NULL) { return -1; } PUT(heap_listp, 0); /* alignment padding */ PUT(heap_listp+(1*WSIZE), PACK(OVERHEAD, 1)); /* prologue header */ PUT(heap_listp+(2*WSIZE), 0); PUT(heap_listp+(3*WSIZE), 0); PUT(heap_listp+(4*WSIZE), PACK(OVERHEAD, 1)); /* prologue footer */ PUT(heap_listp+(5*WSIZE), PACK(0, 1)); /* epilogue header */ heap_listp += (2*WSIZE); freeblockcount = 0; //mm_checkheap(1); char* freeblock; /* Extend the empty heap with a free block of CHUNKSIZE bytes */ if ((freeblock = extend_heap((CHUNKSIZE/WSIZE + 88))) == NULL) { return -1; } insert_block(freeblock); //mm_checkheap(1); /*printf("Remove-a block\n"); remove_block(freeblock); mm_checkheap(1);*/ /*char* anotherfree; anotherfree = extend_heap(CHUNKSIZE/WSIZE); printf("adda annari blokkinni\n"); insert_block(anotherfree);*/ /*char* temp; temp = extend_heap(CHUNKSIZE/WSIZE); printf("adda þriðju blokkinni\n"); insert_block(temp);*/ /*size_t predFirstBlock = GET(PRED(freeblock)); size_t succFirstBlock = GET((char*)SUCC(freeblock)); printf("FIRSTBLOCK, PRED: %x SUCC: %x\n", predFirstBlock, succFirstBlock); remove_block(freeblock); printf("Remove-a fremstu blokk\n"); mm_checkheap(1);*/ return 0; }
//Extend the heap static void *extend_heap(size_t words) { //Even number of words to maintain alignment size_t size = (words % 2) ? ((words + 1) * WSIZE) : (words * WSIZE); uint32_t *bp = mem_sbrk(size); if((long)bp == -1) return NULL; PUT(get_header(bp), PACK(size, 0)); PUT(get_footer(bp), PACK(size, 0)); PUT(block_next(get_header(bp)), PACK(0,1)); return coalesce(bp); }
/* * mm_init - initialize the malloc package. */ int mm_init(void) { if((heap_listp = mem_sbrk(4*WSIZE)) == NULL) return -1; PUT(heap_listp, 0); PUT(heap_listp+WSIZE, PACK(OVERHEAD,1)); PUT(heap_listp+DSIZE, PACK(OVERHEAD,1)); PUT(heap_listp+WSIZE+DSIZE, PACK(0,1)); heap_listp += DSIZE; rover = heap_listp; return 0; }
int mm_init(void) { if ((heap_start_ptr = mem_sbrk(4*WSIZE)) == (void *)-1) /* returns the pointer which will be the strating point of heap */ return -1; PUT(heap_start_ptr, 0); /* Indicates 4 bytes padding at the starting of the heap */ PUT(heap_start_ptr+WSIZE, PACK(OVERHEAD, 1)); /* Prologue (1) */ PUT(heap_start_ptr+DSIZE, PACK(OVERHEAD, 1)); /* Prologue (2) */ PUT(heap_start_ptr+WSIZE+DSIZE, PACK(0, 1)); /* Epilogue */ heap_start_ptr += DSIZE; head_FreeList=NULL; /* Initial Free list Pointer NULL because no memory for payload is available at this point */ if (extend_heap(CHUNKSIZE/WSIZE) == (void *)-1) /* Extends the heap to have blocks of memory (argument is number of words) */ return -1; return 0; }
/* * mm_init - Initialize the malloc package. */ int mm_init(void) { mm_set_first_free(mem_heap_lo()); // put a small block to hold some data mem_sbrk(48); mm_put_header(mem_heap_lo(), SIGN_MARK(48)); total_alloc = 0; total_free = 0; total_brk = 0; return 0; }
/* * 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); /* Alignment padding */ PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1)); /* Prologue header */ PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1)); /* Prologue footer */ PUT(heap_listp + (3*WSIZE), PACK(0, 1)); /* Epilogue header */ heap_listp += (2*WSIZE); /* Extend the empty heap with a free block of CHUNKSIZE bytes */ if (extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1; return 0; }
/* * Initialize: return -1 on error, 0 on success. */ int mm_init(void) { /* Initialize the seg_list with NULL */ seg_list = mem_sbrk(SEG_LIST_SIZE * sizeof(uint32_t *)); for (int i = 0; i < SEG_LIST_SIZE; ++i) { seg_list[i] = NULL; } if ((heap_listp = mem_sbrk(4 * WSIZE)) == (void *)-1) return -1; set_size(heap_listp, 0); // Allignment padding set_size(heap_listp + 1, 0); // Pro of 0 size set_size(heap_listp + 3, 0); // Epi of 0 size (heap_listp + 3)[0] |= 0x40000000; // Mark epi as allocated block_mark(heap_listp + 1, ALLOCATED); // Mark prologue as allocated heap_listp += 1; /* Extend the empty heap with a free block of CHUNKSIZE bytes * extend_heap would ask for 2 more words */ if (extend_heap(CHUNKSIZE + 2) == NULL) return -1; return 0; }
int mm_init(void) { /* Create the initial empty heap */ heap_listp = mem_sbrk(2*WSIZE); if (heap_listp == (void *)-1) return -1; PUT(heap_listp + (0*WSIZE), PACK(WSIZE, 1)); /* Prologue header */ PUT(heap_listp + (1*WSIZE), PACK(0, 3)); /* Epilogue header */ heap_listp += WSIZE; /* Extend the empty heap with a free block of CHUNKSIZE bytes */ if (extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1; return 0; }
static void *extend_heap(size_t words) { char *bp; size_t size; /* Allocate an even number of words to maintain alignment */ size = (words % 2) ? (words+1) * WSIZE : words * WSIZE; if ((long)(bp = mem_sbrk(size)) == -1) return NULL; PUT(bp + size - WSIZE, PACK(0, 1)); /* New epilogue header */ mm_log("setting up epilogue at %p\n", bp + size); return init_free_block(bp, size); }
int mm_init(void) //initialize { if((heap = mem_sbrk(8*WSIZE)) == (void*)-1) return -1; set(heap, 0); set(heap + WSIZE, setMask(2*DSIZE, 1)); //size, header set(heap + WSIZE*2, heap + WSIZE*4); //prev set(heap + WSIZE*3, heap + WSIZE*4); //next set(heap + WSIZE*4, setMask(2*DSIZE, 1));//footer set(heap + WSIZE*5, setMask(0, 1)); //next block set(heap + WSIZE*6, heap + WSIZE*8); //prev set(heap + WSIZE*7, heap + WSIZE*8); //next heap += 4*WSIZE; if(extend_heap((1<<6)/WSIZE) == NULL) return -1; //initialize heap return 0; }
/* * mm_init - initialize the malloc package. */ int mm_init(range_t **ranges) { /* allocate root and nil nodes */ if((rb_root = rb_null = mem_sbrk(4 + MIN_BLOCK_SIZE)) == FAIL) return -1; /* assign sentinel values */ RB_LEFT(rb_root) = RB_RIGHT(rb_root) = rb_null; RB_RED(rb_root) = 0; /* prevent coalesce by setting free bit to 0*/ PREV_SIZE(NEXT_BLOCK(rb_null, MIN_BLOCK_SIZE)) = 0; /* DON't MODIFY THIS STAGE AND LEAVE IT AS IT WAS */ gl_ranges = ranges; return 0; }
/********************************************************** * This function sets up the initial block, as well as * sets the heap list to memb_sbrk as well as * set the free list to NULL as well as * sets the maxBlockSize1 and maxBlockSize2 to 0 **********************************************************/ int mm_init(void) { if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) return -1; PUT(heap_listp, 0); // alignment padding PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1)); // prologue header PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1)); // prologue footer PUT(heap_listp + (3 * WSIZE), PACK(0, 1)); // epilogue header heap_listp += DSIZE; //should case heap_listp as char* free_listp = NULL; maxBlockSize1 = 0; // set the max block size as 0 maxBlockSize2 = 0; // set the second max block size as 0 return 0; }
/* * Initialize: return -1 on error, 0 on success. */ int mm_init(void) { heap_ptr = mem_sbrk(4*WSIZE); if(heap_ptr == (void *)-1) return -1; PUT(heap_ptr, 0); PUT(heap_ptr + (1*WSIZE), PACK(DSIZE, 1)); PUT(heap_ptr + (2*WSIZE), PACK(DSIZE, 1)); PUT(heap_ptr + (3*WSIZE), PACK(0,1)); heap_ptr += WSIZE; if(extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1; return 0; }
/* * malloc */ void *malloc(size_t size) { int newsize = ALIGN(size + SIZE_T_SIZE); unsigned char *p = mem_sbrk(newsize); if( (long)p < 0 ) return NULL; else { p += SIZE_T_SIZE; *SIZE_PTR(p) = newsize; return p; } }
/** * 向kernel申请更多的vm来扩展heap,并把新申请到的vm初始化成free block */ static void *extend_heap(size_t words) { size_t size = (words % 2 == 0) ? words * WSIZE : (words + 1) * WSIZE; void *bp; if ((bp = mem_sbrk(size)) == (void*) -1) { return NULL; } else { /* 初始化free block */ PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* 新的结尾块 */ return coalesce(bp); } }
static void *extend_heap(unsigned int words) { char *bp; unsigned int size; /* allocate an even number of words to maintain alignment */ size = (words % 2)?(words+1)*wsize:words*wsize; if((bp = (char*)mem_sbrk(size)) == (void*)-1) /* mem_sbrk return the old mem_brk */ return NULL; /* inintialize free block header/footer and epilogue header*/ put(getBlockHeader(bp),pack(size,0)); put(getBlockFooter(bp),pack(size,0)); put(getBlockHeader(getNextBlock(bp)),pack(0,1)); /* ??? */ return coalesce(bp); }
/* * Initialize: return -1 on error, 0 on success. */ int mm_init(void) { heap_listp = NULL; classp = NULL; int i = 0; /* Init segregated free list class pointer space */ if ((classp = mem_sbrk(CLASS_NUM * WSIZE)) == (void *)-1) return -1; for (i = 0; i < CLASS_NUM; i++) { SET_CLASS_ROOT_BLK(classp + (i * WSIZE), NULL); } /* Try mm_init with sbrk*/ if ((heap_listp = mem_sbrk(4 * WSIZE)) == (void *)-1) return -1; PUT(heap_listp, 0); PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 3)); /* Prologue header */ PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 3)); /* Prologue footer */ PUT(heap_listp + (3 * WSIZE), PACK(0, 3)); /* Epilogue header */ heap_listp += 2 * WSIZE; if (extend_heap(CHUNKSIZE/WSIZE) == NULL) { return -1; } dbg_printf("HEAP INIT\n"); mm_checkheap(CHECK_HEAP); return 0; }
/* $begin mminit */ int mm_init(void) { /* create the initial empty heap */ hdr_p p; if ((p = mem_sbrk(ALIGN(OVERHEAD))) == NULL){ return -1; } else { p->size = PACK(OVERHEAD, 1); p->next=NULL; p->prev=p; PUT(BLPTR(p), GET_SIZE(p)); } return 0; }
/* * extend_heap - Used to extend heap by size_t */ void* extend_heap(size_t words){ char *bp; size_t size; size = (words % 2) ? (words +1) * WSIZE : words * WSIZE; if((long) (bp = mem_sbrk(size)) == -1) return NULL; PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(NEXT_BLKP(bp)), PACK(0,1)); return bp; //return coalesce(bp); }
int mm_init(void) { if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) return -1; PUT(heap_listp, 0); PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1)); PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); heap_listp += DSIZE; root=NULL; // Initialize root to NULL ( yet no free block ) if (extend_heap(CHUNKSIZE/WSIZE) == (void *)-1) return -1; return 0; }
/* * Initialize: return -1 on error, 0 on success. * Allocates space for free list pointers. * Sets each to point to NULL which means the end of the list. */ int mm_init(void) { void** current; heap_start = mem_sbrk(NUM_FREE_LISTS * sizeof(void*)); if(heap_start == NULL) return -1; free_lists = heap_start; current = heap_start; for (int i = 0; i < NUM_FREE_LISTS; i++) { *current = NULL; current++; } return 0; }
// // mm_init - Initialize the memory manager // int mm_init(void) { // Create the initial empty heap if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) return -1; PUT(heap_listp, 0); //Alignment padding PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1)); //Prologue header PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1)); //prologue footer PUT(heap_listp + (3*WSIZE), PACK(0, 1)); //Epilogue header // Extend the empty heap with a free block of CHUNKSIZE bytes if (extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1; return 0; }