Merge master

master
TuDatTr 2021-01-24 19:52:07 +01:00
commit 6d390de8a2
1 changed files with 32 additions and 66 deletions

98
mm.c
View File

@ -117,51 +117,6 @@ team_t team = {
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 ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@ -177,9 +132,6 @@ static void add_free(void* bp, size_t asize);
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
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)) {
@ -189,6 +141,36 @@ static int getSzClass(size_t asize) {
return LISTSIZE-1;
}
static void add_free(void* ptr, size_t size)
{
int szClass = getSzClass(size);
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)
{
size_t csize = GET_SIZE(HDRP(bp));
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 {
PUT(HDRP(bp), PACK(csize, 1));
PUT(FTRP(bp), PACK(csize, 1));
}
}
static void remove_free(void* bp) {
int szClass = getSzClass(GET_SIZE(HDRP(bp)));
@ -205,24 +187,6 @@ static void remove_free(void* 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;
@ -237,6 +201,7 @@ static void* best_fit(void* ptr, size_t asize) {
}
return bestfit;
}
/* Page 856, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void* find_fit(size_t asize)
{
@ -386,6 +351,7 @@ void *mm_malloc(size_t size)
void mm_free(void *ptr)
{
size_t size = GET_SIZE(HDRP(ptr));
add_free(ptr, size);
PUT(HDRP(ptr), PACK(size, 0));
PUT(FTRP(ptr), PACK(size, 0));