66/100
This commit is contained in:
parent
2b68e9884e
commit
1d1c70c945
77
mm.c
77
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
|
* Blocks have a payload which holds hold necessary data and a list_elem struct so blocks can be stored in lists
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -71,13 +69,11 @@ 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 */
|
||||||
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 void place(struct block *blck, size_t asize);
|
||||||
static struct list* find_list(int not_empty, size_t size);
|
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 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. */
|
||||||
@ -132,6 +128,7 @@ static void set_block_used(struct block *blk, int size) {
|
|||||||
/* Mark a block as free and set its size. */
|
/* Mark a block as free and set its size. */
|
||||||
static void set_block_free(struct block *blk, int size) {
|
static void set_block_free(struct block *blk, int size) {
|
||||||
set_header_and_footer(blk, size, 0);
|
set_header_and_footer(blk, size, 0);
|
||||||
|
list_push_front(find_list(0, size), &blk->elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mm_init(void)
|
int mm_init(void)
|
||||||
@ -161,11 +158,16 @@ int mm_init(void)
|
|||||||
free_lists[i] = current_list;
|
free_lists[i] = current_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < NUM_LISTS; i++)
|
for (int i = 0; i < NUM_LISTS; i++) {
|
||||||
list_sizes[i] = MIN_BLOCK_SIZE_WORDS * (i + 1);
|
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 */
|
/* 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 -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -189,14 +191,8 @@ void *mm_malloc(size_t size) {
|
|||||||
place(blck, words);
|
place(blck, words);
|
||||||
return blck->payload;
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
place(blck, words);
|
place(blck, words);
|
||||||
@ -215,22 +211,9 @@ void mm_free(void *bp)
|
|||||||
|
|
||||||
set_block_free(blk, size);
|
set_block_free(blk, size);
|
||||||
|
|
||||||
list_push_front(find_list(0, size), &blk->elem);
|
|
||||||
|
|
||||||
coalesce(blk);
|
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) {
|
void *mm_realloc(void *ptr, size_t size) {
|
||||||
size_t bytesize;
|
size_t bytesize;
|
||||||
void *new_ptr;
|
void *new_ptr;
|
||||||
@ -296,6 +279,7 @@ void *mm_realloc(void *ptr, size_t size) {
|
|||||||
return new_ptr;
|
return new_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static struct block *coalesce(struct block *bp)
|
static struct block *coalesce(struct block *bp)
|
||||||
{
|
{
|
||||||
bool prev_alloc = prev_blk_footer(bp)->inuse; /* is previous block allocated? */
|
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);
|
list_remove(&next_blk(bp)->elem);
|
||||||
size_t new_size = size + blk_size(next_blk(bp));
|
size_t new_size = size + blk_size(next_blk(bp));
|
||||||
set_block_free(bp, new_size);
|
set_block_free(bp, new_size);
|
||||||
list_push_front(find_list(0, new_size), &bp->elem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!prev_alloc && next_alloc) { /* Case 3 */
|
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;
|
size_t new_size = blk_size(prev_blk(bp)) + size;
|
||||||
bp = prev_blk(bp);
|
bp = prev_blk(bp);
|
||||||
set_block_free(bp, new_size);
|
set_block_free(bp, new_size);
|
||||||
list_push_front(find_list(0, new_size), &bp->elem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else { /* Case 4 */
|
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));
|
size_t new_size = blk_size(prev_blk(bp)) + size + blk_size(next_blk(bp));
|
||||||
bp = prev_blk(bp);
|
bp = prev_blk(bp);
|
||||||
set_block_free(bp, new_size);
|
set_block_free(bp, new_size);
|
||||||
list_push_front(find_list(0, new_size), &bp->elem);
|
|
||||||
}
|
}
|
||||||
return bp;
|
return bp;
|
||||||
}
|
}
|
||||||
@ -343,7 +324,7 @@ static struct block *coalesce(struct block *bp)
|
|||||||
* Extend heap with free block and return its block pointer
|
* Extend heap with free block and return its block pointer
|
||||||
* Does not add new block to free list if called by realloc
|
* 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);
|
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);
|
set_block_free(blk, words);
|
||||||
next_blk(blk)->header = FENCE;
|
next_blk(blk)->header = FENCE;
|
||||||
|
|
||||||
if (realloc) {
|
|
||||||
return blk;
|
|
||||||
}
|
|
||||||
list_push_front(find_list(0, words), &blk->elem);
|
|
||||||
|
|
||||||
return coalesce(blk);
|
return coalesce(blk);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -379,9 +355,6 @@ static void place(struct block *blck, size_t asize) {
|
|||||||
set_block_used(blck, asize);
|
set_block_used(blck, asize);
|
||||||
blck = next_blk(blck);
|
blck = next_blk(blck);
|
||||||
set_block_free(blck, remaining_size);
|
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();
|
//~ print_lists();
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
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;
|
||||||
}
|
}
|
||||||
@ -415,8 +388,6 @@ 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;
|
||||||
}
|
}
|
||||||
@ -431,24 +402,6 @@ 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",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user