Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have to implement malloc in C. To get started with this assignment I see that there are some macros defined. My assignment says
I have to implement malloc in C. To get started with this assignment I see that there are some macros defined. My assignment says "
For consistency with the C standard library's malloc package, which returns blocks aligned on 16-byte boundaries, your allocator must always return pointers that are aligned to 16-byte boundaries. The tester will enforce this requirement for you."
I 'm confused how do I modify the alignment here to return that? Or am does this code already do that?
/ single word (4) or double word (8) alignment #define ALIGNMENT 8 /rounds up to the nearest multiple of ALIGNMENT #define ALIGN(size) (((size) + (ALIGNMENT-1)) &~0x7) #define SIZE-T-SIZE (ALIGN(sizeof (size_t))) /*Additional Macros defined*/ #define SIZE 4 #define DSIZE 8 #define CHUNKSIZE, 16 #define OVERHEAD 24 #define MAX(x ,y) ((x) > (y) ? (x) ; (y) ) #define PACK(size, alloc) ((size) 1 (alloc)) #define GET(p) (*( size_t *) (p)) #define PUT(p, value) (*(size_t *) (p) (value)) #define GET SIZE (p) (GET (p) &~0x7) #define GET ALLOC(p) (GET(p) & 0x1) #define HDRP (bp) ( (void *) (bp) - SIZE) #define FTRP (bp) ( (void *) (bp) + GET-SIZE (HDRP (bp)) -DSIZE) #define NEXT BLKP(bp) ( (void *) (bp) + GET-SIZE (HDRP (bp))) #define PREV-BLKP(bp) ((void *) (bp) -GET-SIZE (HDRP (bp) -WSIZE)) #define NEXT-FREEP(bp) (*(void **) (bp + DSIZE)) #define PREV-FREEP(bp) (*(void **) (bp)) //Size of a word //Size of a double word //Initial heap size //The minimum block size //Finds the maximum of two numbers //Put the size and allocated byte into one word //Read the word at address p //Write the word at address p //Get the size from header/footer //Get the allocated bit from header/footer //Get the address of the header of a block //Get the address of the footer of a block //Get the address of the next block //Get the address of the previous block //Get the address of the next free block //Get the address of the previous free block / single word (4) or double word (8) alignment #define ALIGNMENT 8 /rounds up to the nearest multiple of ALIGNMENT #define ALIGN(size) (((size) + (ALIGNMENT-1)) &~0x7) #define SIZE-T-SIZE (ALIGN(sizeof (size_t))) /*Additional Macros defined*/ #define SIZE 4 #define DSIZE 8 #define CHUNKSIZE, 16 #define OVERHEAD 24 #define MAX(x ,y) ((x) > (y) ? (x) ; (y) ) #define PACK(size, alloc) ((size) 1 (alloc)) #define GET(p) (*( size_t *) (p)) #define PUT(p, value) (*(size_t *) (p) (value)) #define GET SIZE (p) (GET (p) &~0x7) #define GET ALLOC(p) (GET(p) & 0x1) #define HDRP (bp) ( (void *) (bp) - SIZE) #define FTRP (bp) ( (void *) (bp) + GET-SIZE (HDRP (bp)) -DSIZE) #define NEXT BLKP(bp) ( (void *) (bp) + GET-SIZE (HDRP (bp))) #define PREV-BLKP(bp) ((void *) (bp) -GET-SIZE (HDRP (bp) -WSIZE)) #define NEXT-FREEP(bp) (*(void **) (bp + DSIZE)) #define PREV-FREEP(bp) (*(void **) (bp)) //Size of a word //Size of a double word //Initial heap size //The minimum block size //Finds the maximum of two numbers //Put the size and allocated byte into one word //Read the word at address p //Write the word at address p //Get the size from header/footer //Get the allocated bit from header/footer //Get the address of the header of a block //Get the address of the footer of a block //Get the address of the next block //Get the address of the previous block //Get the address of the next free block //Get the address of the previous free blockStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started