Added ASCII Art and reorganized code

master
TuDatTr 2021-01-24 13:55:21 +01:00
parent 4a84229678
commit 7a20a7dd86
1 changed files with 68 additions and 14 deletions

82
mm.c
View File

@ -34,7 +34,37 @@ team_t team = {
/* Second member's email address (leave blank if none) */ /* Second member's email address (leave blank if none) */
"tuan-dat.tran@stud.uni-due.de" "tuan-dat.tran@stud.uni-due.de"
}; };
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// STRUCTURE //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Filled Blk
// 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// HEADER | SIZE | A|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// BODY | ALLOCED MEMORY |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// FOOTER | SIZE | A|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//
// Empty Blk
// 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// HEADER | SIZE | A|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | PTR TO PREV FREE BLK |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | PTR TO NEXT FREE BLK |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// BODY | UNALLOC MEMORY |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// FOOTER | SIZE | A|
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// DEFINES ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/* single word (4) or double word (8) alignment */ /* single word (4) or double word (8) alignment */
#define ALIGNMENT 8 #define ALIGNMENT 8
@ -70,9 +100,29 @@ 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)))
static char *heap_listp; ////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// STATIC VARS /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
const size_t LIST_SIZE = 100;
static char* heap_listp;
static char* seg_list[LIST_SIZE];
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// HELPER PREDEF ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/* Page 856-858, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */ /* 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 856, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void* find_fit(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);
/* Page 831, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static void place(void *bp, size_t asize) static void place(void *bp, size_t asize)
{ {
size_t csize = GET_SIZE(HDRP(bp)); size_t csize = GET_SIZE(HDRP(bp));
@ -88,10 +138,7 @@ static void place(void *bp, size_t asize)
PUT(FTRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1));
} }
} }
static void* first_fit(size_t asize) {
/* Page 856, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void *find_fit(size_t asize)
{
/* First fit search */ /* First fit search */
void *bp; void *bp;
@ -101,6 +148,13 @@ static void *find_fit(size_t asize)
} }
} }
return NULL; /* No fit */ return NULL; /* No fit */
}
/* Page 856, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
static void* find_fit(size_t asize)
{
return first_fit(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. */
@ -150,13 +204,18 @@ static void *extend_heap(size_t words)
return coalesce(bp); return coalesce(bp);
} }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////// MAIN FUNCTIONALITY //////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/* Page 831, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */ /* Page 831, Pearson R. Bryant D. OHallaron. Computer Systems: A Programmers Perspective.3rd Edition, Pearson, 2003. */
/* /*
* mm_init - initialize the malloc package. * mm_init - initialize the malloc package.
*/ */
int mm_init(void) int mm_init(void)
{ {
for (size_t i = 0; i < LIST_SIZE; ++i) {
seg_list[i] = NULL;
}
/* Create the initial empty heap */ /* Create the initial empty heap */
if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1)
return -1; return -1;
@ -179,10 +238,9 @@ int mm_init(void)
*/ */
void *mm_malloc(size_t size) void *mm_malloc(size_t size)
{ {
size_t asize; size_t asize; /* Adjusted block size */
/* 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; char *bp; /* Pointer */
/* Ignore spurious requests */ /* Ignore spurious requests */
if (size == 0) if (size == 0)
@ -193,6 +251,7 @@ void *mm_malloc(size_t size)
asize = 2*DSIZE; asize = 2*DSIZE;
else else
asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE); asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);
/* Search the free list for a fit */ /* Search the free list for a fit */
if ((bp = find_fit(asize)) != NULL) { if ((bp = find_fit(asize)) != NULL) {
place(bp, asize); place(bp, asize);
@ -241,8 +300,3 @@ void *mm_realloc(void *ptr, size_t size)
mm_free(oldptr); mm_free(oldptr);
return newptr; return newptr;
} }
////////////////////////////////////////////////////////////////
/// Helper Functions
////////////////////////////////////////////////////////////////