Compare commits

...

12 Commits

Author SHA1 Message Date
TuDatTr 8a3fc25ab1 Bugfix 2021-01-24 22:58:46 +01:00
TuDatTr a801e2638a Final version? 2021-01-24 22:57:04 +01:00
TuDatTr ad1dd76932 Fixed performance of getSz 2021-01-24 22:53:37 +01:00
TuDatTr 921a49c0fc Removed int-to-void-pointer-cast warnings, added comments 2021-01-24 22:35:46 +01:00
TuDatTr a8a03d0d36 while -> for 2021-01-24 21:56:10 +01:00
TuDatTr 96fcf9f5c4 Added getSizeClass 2021-01-24 21:46:07 +01:00
TuDatTr fcff089f20 Removed unnecessary ocmments, added predefs for static functions 2021-01-24 21:29:11 +01:00
anna_schlittenhardt 31028203d8 Modified helper functions 2021-01-24 21:18:50 +01:00
TuDatTr b1a5273123 Perf index = 57 (util) + 40 (thru) = 97/100 2021-01-24 20:23:57 +01:00
TuDatTr 6d390de8a2 Merge master 2021-01-24 19:52:07 +01:00
anna_schlittenhardt f8f1f2c067 Added new function add_free() 2021-01-24 19:28:35 +01:00
anna_schlittenhardt 27e75d248b Added new function add_free() 2021-01-24 18:08:02 +01:00
2 changed files with 211 additions and 205 deletions

View File

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

390
mm.c
View File

