added realloc
This commit is contained in:
parent
cabd49cb4f
commit
08198ed242
37
mm.c
37
mm.c
@ -212,6 +212,35 @@ void mm_free(void *bp)
|
||||
//~ }
|
||||
//~ }
|
||||
|
||||
void *mm_realloc(void *ptr, size_t size) {
|
||||
void *new_ptr = NULL;
|
||||
|
||||
if (ptr == NULL) {
|
||||
return mm_malloc(size);
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
mm_free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct block *oldblock = ptr - offsetof(struct block, payload);
|
||||
size_t oldwords = blk_size(oldblock);
|
||||
size_t oldsize = oldwords * WSIZE;
|
||||
|
||||
if (!(new_ptr = mm_malloc(size))) {
|
||||
return NULL;
|
||||
}
|
||||
if (size < oldsize) {
|
||||
oldsize = size;
|
||||
}
|
||||
memcpy(new_ptr, ptr, oldsize);
|
||||
mm_free(ptr);
|
||||
|
||||
return new_ptr;
|
||||
|
||||
}
|
||||
|
||||
static struct block *coalesce(struct block *bp)
|
||||
{
|
||||
bool prev_alloc = prev_blk_footer(bp)->inuse; /* is previous block allocated? */
|
||||
@ -275,9 +304,9 @@ static struct block *extend_heap(size_t words, int realloc)
|
||||
}
|
||||
list_push_front(find_list(words), &blk->elem);
|
||||
|
||||
// NOT IMPLEMENTED YET
|
||||
//return coalesce(blk);
|
||||
return NULL;
|
||||
|
||||
return coalesce(blk);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -342,7 +371,7 @@ static struct block *find_fit(size_t num_words)
|
||||
}
|
||||
|
||||
team_t team = {
|
||||
"?", // team name
|
||||
"Micah Moore Felicia Seo",
|
||||
"Micah Moore",
|
||||
"micahmoore@vt.edu",
|
||||
"Felicia Seo",
|
||||
|
Loading…
x
Reference in New Issue
Block a user