From e4933103abb6ba77aa366ed41485a46b05560391 Mon Sep 17 00:00:00 2001 From: Felicia Seo Date: Sat, 12 Nov 2022 16:30:32 -0500 Subject: [PATCH] free_lists, extend_heap, print_lists --- mm.c | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/mm.c b/mm.c index da69789..7b81d04 100644 --- a/mm.c +++ b/mm.c @@ -46,7 +46,7 @@ static size_t align(size_t size) { } -static struct list free_lists[NUM_LISTS]; +static struct list *free_lists[NUM_LISTS]; static size_t list_sizes[NUM_LISTS]; /* Function prototypes for internal helper routines */ @@ -56,6 +56,7 @@ 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. */ @@ -94,7 +95,6 @@ static struct boundary_tag * get_footer(struct block *blk) { - sizeof(struct boundary_tag); } - /* Set a block's size and inuse bit in header and footer */ static void set_header_and_footer(struct block *blk, int size, int inuse) { blk->header.inuse = inuse; @@ -102,8 +102,6 @@ static void set_header_and_footer(struct block *blk, int size, int inuse) { * get_footer(blk) = blk->header; /* Copy header to footer */ } - - /* Mark a block as used and set its size. */ static void set_block_used(struct block *blk, int size) { set_header_and_footer(blk, size, 1); @@ -137,8 +135,8 @@ int mm_init(void) initial[3] = FENCE; /* Epilogue header */ for (int i = 0; i < NUM_LISTS; i++) { - struct list current_list; - list_init(¤t_list); + struct list *current_list = malloc(sizeof(struct list)); + list_init(current_list); free_lists[i] = current_list; } @@ -146,7 +144,7 @@ int mm_init(void) list_sizes[i] = MIN_BLOCK_SIZE_WORDS * (i + 1); /* Extend the empty heap with a free block of CHUNKSIZE bytes */ - if (extend_heap(CHUNKSIZE, 1) == NULL) + if (extend_heap(CHUNKSIZE, 0) == NULL) return -1; return 0; } @@ -205,7 +203,7 @@ void mm_free(void *bp) //~ { //~ 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_back(current_list); e = list_next(e)) { + //~ 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); //~ } @@ -304,21 +302,20 @@ static struct block *extend_heap(size_t words, int realloc) return blk; } list_push_front(find_list(0, words), &blk->elem); - return coalesce(blk); } - /* * Allocate block of memory at address blck */ static void place(struct block *blck, size_t asize) { size_t csize = blk_size(blck); - - /* Remove block from free list */ + + //~ print_lists(); list_remove(&blck->elem); + //~ print_lists(); size_t remaining_size = csize - asize; if (remaining_size >= MIN_BLOCK_SIZE_WORDS) { @@ -328,6 +325,7 @@ static void place(struct block *blck, size_t asize) { /* Add remaining free block back into list */ list_push_front(find_list(0, remaining_size), &blck->elem); + //~ print_lists(); } else { set_block_used(blck, csize); @@ -344,10 +342,10 @@ 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 (not_empty && list_empty(&free_lists[i])) { + if (not_empty && list_empty(free_lists[i])) { continue; } - return &free_lists[i]; + return free_lists[i]; } } return NULL; @@ -360,6 +358,8 @@ 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; } @@ -374,6 +374,24 @@ 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",