free_lists, extend_heap, print_lists

This commit is contained in:
Felicia Seo 2022-11-12 16:30:32 -05:00
parent 9637fb3db3
commit e4933103ab

46
mm.c
View File

@ -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]; static size_t list_sizes[NUM_LISTS];
/* Function prototypes for internal helper routines */ /* 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 struct block *find_fit(size_t num_words);
//~ static void coalesce_free_lists(); //~ static void coalesce_free_lists();
static struct block *coalesce(struct block *bp); static struct block *coalesce(struct block *bp);
//~ static void print_lists();
/* Given a block, obtain previous's block footer. /* Given a block, obtain previous's block footer.
Works for left-most block also. */ Works for left-most block also. */
@ -94,7 +95,6 @@ static struct boundary_tag * get_footer(struct block *blk) {
- sizeof(struct boundary_tag); - sizeof(struct boundary_tag);
} }
/* Set a block's size and inuse bit in header and footer */ /* 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) { static void set_header_and_footer(struct block *blk, int size, int inuse) {
blk->header.inuse = 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 */ * get_footer(blk) = blk->header; /* Copy header to footer */
} }
/* Mark a block as used and set its size. */ /* Mark a block as used and set its size. */
static void set_block_used(struct block *blk, int size) { static void set_block_used(struct block *blk, int size) {
set_header_and_footer(blk, size, 1); set_header_and_footer(blk, size, 1);
@ -137,8 +135,8 @@ int mm_init(void)
initial[3] = FENCE; /* Epilogue header */ initial[3] = FENCE; /* Epilogue header */
for (int i = 0; i < NUM_LISTS; i++) { for (int i = 0; i < NUM_LISTS; i++) {
struct list current_list; struct list *current_list = malloc(sizeof(struct list));
list_init(&current_list); list_init(current_list);
free_lists[i] = current_list; free_lists[i] = current_list;
} }
@ -146,7 +144,7 @@ int mm_init(void)
list_sizes[i] = MIN_BLOCK_SIZE_WORDS * (i + 1); list_sizes[i] = MIN_BLOCK_SIZE_WORDS * (i + 1);
/* Extend the empty heap with a free block of CHUNKSIZE bytes */ /* 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 -1;
return 0; return 0;
} }
@ -205,7 +203,7 @@ void mm_free(void *bp)
//~ { //~ {
//~ for (int i = 0; i < NUM_LISTS; i++) { //~ for (int i = 0; i < NUM_LISTS; i++) {
//~ struct list* current_list = &free_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); //~ struct block* bp = list_entry(e, struct block, elem);
//~ coalesce(bp); //~ coalesce(bp);
//~ } //~ }
@ -304,21 +302,20 @@ static struct block *extend_heap(size_t words, int realloc)
return blk; return blk;
} }
list_push_front(find_list(0, words), &blk->elem); list_push_front(find_list(0, words), &blk->elem);
return coalesce(blk); return coalesce(blk);
} }
/* /*
* Allocate block of memory at address blck * Allocate block of memory at address blck
*/ */
static void place(struct block *blck, size_t asize) { static void place(struct block *blck, size_t asize) {
size_t csize = blk_size(blck); size_t csize = blk_size(blck);
/* Remove block from free list */ //~ print_lists();
list_remove(&blck->elem); list_remove(&blck->elem);
//~ print_lists();
size_t remaining_size = csize - asize; size_t remaining_size = csize - asize;
if (remaining_size >= MIN_BLOCK_SIZE_WORDS) { 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 */ /* Add remaining free block back into list */
list_push_front(find_list(0, remaining_size), &blck->elem); list_push_front(find_list(0, remaining_size), &blck->elem);
//~ print_lists();
} }
else { else {
set_block_used(blck, csize); 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++) { 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])) { if (not_empty && list_empty(free_lists[i])) {
continue; continue;
} }
return &free_lists[i]; return free_lists[i];
} }
} }
return NULL; return NULL;
@ -360,6 +358,8 @@ static struct block *find_fit(size_t num_words)
{ {
struct list* free_list = find_list(1, num_words); struct list* free_list = find_list(1, num_words);
//~ print_lists();
if (free_list == NULL) { if (free_list == NULL) {
return NULL; return NULL;
} }
@ -374,6 +374,24 @@ static struct block *find_fit(size_t num_words)
return NULL; 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 = { team_t team = {
"Micah Moore Felicia Seo", "Micah Moore Felicia Seo",
"Micah Moore", "Micah Moore",