In this lab you will be writing a dynamic storage allocator for C programs, i.e., your own version of the malloc, free and realloc routines. You are encouraged to explore the design space creatively and implement an allocator that is correct, efficient and fast.
The only file you will be modifying and handing in is mm.c.
The mdriver.c program is a driver program that allows you to evaluate the performance of your solution.
Use the command make to generate the driver code and run it with the command ./mdriver -V. (The -V flag displays helpful summary information.)
How to Work on the Lab
Your dynamic storage allocator will consist of the following four functions, which are declared in mm.h and defined in mm.c.
1 2 3 4 5 6 7 8 9 10 11
intmm_init(void); //return value should be -1 if there was a problem in performing the initialization, 0 otherwise
void *mm_malloc(size_t size); // returns a pointer to an allocated block payload of at least size bytes.
voidmm_free(void *ptr); //frees the block pointed to by ptr. It returns nothing
void *mm_realloc(void *ptr, size_t size); //returns a pointer to an allocated region of at least size bytes with the following constraints.
Heap Consistency Checker
You will find it very helpful to write a heap checker that scans the heap and checks it for consistency.
Your heap checker will consist of the function int mm_check(void) in mm.c.
This consistency checker is for your own debugging during development. When you submit mm.c, make sure to remove any calls to mm_check as they will slow down your throughput.
Support Routines
You can invoke the following functions in memlib.c:
1 2 3 4 5
void *mem_sbrk(int incr); void *mem_heap_lo(void); void *mem_heap_hi(void); size t mem_heapsize(void); size t mem_pagesize(void);
The Trace-driven Driver Program
The driver program mdriver.c in the malloclab-handout.tar distribution tests your mm.c package for correctness, space utilization, and throughput.
Programming Rules
You should not invoke any memory-management related library calls or system calls.
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
Evaluation
Hints
During initial development, using tiny trace files will simplify debugging and testing
Don’t start working on your allocator until you understand everything about the simple implicit list allocator on the textbook
Tips
arithmetic of pointers pointing tovoid will result in undefined behavior(in C standard), although in gccvoid* is treated as char* when doing pointer arithmetic
/*****************************************************************************/ /* * 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) { size_t newsize = ALIGN(size); int found = 0 ; char * res = NULL ;