@ -102,7 +102,7 @@ team_t team = {
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE))) #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE))) #define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))
#define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (unsigned int)(ptr)) #define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (size_t)(ptr))
#define PRV_PTR(ptr) ((char *)(ptr)) #define PRV_PTR(ptr) ((char *)(ptr))
#define NXT_PTR(ptr) ((char *)(ptr) + WSIZE) #define NXT_PTR(ptr) ((char *)(ptr) + WSIZE)
@ -117,202 +117,198 @@ team_t team = {
static char* heap_listp; static char* heap_listp;
char* seg_list[LISTSIZE]; 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 //////////////////////////////// ///////////////////////////////// HELPER PREDEF ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/* Page 856-858, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */ /**
static void place(void *bp, size_t asize); @brief Extends the memory of the heap by the specified amount.
/* Page 856, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void* find_fit(size_t asize); @param[in] size amount of memory to extend heap by
/* Page 833, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */ @result Pointer to the newly allocated memory area.
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 size);
static void* extend_heap(size_t words);
static void add_free(void* bp, size_t asize); /**
@brief Adds a free memory location to the segregated list.
@param[in] ptr Pointer of the memory location to be added
@param[in] size size of the area that would be occupied
*/
static void add_free(void *ptr, size_t size);
/**
@brief Removes a free memory location to the segregated list.
@param[in] ptr Pointer of the memory location to be removed
*/
static void remove_free(void *ptr);
/**
@brief Coalesce memory areas around the given pointer, given that they're free.
@param[in] ptr Memory area to look into.
*/
static void *coalesce(void *ptr);
/**
@brief Places The header and footer for our memory blocks.
@param[in] ptr pointer to the memory block that's allocated
@param[in] asize size of memory to allocate
*/
static void *place(void *ptr, size_t asize);
/**
@brief Get the size class of the given size inside of the segregated list
@param[in] asize size class to look for in segregated list
@result Size class (i.e. index of segregated list)
*/
static inline int getSzClass(size_t asize);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPER FUNCTIONS /////////////////////////////// /////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void add_free(void* bp, size_t asize) { static inline int getSzClass(size_t asize) {
insert_node(bp, asize); int i;
} for (i = 0; i < LISTSIZE-1; ++i) {
static int getSzClass(size_t asize) { if (asize <= 1)
for (size_t i = 0; i < LISTSIZE; ++i) { break;
if (asize == (1 << i)) { asize >>= 1;
}
return i; return i;
}
}
return LISTSIZE-1;
} }
static void remove_free(void* bp) { static void *extend_heap(size_t size)
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)); size_t asize;
asize = ALIGN(size);
void *ptr;
remove_free(bp); if ((ptr = mem_sbrk(asize)) == (void *)-1)
return NULL;
/* Set Header and Footer*/
PUT(HDRP(ptr), PACK(asize, 0));
PUT(FTRP(ptr), PACK(asize, 0));
PUT(HDRP(NEXT_BLKP(ptr)), PACK(0, 1));
add_free(ptr, asize);
return coalesce(ptr);
}
static void add_free(void *ptr, size_t size) {
int szClass = getSzClass(size);
void* class_ptr = seg_list[szClass];
void *pre_ptr = NULL;
while ((class_ptr != NULL) && (size > GET_SIZE(HDRP(class_ptr)))) {
pre_ptr = class_ptr;
class_ptr = PRV_BLK(class_ptr);
}
if (class_ptr != NULL && pre_ptr != NULL) {
PUT_PTR(NXT_PTR(class_ptr), ptr);
PUT_PTR(NXT_PTR(ptr), pre_ptr);
PUT_PTR(PRV_PTR(ptr), class_ptr);
PUT_PTR(PRV_PTR(pre_ptr), ptr);
}
else if (class_ptr != NULL && pre_ptr == NULL) {
PUT_PTR(PRV_PTR(ptr), class_ptr);
PUT_PTR(NXT_PTR(class_ptr), ptr);
PUT_PTR(NXT_PTR(ptr), NULL);
seg_list[szClass] = ptr;
}
else if(class_ptr == NULL && pre_ptr != NULL) {
PUT_PTR(NXT_PTR(ptr), pre_ptr);
PUT_PTR(PRV_PTR(ptr), NULL);
PUT_PTR(PRV_PTR(pre_ptr), ptr);
}
else {
PUT_PTR(NXT_PTR(ptr), NULL);
PUT_PTR(PRV_PTR(ptr), NULL);
seg_list[szClass] = ptr;
}
PUT(HDRP(bp), PACK(csize, 1));
PUT(FTRP(bp), PACK(csize, 1));
if (asize <= (2*(1 << (LISTSIZE-1)))) {
return; return;
}
static void remove_free(void *ptr) {
size_t size = GET_SIZE(HDRP(ptr));
int szClass = getSzClass(size);
if (PRV_BLK(ptr) != NULL && NXT_BLK(ptr) != NULL) {
PUT_PTR(NXT_PTR(PRV_BLK(ptr)), NXT_BLK(ptr));
PUT_PTR(PRV_PTR(NXT_BLK(ptr)), PRV_BLK(ptr));
} else if(PRV_BLK(ptr) != NULL && NXT_BLK(ptr) == NULL) {
PUT_PTR(NXT_PTR(PRV_BLK(ptr)), NULL);
seg_list[szClass] = PRV_BLK(ptr);
}
else if(PRV_BLK(ptr) == NULL && NXT_BLK(ptr) != NULL) {
PUT_PTR(PRV_PTR(NXT_BLK(ptr)), NULL);
} else { } else {
bp = NEXT_BLKP(bp); seg_list[szClass] = NULL;
PUT(HDRP(bp), PACK(csize-asize, 0));
PUT(FTRP(bp), PACK(csize-asize, 0));
add_free(bp, csize-asize);
} }
return;
} }
static void* best_fit(void* ptr, size_t asize) {
unsigned long left_over = GET_SIZE(ptr) - asize; static void *coalesce(void *ptr) {
void* bestfit = ptr; size_t prev_alloc = GET_ALLOC(HDRP(PREV_BLKP(ptr)));
unsigned long best_left_over = GET_SIZE(ptr) - asize; size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr)));
while (NXT_PTR(ptr) != NULL) { size_t size = GET_SIZE(HDRP(ptr));
left_over = GET_SIZE(ptr) - asize;
if(left_over<best_left_over){ if (prev_alloc && next_alloc) { // Case 1
best_left_over = left_over; return ptr;
bestfit=ptr;
} }
ptr = NXT_BLK(ptr); else if (prev_alloc && !next_alloc) { // Case 2
remove_free(ptr);
remove_free(NEXT_BLKP(ptr));
size += GET_SIZE(HDRP(NEXT_BLKP(ptr)));
PUT(HDRP(ptr), PACK(size, 0));
PUT(FTRP(ptr), PACK(size, 0));
} else if (!prev_alloc && next_alloc) { // Case 3
remove_free(ptr);
remove_free(PREV_BLKP(ptr));
size += GET_SIZE(HDRP(PREV_BLKP(ptr)));
PUT(FTRP(ptr), PACK(size, 0));
PUT(HDRP(PREV_BLKP(ptr)), PACK(size, 0));
ptr = PREV_BLKP(ptr);
} else { // Case 4
remove_free(ptr);
remove_free(PREV_BLKP(ptr));
remove_free(NEXT_BLKP(ptr));
size += GET_SIZE(HDRP(PREV_BLKP(ptr))) + GET_SIZE(HDRP(NEXT_BLKP(ptr)));
PUT(HDRP(PREV_BLKP(ptr)), PACK(size, 0));
PUT(FTRP(NEXT_BLKP(ptr)), PACK(size, 0));
ptr = PREV_BLKP(ptr);
} }
return bestfit;
add_free(ptr, size);
return ptr;
} }
/* Page 856, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void* find_fit(size_t asize) static void *place(void *ptr, size_t asize) {
{ size_t ptr_size = GET_SIZE(HDRP(ptr));
int szClass = getSzClass(asize); size_t rest = ptr_size - asize;
void* classPtr = seg_list[szClass];
if (classPtr == NULL) { remove_free(ptr);
return NULL;
if (rest <= DSIZE * 2) {
PUT(HDRP(ptr), PACK(ptr_size, 1));
PUT(FTRP(ptr), PACK(ptr_size, 1));
} else { } else {
if (szClass != LISTSIZE -1) { PUT(HDRP(ptr), PACK(rest, 0));
while (NXT_PTR(classPtr) != NULL) { PUT(FTRP(ptr), PACK(rest, 0));
classPtr = NXT_BLK(classPtr); PUT(HDRP(NEXT_BLKP(ptr)), PACK(asize, 1));
PUT(FTRP(NEXT_BLKP(ptr)), PACK(asize, 1));
add_free(ptr, rest);
return NEXT_BLKP(ptr);
} }
return classPtr; return ptr;
} else {
return best_fit(classPtr, asize);
}
}
}
/* Page 833, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void *coalesce(void *bp) {
size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
size_t size = GET_SIZE(HDRP(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;
}
/* Page 831, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void *extend_heap(size_t words)
{
char *bp;
size_t size;
/* Allocate an even number of words to maintain alignment */
size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
if ((long)(bp = mem_sbrk(size)) == -1)
return NULL;
/* Initialize free block header/footer and the epilogue header */
PUT(HDRP(bp), PACK(size,0)); /* Free block header */
PUT(FTRP(bp), PACK(size,0)); /* Free block footer */
PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
add_free(bp, size);
/* Coalesce if the previous block was free */
return coalesce(bp);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -324,7 +320,7 @@ static void *extend_heap(size_t words)
*/ */
int mm_init(void) int mm_init(void)
{ {
// init seg_list // Init seg_list
for (size_t i = 0; i < LISTSIZE; ++i) { for (size_t i = 0; i < LISTSIZE; ++i) {
seg_list[i] = NULL; seg_list[i] = NULL;
} }
@ -353,30 +349,39 @@ void *mm_malloc(size_t size)
{ {
size_t asize; /* Adjusted block size */ size_t asize; /* Adjusted block size */
size_t extendsize; /* Amount to extend heap if no fit */ size_t extendsize; /* Amount to extend heap if no fit */
char *bp; /* Pointer */ void *ptr; /* Pointer */
/* Ignore spurious requests */
if (size == 0) if (size == 0)
return NULL; return NULL;
/* Adjust block size to include overhead and alignment reqs. */ if (size <= DSIZE) {
if (size <= DSIZE) asize = 2 * DSIZE;
asize = 2*DSIZE; } else {
else
asize = ALIGN(size+DSIZE); asize = ALIGN(size+DSIZE);
/* Search the free list for a fit */
if ((bp = find_fit(asize)) != NULL) {
place(bp, asize);
return bp;
} }
/* No fit found. Get more memory and place the block */ size_t sz = asize;
extendsize = MAX(asize,CHUNKSIZE); for (int i = 0;i < LISTSIZE; ++i) {
if ((bp = extend_heap(extendsize/WSIZE)) == NULL) if ((i == LISTSIZE-1) || ((sz < 2) && (seg_list[i] != NULL))) {
ptr = seg_list[i];
while (!((ptr == NULL) || (asize <= GET_SIZE(HDRP(ptr)))))
{
ptr = PRV_BLK(ptr);
}
if (ptr != NULL)
break;
}
sz >>= 1;
}
if (ptr == NULL) {
extendsize = MAX(asize, CHUNKSIZE);
if ((ptr = extend_heap(extendsize)) == NULL)
return NULL; return NULL;
place(bp, asize); }
return bp;
return place(ptr, asize);
} }
/* Page 833, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */ /* Page 833, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
@ -386,6 +391,7 @@ void *mm_malloc(size_t size)
void mm_free(void *ptr) void mm_free(void *ptr)
{ {
size_t size = GET_SIZE(HDRP(ptr)); size_t size = GET_SIZE(HDRP(ptr));
add_free(ptr, size);
PUT(HDRP(ptr), PACK(size, 0)); PUT(HDRP(ptr), PACK(size, 0));
PUT(FTRP(ptr), PACK(size, 0)); PUT(FTRP(ptr), PACK(size, 0));