From 921a49c0fcc104b5baffd3dc4be9f1001219a973 Mon Sep 17 00:00:00 2001 From: TuDatTr Date: Sun, 24 Jan 2021 22:35:46 +0100 Subject: [PATCH] Removed int-to-void-pointer-cast warnings, added comments --- Makefile | 4 ++-- mm.c | 62 ++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index f9f8bed..d4ab256 100644 --- a/Makefile +++ b/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 +CFLAGS = -Wall -O0 -m32 -g03 -pg OBJS = mdriver.o mm.o memlib.o fsecs.o fcyc.o clock.o ftimer.o diff --git a/mm.c b/mm.c index 8b406e0..7c9b3b5 100644 --- a/mm.c +++ b/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) = (unsigned int)(ptr)) +#define PUT_PTR(p, ptr) (*(unsigned int *)(p) = (size_t)(ptr)) #define PRV_PTR(ptr) ((char *)(ptr)) #define NXT_PTR(ptr) ((char *)(ptr) + WSIZE) @@ -120,11 +120,50 @@ char* seg_list[LISTSIZE]; //////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// 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 int getSzClass(size_t asize); //////////////////////////////////////////////////////////////////////////////// @@ -253,27 +292,20 @@ static void *coalesce(void *ptr) { static void *place(void *ptr, size_t asize) { size_t ptr_size = GET_SIZE(HDRP(ptr)); - size_t remainder = ptr_size - asize; + size_t rest = ptr_size - asize; remove_free(ptr); - if (remainder <= DSIZE * 2) { + if (rest <= DSIZE * 2) { PUT(HDRP(ptr), PACK(ptr_size, 1)); PUT(FTRP(ptr), PACK(ptr_size, 1)); - } - else if (asize >= 100) { - PUT(HDRP(ptr), PACK(remainder, 0)); - PUT(FTRP(ptr), PACK(remainder, 0)); + } 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, remainder); + add_free(ptr, rest); return NEXT_BLKP(ptr); - } else { - PUT(HDRP(ptr), PACK(asize, 1)); - PUT(FTRP(ptr), PACK(asize, 1)); - PUT(HDRP(NEXT_BLKP(ptr)), PACK(remainder, 0)); - PUT(FTRP(NEXT_BLKP(ptr)), PACK(remainder, 0)); - add_free(NEXT_BLKP(ptr), remainder); } return ptr; } @@ -331,7 +363,7 @@ void *mm_malloc(size_t size) 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)))) + while (!((ptr == NULL) || (asize <= GET_SIZE(HDRP(ptr))))) { ptr = PRV_BLK(ptr); }