Starting from:

$30

CSE4100-Project 3 Solved

1      Introduction
In this lab you will be writing a dynamic storage allocator for C programs; that is, your own version of the malloc, free, realloc functions. You are encouraged to explore the design space creatively and implement an allocator that is correct, efficient, and fast.

2       Materials
You can start the assignment by downloading prj3-malloc.tar from cyber campus to a directory in which you plan to do your work. Then give the command tar xvf prj3-malloc.tar. This will cause a number of files to be unpacked into the directory.

•    Requirements: The only file you will be modifying and turning in is mm.c, which contains your solution.

•    Test Driver Program: The mdriver.c program is a driver program that allows you to locally evaluate the performance of your solution in the same way that TA will evaluate your final submission. Use the command make to generate the driver code and run it with the command ./mdriver.

3        Project Description
Your dynamic storage allocator will consist of the following functions, which are declared in mm.h and defined in mm.c:

int mm init(void);

void mm malloc(size t size); void mm free(void *ptr);

void mm realloc(void *ptr, size t size);

The mm.c file we have given you implements everything correctly but naively. Using this as a starting place, modify these functions (and possibly define other private static functions), so that they obey the following semantics:

•   mm init: Before calling mm  mallocmm realloc or mm free, the application program (i.e., the trace-driven driver program that you will use to evaluate your implementation) calls mm init to perform any necessary initialization, such as allocating the initial heap area. The return value should be -1 if there was a problem in performing the initialization, 0 otherwise.

•   mm malloc: The mm  malloc routine returns a pointer to an allocated block payload of at least size bytes. The entire allocated block should lie within the heap region and should not overlap with any other allocated chunk.

We will be comparing your implementation to the version of malloc supplied in the standard C library (libc). Since the libc malloc always returns payload pointers that are aligned to 8 bytes, your malloc implementation should do likewise and always return 8-byte aligned pointers.

•   mm free: The mm free routine frees the block pointed to by ptr. It returns nothing. This routine is only guaranteed to work when the passed pointer (ptr) was returned by an earlier call to mm  malloc or mm realloc and has not yet been freed.

•   mm realloc: The mm realloc routine returns a pointer to an allocated region of at least size bytes with the following constraints.

1.    if ptr is NULL, the call is equivalent to mm  malloc(size);

2.    if size is equal to zero, the call is equivalent to mm  free(ptr).

3.    if ptr is not NULL, it must have been returned by an earlier call to mm malloc or mm realloc. The call to mm realloc changes the size of the memory block pointed to by ptr (the old block) to size bytes and returns the address of the new block. Notice that the address of the new block might be the same as the old block, or it might be different, depending on your implementation, the amount of internal fragmentation in the old block, and the size of the realloc request.

The contents of the new block are the same as those of the old ptr block, up to the minimum of the old and new sizes. Everything else is uninitialized. For example, if the old block is 8 bytes and the new block is 12 bytes, then the first 8 bytes of the new block are identical to the first 8 bytes of the old block and the last 4 bytes are uninitialized. Similarly, if the old block is 8 bytes and the new block is 4 bytes, then the contents of the new block are identical to the first 4 bytes of the old block.

These semantics match the the semantics of the corresponding libc malloc, realloc, and free routines. Type man malloc to the shell for complete documentation.

4        Heap Consistency Checker
Dynamic memory allocators are notoriously tricky beasts to program correctly and efficiently. They are difficult to program correctly because they involve a lot of untyped pointer manipulation. You will find it very helpful to write a heap checker that scans the heap and checks it for consistency.

Some examples of what a heap checker might check are:

•   Is every block in the free list marked as free?

•   Are there any contiguous free blocks that somehow escaped coalescing?

•   Is every free block actually in the free list?

•   Do the pointers in the free list point to valid free blocks?

•   Do any allocated blocks overlap?

•   Do the pointers in a heap block point to valid heap addresses?

One of your possible heap checkers will consist of the function int mm check(void) in mm.c. It will check any invariants or consistency conditions you consider prudent. It returns a nonzero value if and only if your heap is consistent. You are not limited to the listed suggestions nor are you required to check all of them. You are encouraged to print out error messages when mm check fails.

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. Additional points will be given for your mm check function. Make sure to put in comments and descriptions about what you are checking in the document.

5       Supporting Routines
The memlib.c package simulates the memory system for your dynamic memory allocator. You can invoke the following functions in memlib.c:

•   void *mem sbrk(int incr): Expands the heap by incr bytes, where incr is a positive non-zero integer and returns a generic pointer to the first byte of the newly allocated heap area. The semantics are identical to the Unix sbrk function, except that mem  sbrk accepts only a positive non-zero integer argument.

•   void *mem heaplo(void): Returns a generic pointer to the first byte in the heap.

•   void *mem heap hi(void): Returns a generic pointer to the last byte in the heap.

•   size t mem heapsize(void): Returns the current size of the heap in bytes.

•   size t mem pagesize(void): Returns the system’s page size in bytes (4K on Linux systems).

6        The Trace-driven Driver Program
The driver program mdriver.c in the prj3-malloc.tar distribution tests your mm.c package for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files that are included in the prj3-malloc.tar distribution. Each trace file contains a sequence of allocate, reallocate, and free directions that instruct the driver to call your mm malloc, mm realloc, and mm free routines in some sequence. The driver and the trace files are the same ones we will use when we grade your handin mm.c file.

The driver mdriver.c accepts the following command line arguments:

•   -t <tracedir>: Look for the default trace files in directory tracedir instead of the default directory defined in config.h.

•   -f <tracefile>: Use one particular tracefile for testing instead of the default set of tracefiles.

•   -h: Print a summary of the command line arguments.

•   -l: Run and measure libc malloc in addition to the student’s malloc package.

•   -v: Verbose output. Print a performance breakdown for each tracefile in a compact table.

•   -V: More verbose output. Prints additional diagnostic information as each trace file is processed. Useful during debugging for determining which trace file is causing your malloc package to fail.

7       Programming Rules
•   You should not change any of the interfaces in mm.c.

•   You should not invoke any memory-management related library calls or system calls. This excludes the use of malloc, calloc, free, realloc, sbrk, brk or any variants of these calls in your code.

•   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.

•   For consistency with the libc malloc package, which returns blocks aligned on 8-byte boundaries, your allocator must always return pointers that are aligned to 8-byte boundaries. The driver will enforce this requirement for you.

More products