Added ASCII Art and reorganized code
parent
4a84229678
commit
7a20a7dd86
80
mm.c
80
mm.c
|
@ -34,7 +34,37 @@ team_t team = {
|
|||
/* Second member's email address (leave blank if none) */
|
||||
"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 */
|
||||
#define ALIGNMENT 8
|
||||
|
||||
|
@ -70,9 +100,29 @@ team_t team = {
|
|||
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))
|
||||
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////// 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. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
static void place(void *bp, size_t asize);
|
||||
/* Page 856, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
static void* find_fit(size_t asize);
|
||||
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
static void* coalesce(void *bp);
|
||||
/* Page 831, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
static void* extend_heap(size_t words);
|
||||
/* Page 831, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static void place(void *bp, size_t asize)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/* Page 856, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
static void *find_fit(size_t asize)
|
||||
{
|
||||
static void* first_fit(size_t asize) {
|
||||
/* First fit search */
|
||||
void *bp;
|
||||
|
||||
|
@ -101,6 +148,13 @@ static void *find_fit(size_t asize)
|
|||
}
|
||||
}
|
||||
return NULL; /* No fit */
|
||||
|
||||
}
|
||||
|
||||
/* Page 856, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
static void* find_fit(size_t asize)
|
||||
{
|
||||
return first_fit(asize);
|
||||
}
|
||||
|
||||
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
|
@ -150,13 +204,18 @@ static void *extend_heap(size_t words)
|
|||
return coalesce(bp);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////// MAIN FUNCTIONALITY //////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/* Page 831, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
/*
|
||||
* mm_init - initialize the malloc package.
|
||||
*/
|
||||
int mm_init(void)
|
||||
{
|
||||
for (size_t i = 0; i < LIST_SIZE; ++i) {
|
||||
seg_list[i] = NULL;
|
||||
}
|
||||
/* Create the initial empty heap */
|
||||
if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1)
|
||||
return -1;
|
||||
|
@ -179,10 +238,9 @@ int mm_init(void)
|
|||
*/
|
||||
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 */
|
||||
char *bp;
|
||||
char *bp; /* Pointer */
|
||||
|
||||
/* Ignore spurious requests */
|
||||
if (size == 0)
|
||||
|
@ -193,6 +251,7 @@ void *mm_malloc(size_t size)
|
|||
asize = 2*DSIZE;
|
||||
else
|
||||
asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);
|
||||
|
||||
/* Search the free list for a fit */
|
||||
if ((bp = find_fit(asize)) != NULL) {
|
||||
place(bp, asize);
|
||||
|
@ -241,8 +300,3 @@ void *mm_realloc(void *ptr, size_t size)
|
|||
mm_free(oldptr);
|
||||
return newptr;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
/// Helper Functions
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
|
|
Loading…
Reference in New Issue