Removed unnecessary ocmments, added predefs for static functions
parent
31028203d8
commit
fcff089f20
67
mm.c
67
mm.c
|
@ -89,7 +89,6 @@ team_t team = {
|
||||||
/* Read and write a word at address p */
|
/* Read and write a word at address p */
|
||||||
#define GET(p) (*(unsigned int *)(p))
|
#define GET(p) (*(unsigned int *)(p))
|
||||||
#define PUT(p, val) (*(unsigned int *)(p) = (val))
|
#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 */
|
/* Read the size and allocated fields from address p */
|
||||||
#define GET_SIZE(p) (GET(p) & ~0x7)
|
#define GET_SIZE(p) (GET(p) & ~0x7)
|
||||||
|
@ -121,13 +120,12 @@ char* seg_list[LISTSIZE];
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
///////////////////////////////// HELPER PREDEF ////////////////////////////////
|
///////////////////////////////// HELPER PREDEF ////////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/* Page 856-858, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
static void *extend_heap(size_t size);
|
||||||
static void* place(void *bp, size_t asize);
|
static void add_free(void *ptr, size_t size);
|
||||||
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
static void remove_free(void *ptr);
|
||||||
static void* coalesce(void *bp);
|
static void *coalesce(void *ptr);
|
||||||
/* Page 831, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
static void *place(void *ptr, size_t asize);
|
||||||
static void* extend_heap(size_t words);
|
|
||||||
static void add_free(void* bp, size_t asize);
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
|
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -141,9 +139,9 @@ static void *extend_heap(size_t size)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Set Header and Footer*/
|
/* Set Header and Footer*/
|
||||||
PUTT(HDRP(ptr), PACK(asize, 0));
|
PUT(HDRP(ptr), PACK(asize, 0));
|
||||||
PUTT(FTRP(ptr), PACK(asize, 0));
|
PUT(FTRP(ptr), PACK(asize, 0));
|
||||||
PUTT(HDRP(NEXT_BLKP(ptr)), PACK(0, 1));
|
PUT(HDRP(NEXT_BLKP(ptr)), PACK(0, 1));
|
||||||
|
|
||||||
add_free(ptr, asize);
|
add_free(ptr, asize);
|
||||||
|
|
||||||
|
@ -155,26 +153,23 @@ static void add_free(void *ptr, size_t size) {
|
||||||
void *searching_ptr = ptr;
|
void *searching_ptr = ptr;
|
||||||
void *in_ptr = NULL;
|
void *in_ptr = NULL;
|
||||||
|
|
||||||
/* Select List*/
|
|
||||||
while ((list < LISTSIZE - 1) && (size > 1)) {
|
while ((list < LISTSIZE - 1) && (size > 1)) {
|
||||||
size >>= 1;
|
size >>= 1;
|
||||||
list++;
|
list++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Search in size-ascending order*/
|
|
||||||
searching_ptr = seg_list[list];
|
searching_ptr = seg_list[list];
|
||||||
while ((searching_ptr != NULL) && (size > GET_SIZE(HDRP(searching_ptr)))) {
|
while ((searching_ptr != NULL) && (size > GET_SIZE(HDRP(searching_ptr)))) {
|
||||||
in_ptr = searching_ptr;
|
in_ptr = searching_ptr;
|
||||||
searching_ptr = PRV_BLK(searching_ptr);
|
searching_ptr = PRV_BLK(searching_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set previous and next*/
|
|
||||||
if (searching_ptr != NULL && in_ptr != NULL) {
|
if (searching_ptr != NULL && in_ptr != NULL) {
|
||||||
PUT_PTR(NXT_PTR(searching_ptr), ptr);
|
PUT_PTR(NXT_PTR(searching_ptr), ptr);
|
||||||
PUT_PTR(NXT_PTR(ptr), in_ptr);
|
PUT_PTR(NXT_PTR(ptr), in_ptr);
|
||||||
PUT_PTR(PRV_PTR(ptr), searching_ptr);
|
PUT_PTR(PRV_PTR(ptr), searching_ptr);
|
||||||
PUT_PTR(PRV_PTR(in_ptr), ptr);
|
PUT_PTR(PRV_PTR(in_ptr), ptr);
|
||||||
}
|
}
|
||||||
else if (searching_ptr != NULL && in_ptr == NULL) {
|
else if (searching_ptr != NULL && in_ptr == NULL) {
|
||||||
PUT_PTR(PRV_PTR(ptr), searching_ptr);
|
PUT_PTR(PRV_PTR(ptr), searching_ptr);
|
||||||
PUT_PTR(NXT_PTR(searching_ptr), ptr);
|
PUT_PTR(NXT_PTR(searching_ptr), ptr);
|
||||||
|
@ -200,7 +195,6 @@ static void remove_free(void *ptr) {
|
||||||
int list = 0;
|
int list = 0;
|
||||||
size_t size = GET_SIZE(HDRP(ptr));
|
size_t size = GET_SIZE(HDRP(ptr));
|
||||||
|
|
||||||
/* Select List*/
|
|
||||||
while ((list < LISTSIZE - 1) && (size > 1)) {
|
while ((list < LISTSIZE - 1) && (size > 1)) {
|
||||||
size >>= 1;
|
size >>= 1;
|
||||||
list++;
|
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 prev_alloc = GET_ALLOC(HDRP(PREV_BLKP(ptr)));
|
||||||
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr)));
|
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr)));
|
||||||
size_t size = GET_SIZE(HDRP(ptr));
|
size_t size = GET_SIZE(HDRP(ptr));
|
||||||
|
|
||||||
|
|
||||||
if (prev_alloc && next_alloc) { // Case 1
|
if (prev_alloc && next_alloc) { // Case 1
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
@ -260,37 +252,28 @@ static void *coalesce(void *ptr)
|
||||||
return 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 ptr_size = GET_SIZE(HDRP(ptr));
|
||||||
size_t remainder = ptr_size - asize;
|
size_t remainder = ptr_size - asize;
|
||||||
|
|
||||||
remove_free(ptr);
|
remove_free(ptr);
|
||||||
|
|
||||||
|
|
||||||
if (remainder <= DSIZE * 2) {
|
if (remainder <= DSIZE * 2) {
|
||||||
// Do not split block
|
|
||||||
PUT(HDRP(ptr), PACK(ptr_size, 1));
|
PUT(HDRP(ptr), PACK(ptr_size, 1));
|
||||||
PUT(FTRP(ptr), PACK(ptr_size, 1));
|
PUT(FTRP(ptr), PACK(ptr_size, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (asize >= 100) {
|
else if (asize >= 100) {
|
||||||
// Split block
|
|
||||||
PUT(HDRP(ptr), PACK(remainder, 0));
|
PUT(HDRP(ptr), PACK(remainder, 0));
|
||||||
PUT(FTRP(ptr), PACK(remainder, 0));
|
PUT(FTRP(ptr), PACK(remainder, 0));
|
||||||
PUTT(HDRP(NEXT_BLKP(ptr)), PACK(asize, 1));
|
PUT(HDRP(NEXT_BLKP(ptr)), PACK(asize, 1));
|
||||||
PUTT(FTRP(NEXT_BLKP(ptr)), PACK(asize, 1));
|
PUT(FTRP(NEXT_BLKP(ptr)), PACK(asize, 1));
|
||||||
add_free(ptr, remainder);
|
add_free(ptr, remainder);
|
||||||
return NEXT_BLKP(ptr);
|
return NEXT_BLKP(ptr);
|
||||||
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
// Split block
|
|
||||||
PUT(HDRP(ptr), PACK(asize, 1));
|
PUT(HDRP(ptr), PACK(asize, 1));
|
||||||
PUT(FTRP(ptr), PACK(asize, 1));
|
PUT(FTRP(ptr), PACK(asize, 1));
|
||||||
PUTT(HDRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
|
PUT(HDRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
|
||||||
PUTT(FTRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
|
PUT(FTRP(NEXT_BLKP(ptr)), PACK(remainder, 0));
|
||||||
add_free(NEXT_BLKP(ptr), remainder);
|
add_free(NEXT_BLKP(ptr), remainder);
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
|
@ -305,7 +288,7 @@ static void *place(void *ptr, size_t asize)
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -334,13 +317,11 @@ 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 */
|
||||||
void *ptr = NULL; /* Pointer */
|
void *ptr; /* Pointer */
|
||||||
|
|
||||||
// Ignore size 0 cases
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// Align block size
|
|
||||||
if (size <= DSIZE) {
|
if (size <= DSIZE) {
|
||||||
asize = 2 * DSIZE;
|
asize = 2 * DSIZE;
|
||||||
} else {
|
} else {
|
||||||
|
@ -349,11 +330,9 @@ void *mm_malloc(size_t size)
|
||||||
|
|
||||||
int list = 0;
|
int list = 0;
|
||||||
size_t searchsize = asize;
|
size_t searchsize = asize;
|
||||||
// Search for free block in segregated list
|
|
||||||
while (list < LISTSIZE) {
|
while (list < LISTSIZE) {
|
||||||
if ((list == LISTSIZE- 1) || ((searchsize <= 1) && (seg_list[list] != NULL))) {
|
if ((list == LISTSIZE- 1) || ((searchsize <= 1) && (seg_list[list] != NULL))) {
|
||||||
ptr = seg_list[list];
|
ptr = seg_list[list];
|
||||||
// Ignore blocks that are too small or marked with the reallocation bit
|
|
||||||
while ((ptr != NULL) && (asize > GET_SIZE(HDRP(ptr))))
|
while ((ptr != NULL) && (asize > GET_SIZE(HDRP(ptr))))
|
||||||
{
|
{
|
||||||
ptr = PRV_BLK(ptr);
|
ptr = PRV_BLK(ptr);
|
||||||
|
@ -366,7 +345,6 @@ void *mm_malloc(size_t size)
|
||||||
list++;
|
list++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if free block is not found, extend the heap
|
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
extendsize = MAX(asize, CHUNKSIZE);
|
extendsize = MAX(asize, CHUNKSIZE);
|
||||||
|
|
||||||
|
@ -374,12 +352,7 @@ void *mm_malloc(size_t size)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place and divide block
|
return place(ptr, asize);
|
||||||
ptr = place(ptr, asize);
|
|
||||||
|
|
||||||
|
|
||||||
// Return pointer to newly allocated block
|
|
||||||
return ptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||||
|
|
Loading…
Reference in New Issue