Added new function add_free()

master
anna_schlittenhardt 2021-01-24 19:28:35 +01:00
parent 27e75d248b
commit f8f1f2c067
1 changed files with 19 additions and 8 deletions

27
mm.c
View File

@ -104,8 +104,11 @@ team_t team = {
#define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (unsigned int)(ptr)) #define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (unsigned int)(ptr))
#define PRV_FREE(bp) ((char *)(bp)) #define PRV_PTR(ptr) ((char *)(ptr))
#define NXT_FREE(bp) ((char *)(bp) + WSIZE) #define NXT_PTR(bp) ((char *)(bp) + WSIZE)
#define PRV_BLK(bp) (*(char **)(bp))
#define NXT_BLK(bp) (*(char **)(NXT_PTR(bp)))
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// STATIC VARS ///////////////////////////////// ////////////////////////////////// STATIC VARS /////////////////////////////////
@ -134,6 +137,14 @@ static void add_free(void* ptr, size_t size)
{ {
int szClass = getSzClass(size); int szClass = getSzClass(size);
void* classPtr = seg_list[szClass]; void* classPtr = seg_list[szClass];
while(classPtr != NULL)
{
classPtr = NXT_PTR(classPtr);
}
PUT_PTR(NXT_PTR(classPTR), ptr)
PUT_PTR(PRV_PTR(ptr), classPtr)
PUT_PTR(NXT_PTR(ptr), NULL);
} }
static void place(void *bp, size_t asize) static void place(void *bp, size_t asize)
@ -165,13 +176,13 @@ static void* best_fit(void* ptr, size_t asize) {
unsigned long left_over = GET_SIZE(ptr) - asize; unsigned long left_over = GET_SIZE(ptr) - asize;
void* bestfit = ptr; void* bestfit = ptr;
int best_left_over = GET_SIZE(ptr) - asize); int best_left_over = GET_SIZE(ptr) - asize);
while (NXT_FREE(ptr) != NULL) { while (NXT_PTR(ptr) != NULL) {
left_over = GET_SIZE(ptr) - asize); left_over = GET_SIZE(ptr) - asize);
if(left_over<best_left_over){ if(left_over<best_left_over){
best_left_over = left_over; best_left_over = left_over;
bestfit=ptr; bestfit=ptr;
} }
ptr = NXT_FREE(ptr); ptr = NXT_PTR(ptr);
} }
return bestfit; return bestfit;
} }
@ -184,8 +195,8 @@ static void* find_fit(size_t asize)
return NULL; return NULL;
} else { } else {
if (szClass != LISTSIZE -1) { if (szClass != LISTSIZE -1) {
while (NXT_FREE(classPtr) != NULL) { while (NXT_PTR(classPtr) != NULL) {
classPtr = NXT_FREE(classPtr); classPtr = NXT_PTR(classPtr);
} }
return classPtr; return classPtr;
} else { } else {
@ -241,8 +252,8 @@ static void *extend_heap(size_t words)
PUT(FTRP(bp), PACK(size,0)); /* Free block footer */ PUT(FTRP(bp), PACK(size,0)); /* Free block footer */
PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */ PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
PUT_PTR(PRV_FREE(bp), 0); PUT_PTR(PRV_PTR(bp), 0);
PUT_PTR(NXT_FREE(bp), 0); PUT_PTR(NXT_PTR(bp), 0);
/* Coalesce if the previous block was free */ /* Coalesce if the previous block was free */
return coalesce(bp); return coalesce(bp);