not_empty, coalesce
This commit is contained in:
parent
08198ed242
commit
9637fb3db3
40
mm.c
40
mm.c
@ -52,7 +52,7 @@ 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, int realloc);
|
||||||
static void place(struct block *blck, size_t asize);
|
static void place(struct block *blck, size_t asize);
|
||||||
static struct list* find_list(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 void coalesce_free_lists();
|
||||||
static struct block *coalesce(struct block *bp);
|
static struct block *coalesce(struct block *bp);
|
||||||
@ -196,7 +196,7 @@ void mm_free(void *bp)
|
|||||||
|
|
||||||
set_block_free(blk, size);
|
set_block_free(blk, size);
|
||||||
|
|
||||||
list_push_front(find_list(size), &blk->elem);
|
list_push_front(find_list(0, size), &blk->elem);
|
||||||
|
|
||||||
coalesce(blk);
|
coalesce(blk);
|
||||||
}
|
}
|
||||||
@ -255,30 +255,31 @@ static struct block *coalesce(struct block *bp)
|
|||||||
else if (prev_alloc && !next_alloc) { /* Case 2 */
|
else if (prev_alloc && !next_alloc) { /* Case 2 */
|
||||||
// combine this block and next block by extending it
|
// combine this block and next block by extending it
|
||||||
list_remove(&bp->elem);
|
list_remove(&bp->elem);
|
||||||
set_block_free(bp, size + blk_size(next_blk(bp)));
|
|
||||||
list_remove(&next_blk(bp)->elem);
|
list_remove(&next_blk(bp)->elem);
|
||||||
list_push_front(find_list(size + blk_size(next_blk(bp))), &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 */
|
else if (!prev_alloc && next_alloc) { /* Case 3 */
|
||||||
// combine previous and this block by extending previous
|
// combine previous and this block by extending previous
|
||||||
|
list_remove(&prev_blk(bp)->elem);
|
||||||
list_remove(&bp->elem);
|
list_remove(&bp->elem);
|
||||||
|
size_t new_size = blk_size(prev_blk(bp)) + size;
|
||||||
bp = prev_blk(bp);
|
bp = prev_blk(bp);
|
||||||
set_block_free(bp, size + blk_size(bp));
|
set_block_free(bp, new_size);
|
||||||
list_remove(&bp->elem);
|
list_push_front(find_list(0, new_size), &bp->elem);
|
||||||
list_push_front(find_list(size + blk_size(next_blk(bp))), &bp->elem);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else { /* Case 4 */
|
else { /* Case 4 */
|
||||||
// combine all previous, this, and next block into one
|
// combine all previous, this, and next block into one
|
||||||
list_remove(&bp->elem);
|
list_remove(&prev_blk(bp)->elem);
|
||||||
set_block_free(prev_blk(bp),
|
|
||||||
size + blk_size(next_blk(bp)) + blk_size(prev_blk(bp)));
|
|
||||||
bp = prev_blk(bp);
|
|
||||||
list_remove(&bp->elem);
|
list_remove(&bp->elem);
|
||||||
list_remove(&next_blk(bp)->elem);
|
list_remove(&next_blk(bp)->elem);
|
||||||
list_push_front(find_list(size + blk_size(next_blk(bp))), &bp->elem);
|
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;
|
return bp;
|
||||||
}
|
}
|
||||||
@ -302,7 +303,7 @@ static struct block *extend_heap(size_t words, int realloc)
|
|||||||
if (realloc) {
|
if (realloc) {
|
||||||
return blk;
|
return blk;
|
||||||
}
|
}
|
||||||
list_push_front(find_list(words), &blk->elem);
|
list_push_front(find_list(0, words), &blk->elem);
|
||||||
|
|
||||||
|
|
||||||
return coalesce(blk);
|
return coalesce(blk);
|
||||||
@ -326,7 +327,7 @@ static void place(struct block *blck, size_t asize) {
|
|||||||
set_block_free(blck, remaining_size);
|
set_block_free(blck, remaining_size);
|
||||||
|
|
||||||
/* Add remaining free block back into list */
|
/* Add remaining free block back into list */
|
||||||
list_push_front(find_list(remaining_size), &blck->elem);
|
list_push_front(find_list(0, remaining_size), &blck->elem);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
set_block_used(blck, csize);
|
set_block_used(blck, csize);
|
||||||
@ -335,12 +336,15 @@ static void place(struct block *blck, size_t asize) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find free list with blocks of given size
|
* Find free list with blocks of given size
|
||||||
|
*
|
||||||
|
* not_empty == 0, find "any" list
|
||||||
|
* not_empty == 1, find a not empty list
|
||||||
*/
|
*/
|
||||||
static struct list* find_list(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 (list_empty(&free_lists[i])) {
|
if (not_empty && list_empty(&free_lists[i])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return &free_lists[i];
|
return &free_lists[i];
|
||||||
@ -354,7 +358,7 @@ static struct list* find_list(size_t n_words) {
|
|||||||
*/
|
*/
|
||||||
static struct block *find_fit(size_t num_words)
|
static struct block *find_fit(size_t num_words)
|
||||||
{
|
{
|
||||||
struct list* free_list = find_list(num_words);
|
struct list* free_list = find_list(1, num_words);
|
||||||
|
|
||||||
if (free_list == NULL) {
|
if (free_list == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user