Removed unnecessary ocmments, added predefs for static functions

master
TuDatTr 2021-01-24 21:29:11 +01:00
parent 31028203d8
commit fcff089f20
1 changed files with 20 additions and 47 deletions

65
mm.c
View File

@ -89,7 +89,6 @@ team_t team = {
/* Read and write a word at address p */
#define GET(p) (*(unsigned int *)(p))
#define PUT(p, val) (*(unsigned int *)(p) = (val))
#define PUTT(p, val) (*(unsigned int *)(p) = (val))
/* Read the size and allocated fields from address p */
#define GET_SIZE(p) (GET(p) & ~0x7)
@ -121,13 +120,12 @@ char* seg_list[LISTSIZE];
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// 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);
/* Page 833, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
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);
static void add_free(void* bp, size_t asize);
static void *extend_heap(size_t size);
static void add_free(void *ptr, size_t size);
static void remove_free(void *ptr);
static void *coalesce(void *ptr);
static void *place(void *ptr, size_t asize);
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@ -141,9 +139,9 @@ static void *extend_heap(size_t size)
return NULL;
/* Set Header and Footer*/
PUTT(HDRP(ptr), PACK(asize, 0));
PUTT(FTRP(ptr), PACK(asize, 0));
PUTT(HDRP(NEXT_BLKP(ptr)), PACK(0, 1));
PUT(HDRP(ptr), PACK(asize, 0));
PUT(FTRP(ptr), PACK(asize, 0));
PUT(HDRP(NEXT_BLKP(ptr)), PACK(0, 1));
add_free(ptr, asize);
@ -155,20 +153,17 @@ static void add_free(void *ptr, size_t size) {
void *searching_ptr = ptr;
void *in_ptr = NULL;
/* Select List*/
while ((list < LISTSIZE - 1) && (size > 1)) {
size >>= 1;
list++;
}
/* Search in size-ascending order*/
searching_ptr = seg_list[list];
while ((searching_ptr != NULL) && (size > GET_SIZE(HDRP(searching_ptr)))) {
in_ptr = searching_ptr;
searching_ptr = PRV_BLK(searching_ptr);
}
/* Set previous and next*/
if (searching_ptr != NULL && in_ptr != NULL) {
PUT_PTR(NXT_PTR(searching_ptr), ptr);
PUT_PTR(NXT_PTR(ptr), in_ptr);
@ -200,7 +195,6 @@ static void remove_free(void *ptr) {
int list = 0;
size_t size = GET_SIZE(HDRP(ptr));
/* Select List*/
while ((list < LISTSIZE - 1) && (size > 1)) {
size >>= 1;
list++;
@ -222,13 +216,11 @@ static void remove_free(void *ptr) {
}
static void *coalesce(void *ptr)
{
static void *coalesce(void *ptr) {
size_t prev_alloc = GET_ALLOC(HDRP(PREV_BLKP(ptr)));
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr)));
size_t size = GET_SIZE(HDRP(ptr));
if (prev_alloc && next_alloc) { // Case 1
return ptr;
}
@ -260,37 +252,28 @@ static void *coalesce(void *ptr)
return ptr;
}
static void *place(void *ptr, size_t asize)
{
static void *place(void *ptr, size_t asize) {
size_t ptr_size = GET_SIZE(HDRP(ptr));
size_t remainder = ptr_size - asize;
remove_free(ptr);
if (remainder <= DSIZE * 2) {
// Do not split block
PUT(HDRP(ptr), PACK(ptr_size, 1));
PUT(FTRP(ptr), PACK(ptr_size, 1));
}
else if (asize >= 100) {
// Split block
PUT(HDRP(ptr), PACK(remainder, 0));
PUT(FTRP(ptr), PACK(remainder, 0));
PUTT(HDRP(NEXT_BLKP(ptr)), PACK(asize, 1));
PUTT(FTRP(NEXT_BLKP(ptr)), PACK(asize, 1));
PUT(HDRP(NEXT_BLKP(ptr)), PACK(asize, 1));
PUT(FTRP(NEXT_BLKP(ptr)), PACK(asize, 1));
add_free(ptr, remainder);
return NEXT_BLKP(ptr);
}
else {
// Split block
} else {
PUT(HDRP(ptr), PACK(asize, 1));
PUT(FTRP(ptr), PACK(asize, 1));
PUTT(HDRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
PUTT(FTRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
PUT(HDRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
PUT(FTRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
add_free(NEXT_BLKP(ptr), remainder);
}
return ptr;
@ -305,7 +288,7 @@ static void *place(void *ptr, size_t asize)
*/
int mm_init(void)
{
// init seg_list
// Init seg_list
for (size_t i = 0; i < LISTSIZE; ++i) {
seg_list[i] = NULL;
}
@ -334,13 +317,11 @@ void *mm_malloc(size_t size)
{
size_t asize; /* Adjusted block size */
size_t extendsize; /* Amount to extend heap if no fit */
void *ptr = NULL; /* Pointer */
void *ptr; /* Pointer */
// Ignore size 0 cases
if (size == 0)
return NULL;
// Align block size
if (size <= DSIZE) {
asize = 2 * DSIZE;
} else {
@ -349,11 +330,9 @@ void *mm_malloc(size_t size)
int list = 0;
size_t searchsize = asize;
// Search for free block in segregated list
while (list < LISTSIZE) {
if ((list == LISTSIZE- 1) || ((searchsize <= 1) && (seg_list[list] != NULL))) {
ptr = seg_list[list];
// Ignore blocks that are too small or marked with the reallocation bit
while ((ptr != NULL) && (asize > GET_SIZE(HDRP(ptr))))
{
ptr = PRV_BLK(ptr);
@ -366,7 +345,6 @@ void *mm_malloc(size_t size)
list++;
}
// if free block is not found, extend the heap
if (ptr == NULL) {
extendsize = MAX(asize, CHUNKSIZE);
@ -374,12 +352,7 @@ void *mm_malloc(size_t size)
return NULL;
}
// Place and divide block
ptr = place(ptr, asize);
// Return pointer to newly allocated block
return ptr;
return place(ptr, asize);
}
/* Page 833, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */