Lab4 - SS10, 2011 Lab Assignment 4: Dynamic Memory...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
SS10, 2011 Lab Assignment 4: Dynamic Memory Allocator Assigned: June 3 Due: June 20, 19:59:59 PM Introduction 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. Logistics You should finish this lab by yourself. How to Work on the Lab Your dynamic storage allocator will consist of the following four functions ( mm_init , mm_malloc , mm_free and mm_realloc ), which are declared in mm.h” and defined in “ mm.c” . The “ mm.c” file we have given you implements the simplest but still functionally correct malloc package that we could think of. Using this as a starting point, modify these functions (and possibly define other private static functions), so that they obey the following semantics: 1. mm_init : Before calling mm_malloc mm_realloc or mm_free , the application program (i.e., the trace-driven test harness that you will use to evaluate your implementation) calls mm_init to perform any
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
necessary initializations, such as allocating the initial heap area. The return value should be -1 if there was a problem in performing the initialization, 0 otherwise. 2. 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 compare 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. 3. 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. 4. mm_realloc : The mm_realloc routine returns a pointer to an allocated region of at least size bytes with the following constraints. o if ptr is NULL, the call is equivalent to mm_malloc ( size ); o if size is equal to zero, the call is equivalent to mm_free ( pt r); o 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/19/2011 for the course CS 000 taught by Professor Jing during the Fall '10 term at Fudan University.

Page1 / 7

Lab4 - SS10, 2011 Lab Assignment 4: Dynamic Memory...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online