Url:
http://www.cs.utk.edu/~plank/plank/classes/cs360/360/labs/lab6/lab.html
This assignment is for you to write malloc(). As always, we will
proceed in several steps.
Part 1: jmalloc1.c
Before doing anything, read the lecture notes of
the first
malloc lecture.
These give you an overview of malloc() and sbrk().
Now, you are to write jmalloc1(int size), which is to work like
malloc():
It returns a chunk of memory to the user which is
at least size bytes. Jmalloc1() is to be as malloc()
is described in the first malloc lecture. It will maintain a buffer
of free memory and allocate memory from this buffer. When the buffer
is too small, it will allocate a new buffer. Implement jfree1() to
do nothing. Use sbrk() to get new memory.
Jmalloc1() should be like malloc() and allocate an extra
8 bytes before the memory which is returned to the user. Like
malloc(), the first 4 of these bytes should be an integer containing
the size of the amount of memory which jmalloc1() carved off
the buffer.
If jmalloc1() runs out of memory, have it return NULL.
Part 2: Porting jmalloc1() to be malloc
When you are confident that jmalloc1() works, move it to
malloc1.c. This should be a file that defines malloc(),
free(), calloc() and realloc(). Once you compile
it, you can link it with other programs (for example those in lab 1),
and they will use your malloc(), free(), calloc(),
and realloc() instead of the standard ones supplied with Unix.
Malloc() should be just like jmalloc1(). Free() should
do nothing -- i.e. free() won't actually free memory.
This is ok -- however
programs using these versions of malloc(), etc, will run out of
memory faster than those that use Unix's malloc(). Read the
man pages on calloc() and realloc(). They can be implemented
with your malloc(). In other words, calloc() calls
malloc() and then bzero() (read the man page). Realloc()
will always reallocate -- it will call malloc(), bcopy(), and
then free().
You must be careful with this port of jmalloc1() to malloc().
For example, if you try to debug by calling printf() you must remember
that printf() may call malloc().
Part 3: jmalloc2() and jfree2()
Now, we want to write jmalloc2() so that you may call jfree2()
to free memory, and it will actually work. Thus, you need to implement
the free list view of malloc() as described in
the
second malloc lecture.
Read those notes if you have not done so already. When you're done,
implement jmalloc2() and jfree2() so that they use a free list.
Jmalloc() takes a chunk of memory off the free list and carves a
piece off for the user, or gives the user the entire chunk. If a piece
was carved off, the remainder should go back onto the free list.
Jfree2() puts a chunk of memory back onto the free list. This is
tricky stuff -- you must be careful programming it.
Part 4: Porting jmalloc2() to be malloc
Now, do as you did in part 2. Copy jmalloc2.c to malloc2.c
and implement malloc(),
free(), calloc() and realloc() in malloc2.c.
Show that it works with code from previous assignments. Again you must
be very careful.
Part 5: Optional
This is only if you want to do it. You will get no credit for this part.
However, it will give you real hacking practice. Implement coalescing
of nodes in the free list. In other words, if the user calls free(),
and the newly freed chunk of memory is adjacent to chunks in the free
list, coalesce them into one chunk. This will help thwart fragmentation
as discussed in class.