Compare commits
No commits in common. "8a3fc25ab19feab09514a3a69c58e5a0a8eac460" and "24c7c4e7df4798f9bded80e1cadcc82d0d5b626e" have entirely different histories.
8a3fc25ab1
...
24c7c4e7df
4
Makefile
4
Makefile
|
@ -2,8 +2,8 @@
|
|||
# Students' Makefile for the Malloc Lab
|
||||
#
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -O2 -m32
|
||||
# CFLAGS = -Wall -O0 -m32 -g03 -pg
|
||||
# CFLAGS = -Wall -O2 -m32 -g03 -pg
|
||||
CFLAGS = -Wall -O0 -m32 -g03 -pg
|
||||
|
||||
OBJS = mdriver.o mm.o memlib.o fsecs.o fcyc.o clock.o ftimer.o
|
||||
|
||||
|
|
398
mm.c
398
mm.c
|
@ -102,7 +102,7 @@ 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)))
|
||||
|
||||
#define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (size_t)(ptr))
|
||||
#define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (unsigned int)(ptr))
|
||||
|
||||
#define PRV_PTR(ptr) ((char *)(ptr))
|
||||
#define NXT_PTR(ptr) ((char *)(ptr) + WSIZE)
|
||||
|
@ -117,198 +117,202 @@ team_t team = {
|
|||
static char* heap_listp;
|
||||
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 ////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
@brief Extends the memory of the heap by the specified amount.
|
||||
|
||||
@param[in] size amount of memory to extend heap by
|
||||
@result Pointer to the newly allocated memory area.
|
||||
*/
|
||||
static void *extend_heap(size_t size);
|
||||
|
||||
/**
|
||||
@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);
|
||||
|
||||
/* 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);
|
||||
static void add_free(void* bp, size_t asize);
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////// HELPER FUNCTIONS ///////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static inline int getSzClass(size_t asize) {
|
||||
int i;
|
||||
for (i = 0; i < LISTSIZE-1; ++i) {
|
||||
if (asize <= 1)
|
||||
break;
|
||||
asize >>= 1;
|
||||
static void add_free(void* bp, size_t asize) {
|
||||
insert_node(bp, asize);
|
||||
}
|
||||
static int getSzClass(size_t asize) {
|
||||
for (size_t i = 0; i < LISTSIZE; ++i) {
|
||||
if (asize == (1 << i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return LISTSIZE-1;
|
||||
}
|
||||
|
||||
static void *extend_heap(size_t size)
|
||||
static void remove_free(void* bp) {
|
||||
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 asize;
|
||||
asize = ALIGN(size);
|
||||
void *ptr;
|
||||
size_t csize = GET_SIZE(HDRP(bp));
|
||||
|
||||
if ((ptr = mem_sbrk(asize)) == (void *)-1)
|
||||
remove_free(bp);
|
||||
|
||||
PUT(HDRP(bp), PACK(csize, 1));
|
||||
PUT(FTRP(bp), PACK(csize, 1));
|
||||
if (asize <= (2*(1 << (LISTSIZE-1)))) {
|
||||
return;
|
||||
} else {
|
||||
bp = NEXT_BLKP(bp);
|
||||
PUT(HDRP(bp), PACK(csize-asize, 0));
|
||||
PUT(FTRP(bp), PACK(csize-asize, 0));
|
||||
add_free(bp, csize-asize);
|
||||
}
|
||||
}
|
||||
|
||||
static void* best_fit(void* ptr, size_t asize) {
|
||||
unsigned long left_over = GET_SIZE(ptr) - asize;
|
||||
void* bestfit = ptr;
|
||||
unsigned long best_left_over = GET_SIZE(ptr) - asize;
|
||||
while (NXT_PTR(ptr) != NULL) {
|
||||
left_over = GET_SIZE(ptr) - asize;
|
||||
if(left_over<best_left_over){
|
||||
best_left_over = left_over;
|
||||
bestfit=ptr;
|
||||
}
|
||||
ptr = NXT_BLK(ptr);
|
||||
}
|
||||
return bestfit;
|
||||
}
|
||||
/* 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)
|
||||
{
|
||||
int szClass = getSzClass(asize);
|
||||
void* classPtr = seg_list[szClass];
|
||||
if (classPtr == NULL) {
|
||||
return NULL;
|
||||
} else {
|
||||
if (szClass != LISTSIZE -1) {
|
||||
while (NXT_PTR(classPtr) != NULL) {
|
||||
classPtr = NXT_BLK(classPtr);
|
||||
}
|
||||
return classPtr;
|
||||
} else {
|
||||
return best_fit(classPtr, asize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s 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. O’Hallaron. Computer Systems: A Programmer’s 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;
|
||||
|
||||
/* Set Header and Footer*/
|
||||
PUT(HDRP(ptr), PACK(asize, 0));
|
||||
PUT(FTRP(ptr), PACK(asize, 0));
|
||||
PUT(HDRP(NEXT_BLKP(ptr)), PACK(0, 1));
|
||||
/* 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(ptr, asize);
|
||||
add_free(bp, size);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
seg_list[szClass] = NULL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
add_free(ptr, size);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void *place(void *ptr, size_t asize) {
|
||||
size_t ptr_size = GET_SIZE(HDRP(ptr));
|
||||
size_t rest = ptr_size - asize;
|
||||
|
||||
remove_free(ptr);
|
||||
|
||||
if (rest <= DSIZE * 2) {
|
||||
PUT(HDRP(ptr), PACK(ptr_size, 1));
|
||||
PUT(FTRP(ptr), PACK(ptr_size, 1));
|
||||
} else {
|
||||
PUT(HDRP(ptr), PACK(rest, 0));
|
||||
PUT(FTRP(ptr), PACK(rest, 0));
|
||||
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 ptr;
|
||||
/* Coalesce if the previous block was free */
|
||||
return coalesce(bp);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -320,7 +324,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;
|
||||
}
|
||||
|
@ -349,39 +353,30 @@ void *mm_malloc(size_t size)
|
|||
{
|
||||
size_t asize; /* Adjusted block size */
|
||||
size_t extendsize; /* Amount to extend heap if no fit */
|
||||
void *ptr; /* Pointer */
|
||||
char *bp; /* Pointer */
|
||||
|
||||
/* Ignore spurious requests */
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
if (size <= DSIZE) {
|
||||
/* Adjust block size to include overhead and alignment reqs. */
|
||||
if (size <= DSIZE)
|
||||
asize = 2*DSIZE;
|
||||
} else {
|
||||
else
|
||||
asize = ALIGN(size+DSIZE);
|
||||
|
||||
/* Search the free list for a fit */
|
||||
if ((bp = find_fit(asize)) != NULL) {
|
||||
place(bp, asize);
|
||||
return bp;
|
||||
}
|
||||
|
||||
size_t sz = asize;
|
||||
for (int i = 0;i < LISTSIZE; ++i) {
|
||||
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) {
|
||||
/* No fit found. Get more memory and place the block */
|
||||
extendsize = MAX(asize,CHUNKSIZE);
|
||||
|
||||
if ((ptr = extend_heap(extendsize)) == NULL)
|
||||
if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return place(ptr, asize);
|
||||
place(bp, asize);
|
||||
return bp;
|
||||
}
|
||||
|
||||
/* Page 833, Pearson R. Bryant – D. O’Hallaron. Computer Systems: A Programmer’s Perspective.3rd Edition, Pearson, 2003. */
|
||||
|
@ -391,7 +386,6 @@ void *mm_malloc(size_t size)
|
|||
void mm_free(void *ptr)
|
||||
{
|
||||
size_t size = GET_SIZE(HDRP(ptr));
|
||||
add_free(ptr, size);
|
||||
PUT(HDRP(ptr), PACK(size, 0));
|
||||
PUT(FTRP(ptr), PACK(size, 0));
|
||||
|
||||
|
|
Loading…
Reference in New Issue