Question
Please help with mm-native.c . I thought I understood it but I can't figure out how to get it working and also satisfy the rules:
Please help with mm-native.c . I thought I understood it but I can't figure out how to get it working and also satisfy the rules:
Programming Rules:
You may not change any of the interfaces in mm.c.
You may not invoke any memory-management related library calls or system calls. Thus, you may NOT use malloc, calloc, free, realloc, sbrk, brk or any variants of these calls in your code.
You may NOT implement your allocator as an implicit list. You MUST create an explicit list scheme, although which specific type of explicit list you choose is your prerogative.
You are not allowed to define any global or static compound data structures such as arrays, structs, trees, or lists in your mm.c program. However, you are allowed to declare global scalar variables such as integers, floats, and pointers in mm.c. Thus, you may use scalar variables as root nodes for tree or list structures superimposed on blocks in the heap.
For consistency with the libc malloc package, which returns blocks aligned on 8-byte boundaries, your allocator must always return pointers to blocks that are aligned to 8-byte boundaries. The driver will enforce this requirement.
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
*
* In this naive approach, a block is allocated by simply incrementing
* the brk pointer. A block is pure payload. There are no headers or
* footers. Blocks are never coalesced or reused. Realloc is
* implemented directly using mm_malloc and mm_free.
*
* NOTE TO STUDENTS: Replace this header comment with your own header
* comment that gives a high level description of your solution.
*/
#include
#include
#include
#include
#include
#include "mm.h"
#include "memlib.h"
/* single word (4) or double word (8) alignment */
#define WSIZE 4
#define DSIZE 8
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
/* some macros (to start you off) */
/* Read a word at address p */
#define GET(p) (*(unsigned int *)(p))
/* Read the size field from address p */
#define GET_SIZE(p) (GET(p) & ~0x7)
/* Given block ptr bp, compute address of its header */
#define HDRP(bp) ((char *)(bp) - WSIZE)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
/*
* mm_init - initialize the malloc package.
*/
int mm_init(void)
{
return 0;
}
/*
* mm_malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void *mm_malloc(size_t size)
{
int newsize = ALIGN(size + SIZE_T_SIZE);
void *p = mem_sbrk(newsize);
if (p == (void *)-1)
return NULL;
else {
*(size_t *)p = size;
return (void *)((char *)p + SIZE_T_SIZE);
}
}
/*
* mm_free - Currently, freeing a block does nothing.
* You must revise this function so that it frees the block.
*/
void mm_free(void *ptr)
{
}
/*
* mm_realloc - Implemented simply in terms of mm_malloc and mm_free
*/
void *mm_realloc(void *ptr, size_t size)
{
void *oldptr = ptr;
void *newptr;
size_t copySize;
newptr = mm_malloc(size);
if (newptr == NULL)
return NULL;
copySize= GET_SIZE(HDRP(ptr)) - DSIZE;
if (size < copySize)
copySize = size;
memcpy(newptr, oldptr, copySize);
mm_free(oldptr);
return newptr;
}
Step 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