From 1d1c70c945ca489557b89bf27663dab9ec2900e5 Mon Sep 17 00:00:00 2001 From: Felicia Seo Date: Mon, 14 Nov 2022 15:32:08 -0500 Subject: [PATCH] 66/100 --- mm.c | 77 ++++++++++++------------------------------------------------ 1 file changed, 15 insertions(+), 62 deletions(-) diff --git a/mm.c b/mm.c index 5d6ff7c..a6c36ac 100644 --- a/mm.c +++ b/mm.c @@ -16,9 +16,7 @@ * * Blocks have a payload which holds hold necessary data and a list_elem struct so blocks can be stored in lists */ - - - + #include #include #include @@ -71,13 +69,11 @@ static struct list *free_lists[NUM_LISTS]; static size_t list_sizes[NUM_LISTS]; /* Function prototypes for internal helper routines */ -static struct block *extend_heap(size_t words, int realloc); +static struct block *extend_heap(size_t words); static void place(struct block *blck, size_t asize); static struct list* find_list(int not_empty, size_t size); static struct block *find_fit(size_t num_words); -//~ static void coalesce_free_lists(); static struct block *coalesce(struct block *bp); -//~ static void print_lists(); /* Given a block, obtain previous's block footer. Works for left-most block also. */ @@ -132,6 +128,7 @@ static void set_block_used(struct block *blk, int size) { /* Mark a block as free and set its size. */ static void set_block_free(struct block *blk, int size) { set_header_and_footer(blk, size, 0); + list_push_front(find_list(0, size), &blk->elem); } int mm_init(void) @@ -161,11 +158,16 @@ int mm_init(void) free_lists[i] = current_list; } - for (int i = 0; i < NUM_LISTS; i++) - list_sizes[i] = MIN_BLOCK_SIZE_WORDS * (i + 1); + for (int i = 0; i < NUM_LISTS; i++) { + int size = 1; + for (int j = 0; j < i; j++) { + size *= 2; + } + list_sizes[i] = MIN_BLOCK_SIZE_WORDS * (size); + } /* Extend the empty heap with a free block of CHUNKSIZE bytes */ - if (extend_heap(CHUNKSIZE, 0) == NULL) + if (extend_heap(CHUNKSIZE) == NULL) return -1; return 0; } @@ -189,14 +191,8 @@ void *mm_malloc(size_t size) { place(blck, words); return blck->payload; } - - //~ coalesce_free_lists(); - //~ if ((blck = find_fit(words)) != NULL) { - //~ place(blck, words); - //~ return blck->payload; - //~ } - if ((blck = extend_heap(words, 0)) == NULL){ + if ((blck = extend_heap(words)) == NULL){ return NULL; } place(blck, words); @@ -215,22 +211,9 @@ void mm_free(void *bp) set_block_free(blk, size); - list_push_front(find_list(0, size), &blk->elem); - coalesce(blk); } -//~ static void coalesce_free_lists() -//~ { - //~ for (int i = 0; i < NUM_LISTS; i++) { - //~ struct list* current_list = &free_lists[i]; - //~ for (struct list_elem* e = list_begin(current_list); e != list_end(current_list); e = list_next(e)) { - //~ struct block* bp = list_entry(e, struct block, elem); - //~ coalesce(bp); - //~ } - //~ } -//~ } - void *mm_realloc(void *ptr, size_t size) { size_t bytesize; void *new_ptr; @@ -296,6 +279,7 @@ void *mm_realloc(void *ptr, size_t size) { return new_ptr; } + static struct block *coalesce(struct block *bp) { bool prev_alloc = prev_blk_footer(bp)->inuse; /* is previous block allocated? */ @@ -313,7 +297,6 @@ static struct block *coalesce(struct block *bp) list_remove(&next_blk(bp)->elem); size_t new_size = size + blk_size(next_blk(bp)); set_block_free(bp, new_size); - list_push_front(find_list(0, new_size), &bp->elem); } else if (!prev_alloc && next_alloc) { /* Case 3 */ @@ -323,7 +306,6 @@ static struct block *coalesce(struct block *bp) size_t new_size = blk_size(prev_blk(bp)) + size; bp = prev_blk(bp); set_block_free(bp, new_size); - list_push_front(find_list(0, new_size), &bp->elem); } else { /* Case 4 */ @@ -334,7 +316,6 @@ static struct block *coalesce(struct block *bp) size_t new_size = blk_size(prev_blk(bp)) + size + blk_size(next_blk(bp)); bp = prev_blk(bp); set_block_free(bp, new_size); - list_push_front(find_list(0, new_size), &bp->elem); } return bp; } @@ -343,7 +324,7 @@ static struct block *coalesce(struct block *bp) * Extend heap with free block and return its block pointer * Does not add new block to free list if called by realloc */ -static struct block *extend_heap(size_t words, int realloc) +static struct block *extend_heap(size_t words) { void *blck = mem_sbrk(words * WSIZE); @@ -355,11 +336,6 @@ static struct block *extend_heap(size_t words, int realloc) set_block_free(blk, words); next_blk(blk)->header = FENCE; - if (realloc) { - return blk; - } - list_push_front(find_list(0, words), &blk->elem); - return coalesce(blk); } @@ -379,9 +355,6 @@ static void place(struct block *blck, size_t asize) { set_block_used(blck, asize); blck = next_blk(blck); set_block_free(blck, remaining_size); - - /* Add remaining free block back into list */ - list_push_front(find_list(0, remaining_size), &blck->elem); //~ print_lists(); } else { @@ -398,7 +371,7 @@ static void place(struct block *blck, size_t asize) { static struct list* find_list(int not_empty, size_t n_words) { for (int i = 0; i < NUM_LISTS; i++) { - if ((n_words < list_sizes[i]) || (i == (NUM_LISTS - 1))) { + if ((n_words <= list_sizes[i]) || (i == (NUM_LISTS - 1))) { if (not_empty && list_empty(free_lists[i])) { continue; } @@ -415,8 +388,6 @@ static struct block *find_fit(size_t num_words) { struct list* free_list = find_list(1, num_words); - //~ print_lists(); - if (free_list == NULL) { return NULL; } @@ -431,24 +402,6 @@ static struct block *find_fit(size_t num_words) return NULL; } -//~ static void print_lists() -//~ { - //~ for (int i = 0; i < NUM_LISTS; i++) - //~ { - //~ struct list *lp = free_lists[i]; - - //~ printf("Free List %d: ", i); - - //~ for (struct list_elem *e = list_begin(lp); e != list_end(lp); e = list_next(e)) - //~ { - //~ struct block *bp = list_entry(e, struct block, elem); - //~ printf("{inuse: %d, size: %d}\t", bp->header.inuse, bp->header.size); - //~ } - - //~ printf("\n"); - //~ } -//~ } - team_t team = { "Micah Moore Felicia Seo", "Micah Moore",