Added remove_free()

master
TuDatTr 2021-01-24 19:40:08 +01:00
parent 0074f194a9
commit 24c7c4e7df
2 changed files with 109 additions and 41 deletions

View File

@ -2,7 +2,8 @@
# Students' Makefile for the Malloc Lab
#
CC = gcc
CFLAGS = -Wall -O2 -m32 -g03 -pg
# CFLAGS = -Wall -O2 -m32 -g03 -pg
CFLAGS = -Wall -O0 -m32 -g03 -pg
OBJS = mdriver.o mm.o memlib.o fsecs.o fcyc.o clock.o ftimer.o

147
mm.c
View File

@ -104,16 +104,64 @@ team_t team = {
#define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (unsigned int)(ptr))
#define PRV_FREE(bp) ((char *)(bp))
#define NXT_FREE(bp) ((char *)(bp) + WSIZE)
#define PRV_PTR(ptr) ((char *)(ptr))
#define NXT_PTR(ptr) ((char *)(ptr) + WSIZE)
#define PRV_BLK(ptr) (*(char **)(ptr))
#define NXT_BLK(ptr) (*(char **)(NXT_PTR(ptr)))
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// STATIC VARS /////////////////////////////////
////////////////////////////////// GLOBAL VARS /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
const size_t LISTSIZE = 11;
#define LISTSIZE 20
static char* heap_listp;
char* seg_list[LISTSIZE];
static void insert_node(void *ptr, size_t size) {
int list = 0;
void *search_ptr = ptr;
void *insert_ptr = NULL;
// Select segregated list
while ((list < LISTSIZE - 1) && (size > 1)) {
size >>= 1;
list++;
}
// Keep size ascending order and search
search_ptr = seg_list[list];
while ((search_ptr != NULL) && (size > GET_SIZE(HDRP(search_ptr)))) {
insert_ptr = search_ptr;
search_ptr = PRV_BLK(search_ptr);
}
// Set predecessor and successor
if (search_ptr != NULL) {
if (insert_ptr != NULL) {
PUT_PTR(PRV_PTR(ptr), search_ptr);
PUT_PTR(NXT_PTR(search_ptr), ptr);
PUT_PTR(NXT_PTR(ptr), insert_ptr);
PUT_PTR(PRV_PTR(insert_ptr), ptr);
} else {
PUT_PTR(PRV_PTR(ptr), search_ptr);
PUT_PTR(NXT_PTR(search_ptr), ptr);
PUT_PTR(NXT_PTR(ptr), NULL);
seg_list[list] = ptr;
}
} else {
if (insert_ptr != NULL) {
PUT_PTR(PRV_PTR(ptr), NULL);
PUT_PTR(NXT_PTR(ptr), insert_ptr);
PUT_PTR(PRV_PTR(insert_ptr), ptr);
} else {
PUT_PTR(PRV_PTR(ptr), NULL);
PUT_PTR(NXT_PTR(ptr), NULL);
seg_list[list] = ptr;
}
}
return;
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// HELPER PREDEF ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@ -125,53 +173,67 @@ static void* find_fit(size_t asize);
static void* coalesce(void *bp);
/* Page 831, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void* extend_heap(size_t words);
/* Page 831, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void add_free(void* bp, size_t asize);
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static void place(void *bp, size_t asize)
{
size_t csize = GET_SIZE(HDRP(bp));
if (asize <= (2*(1 << (LISTSIZE-1)))) {
PUT(HDRP(bp), PACK(csize, 1));
PUT(FTRP(bp), PACK(csize, 1));
} else {
// Sonderfall 10
}
if ((csize - asize) >= (2*DSIZE)) {
PUT(HDRP(bp), PACK(asize, 1));
PUT(FTRP(bp), PACK(asize, 1));
bp = NEXT_BLKP(bp);
PUT(HDRP(bp), PACK(csize-asize, 0));
PUT(FTRP(bp), PACK(csize-asize, 0));
}
else {
}
static void add_free(void* bp, size_t asize) {
insert_node(bp, asize);
}
static int getSzClass(size_t asize) {
for (size_t i = 0; i < LISTSIZE; ++i) {
if (asize == (1 << i)) {
return i;
}
}
return LISTSIZE;
return LISTSIZE-1;
}
static void remove_free(void* bp) {
int szClass = getSzClass(GET_SIZE(HDRP(bp)));
if (PRV_BLK(bp) == NULL && NXT_BLK(bp) == NULL) {
seg_list[szClass] = NULL;
} else if (PRV_BLK(bp) != NULL && NXT_BLK(bp) == NULL) {
PUT_PTR(PRV_PTR(PRV_BLK(bp)), NULL);
seg_list[szClass] = PRV_BLK(bp);
} else if (PRV_BLK(bp) == NULL && NXT_BLK(bp) != NULL) {
PUT_PTR(PRV_PTR(NXT_BLK(bp)), NULL);
} if (PRV_BLK(bp) != NULL && NXT_BLK(bp) != NULL) {
PUT_PTR(NXT_PTR(PRV_BLK(bp)), NXT_BLK(bp));
PUT_PTR(PRV_PTR(NXT_BLK(bp)), PRV_BLK(bp));
}
}
static void place(void *bp, size_t asize)
{
size_t csize = GET_SIZE(HDRP(bp));
remove_free(bp);
PUT(HDRP(bp), PACK(csize, 1));
PUT(FTRP(bp), PACK(csize, 1));
if (asize <= (2*(1 << (LISTSIZE-1)))) {
return;
} else {
bp = NEXT_BLKP(bp);
PUT(HDRP(bp), PACK(csize-asize, 0));
PUT(FTRP(bp), PACK(csize-asize, 0));
add_free(bp, csize-asize);
}
}
static void* best_fit(void* ptr, size_t asize) {
unsigned long left_over = GET_SIZE(ptr) - asize;
void* bestfit = ptr;
int best_left_over = GET_SIZE(ptr) - asize);
while (NXT_FREE(ptr) != NULL) {
left_over = GET_SIZE(ptr) - asize);
unsigned long best_left_over = GET_SIZE(ptr) - asize;
while (NXT_PTR(ptr) != NULL) {
left_over = GET_SIZE(ptr) - asize;
if(left_over<best_left_over){
best_left_over = left_over;
bestfit=ptr;
}
ptr = NXT_FREE(ptr);
ptr = NXT_BLK(ptr);
}
return bestfit;
}
@ -184,8 +246,8 @@ static void* find_fit(size_t asize)
return NULL;
} else {
if (szClass != LISTSIZE -1) {
while (NXT_FREE(classPtr) != NULL) {
classPtr = NXT_FREE(classPtr);
while (NXT_PTR(classPtr) != NULL) {
classPtr = NXT_BLK(classPtr);
}
return classPtr;
} else {
@ -203,21 +265,31 @@ static void *coalesce(void *bp) {
if (prev_alloc && next_alloc) { /* Case 1 */
return bp;
} else if (prev_alloc && !next_alloc) { /* Case 2 */
remove_free(bp);
remove_free(NEXT_BLKP(bp));
size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
PUT(HDRP(bp),PACK(size,0));
PUT(FTRP(bp),PACK(size,0));
} else if (!prev_alloc && next_alloc) { /* Case 3 */
remove_free(bp);
remove_free(PREV_BLKP(bp));
size += GET_SIZE(HDRP(PREV_BLKP(bp)));
PUT(FTRP(bp), PACK(size, 0));
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
bp = PREV_BLKP(bp);
} else { /* Case 4 */
remove_free(bp);
remove_free(PREV_BLKP(bp));
remove_free(NEXT_BLKP(bp));
size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
GET_SIZE(FTRP(NEXT_BLKP(bp)));
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
bp = PREV_BLKP(bp);
}
add_free(bp, size);
return bp;
}
@ -227,10 +299,6 @@ static void *extend_heap(size_t words)
char *bp;
size_t size;
if (words == 0) {
return NULL;
}
/* Allocate an even number of words to maintain alignment */
size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
if ((long)(bp = mem_sbrk(size)) == -1)
@ -241,8 +309,7 @@ static void *extend_heap(size_t words)
PUT(FTRP(bp), PACK(size,0)); /* Free block footer */
PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
PUT_PTR(PRV_FREE(bp), 0);
PUT_PTR(NXT_FREE(bp), 0);
add_free(bp, size);
/* Coalesce if the previous block was free */
return coalesce(bp);