DOC PREVIEW
Harvey Mudd CS 105 - Malloc Lab: Writing a Dynamic Storage Allocator

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 105Malloc Lab: Writing a Dynamic Storage AllocatorSee Web page for due date1 IntroductionIn this lab you will be writing a dynamic storage allocator for C programs, i.e., your own version of themalloc, free and realloc routines. You are encouraged to explore the design space creatively andimplement an allocator that is correct, efficient and fast.2 LogisticsAs usual, you must work in pairs. Any clarifications and revisions to the assignment will be e-mailed to theclass list or posted on the course Web page.3 Handout InstructionsStart by downloading malloclab-handout.tar from the Web page to a protected directory in whichyou plan to do your work. Then give the command: tar xvf malloclab-handout.tar. Thiswill cause a number of files to be unpacked into the directory. The only file you will be modifying andhanding in is mm.c. The mdriver.c program is a driver program that allows you to evaluate the perfor-mance 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.)As with other labs, mm.c contains a C structure named team, which you should fill in with informationabout your programming team. Do this right away so you don’t forget.When you have completed the lab, you will submit only one file (mm.c), which contains your solution.4 Getting a Low ScoreThe malloc lab is by far the most difficult lab in CS 105. The primary reason students have problems is thatthey follow poor programming practices. Here are some good ways to make the lab a disaster:1• Don’t read the hints at the end of this handout. Just start work right away using the information onpage 1.• Don’t put any effort into the heap checker. Writing a thorough heap checker is difficult and requirescareful thinking. Never mind that the time invested pays off tenfold; just go straight to the lab.• Be sloppy and take shortcuts. Convince yourself that you’ll fix your sloppiness later. Take hastyapproaches because you don’t want to waste time writing a correct helper function. Slap in temporarycode of the “Hey, why don’t we try adding 4?” variety to see if it fixes your bug, and then just leaveit there while you get distracted by the next problem.• Only call the heap checker once. It slows your code down; besides, it keeps crashing your programwith an error message you don’t understand. Better to just turn it off.• Don’t use paper to diagram your ideas. It’s easy to keep track of all those pointer manipulations inyour head, right? Why kill trees drawing them out to make sure you understand what’s going on?On the other hand, if you want to make this lab a breeze, start with diagrams, design a heap checker that canhandle all of the conditions listed below, and be maniacal about neatness.5 How to Work on the LabYour dynamic storage allocator will consist of the following four functions, which are declared in mm.hand 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 the simplest but still functionally correct malloc package thatwe could think of. Using this as a starting place, modify these functions (and possibly define other privatestatic functions), so that they obey the following semantics:• mm init: Before calling mm malloc mm 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 toperform any necessary initialization, such as allocating the initial heap area. The return value shouldbe 0 if all was OK, and -1 if there was a problem during initialization.Note: mm init may be called more than once by the driver. It should not remember any externalstate, and should not assume that it is only called once. Each call to mm init should reset theallocator, forgetting everything that has gone before.2• mm malloc: The mm malloc routine returns a pointer to an allocated block of at least size bytes.The entire allocated block should lie within the heap region and should not overlap with any otherallocated chunk. Note that the returned value should point to the “payload”—the area available foruse by the caller—rather than to whatever header you might choose to put on the block.We will comparing your implementation to the version of malloc supplied in the standard C library(libc). The libc malloc always returns payload pointers that are aligned to 8 bytes, which isexcessive on Linux; your malloc implementation can gain a few utilization points by aligning onlyto 4 bytes.In the Linux specification, it is legal to allocate 0 bytes; your implementation should behave gracefullyin this case. It is up to you whether to return NULL or to return a non-NULL pointer that can legallybe passed to mm free.• mm free: The mm free routine frees the block pointed to by ptr. It returns nothing. This rou-tine is only guaranteed to work when the passed pointer (ptr) was returned by an earlier call tomm malloc or mm realloc and has not yet been freed.In the Linux specification, NULL can be passed to free. You can write mm free to accept NULLpointers, but the test driver will never exercise this case.• mm realloc: The mm realloc routine returns a pointer to an allocated region of at least sizebytes with the following constraints.– If ptr is NULL, the call is equivalent to mm malloc(size);– If size is equal to zero, the call is equivalent to mm free(ptr);– If ptr is not NULL, it must have been returned by an earlier call to either mm malloc ormm realloc. The call to mm realloc changes the size of the memory block pointed to byptr (the old block) to size bytes and returns the address of the new block. Notice that theaddress of the new block might be the same as the old block, or it might be different, dependingon your implementation, the amount of internal fragmentation in the old block, and the size ofthe realloc request.The contents of the new block are the same as those of the old ptr block, up to the minimum ofthe old and new sizes. Everything else is uninitialized. For example, if the old block is 8 bytesand the new block is 12 bytes, then the first 8 bytes of the new block are identical to the first 8bytes of the old block and the last 4 bytes are uninitialized. Similarly, if the old block is 8 bytesand the new block is 4 bytes, then the contents of the new block are identical to the first 4


View Full Document

Harvey Mudd CS 105 - Malloc Lab: Writing a Dynamic Storage Allocator

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Malloc Lab: Writing a Dynamic Storage Allocator
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Malloc Lab: Writing a Dynamic Storage Allocator and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Malloc Lab: Writing a Dynamic Storage Allocator 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?