malloc- Memory Allocation
Operating systems provide functionality through the system call
mechanism. System calls tend to be low-level routines that are closely tied
to basic system functionality. To make these features more usable, higher level
system libraries implement higher-level abstractions. For example, consider
the difference between
printf(). For this
assignment you will take on the role of a system library developer and implement
your own version of the C memory allocation routine-
In this lab, you will:
sbrk()system call to request memory from the operating system
This is a solo assignment. Please do not collaborate with other students on this assignment.
void *malloc( size_t size );
void free( void *ptr );
void *calloc( size_t num_of_elts, size_t elt_size );
void *realloc( void *pointer, size_t size);
Link or compile any C/C++ program.
This is an individual assignment. You must write this in C (use the gcc compiler), not in C++.
The goal of this assignment is to replace the memory allocation routines that
are provided by the C/C++ standard library. The
malloc function is called by
programmers to request a contiguous block of memory. The
free function frees
a block of memory previously allocated, so it can be reused. The
realloc functions are minor variants of
Read the man page for these functions for more details. In particular,
is defined in the include file
<sys/types.h> and you can treat it like any other
You are to create a file which contains code implementing the C library functions
This is not a complete program, so you'll need to write test programs as well.
malloc should maintain a list of free memory blocks and fill incoming requests
for memory from the list. When searching the free list for a block of suffcient
size, use the first-fit method. If no large enough block is found, create a new
free block by calling
sbrk(). The new free block should be a multiple of the
system page size, and large enough to fill the request. If
sbrk() fails, set
ENOMEM and return
NULL, as described in
malloc man page.
If the requested block is smaller than the found free block, you'll need to split
the free block into two pieces: one that stays on the free list, and one that
malloc can return.
malloc should always return values that are divisible by 8 (long word aligned).
free function frees memory allocated by malloc. It should simply add the
block back onto the free list. If
free is called with a
NULL pointer, it returns. If
free is called with a pointer not allocated by
malloc, the results are undefined.
It would be nice if
free combined adjacent free blocks, but that is not required.
Start by writing (or gathering) programs that call
Make sure you understand what they do, and test them with the built in
Call your functions
mycalloc until you're
really, really sure everything works. Once you rename them
they get called by constructors, by stream I/O, and in all sorts of places that
will cause programs to fail utterly if the memory functions aren't working. This
is the reason you are required to use C rather than C++ for this assignment.
mymalloc first and test it thoroughly before attempting the other three
functions. Test these functions by writing a separate program which has a
main() and makes calls to
myfree, etc. In your test program, you'll
need to declare
myfree, ... as
malloc, use a global variable to keep track of the first block on the
linked list of free blocks. At the beginning of each block of memory, leave extra
space for a structure that contains the size of the block and a pointer to the
next block in linked list. (Use a doubly linked list if you prefer.)
sysconf(_SC_PAGESIZE) to find the system's page size when you need to
request new memory from the system.
Since blocks need to be long word aligned, you'll need to be able to round a number up to the nearest multiple of 8. This is surprisingly tricky. Write a function to do this and test it separately.
Here is a sample "bad" version of malloc that works, but conforms to very few of the specifications given: bad_malloc.c.
Once you're _really_ sure
malloc is working, write
free. Test some more, and then
calloc function should call
memset to clear the newly allocated block. The
malloc to make a new block,
memcpy to move the old information
into the new block, and then
free to release the old block. Beware that in
realloc, the block can be resized larger or smaller, and the number of bytes
you copy is the smaller of the old and new sizes.
When you're sure everything is working, rename your functions
calloc. If you now compile any program along with your malloc.c,
they will get called instead of the standard C/C++ memory allocators.
I use the programs
heavily test your memory allocators. Feel free to test your functions with these programs as well.
malloc.cand your email.
Create a .tgz archive of your lab directory and email it to
Your submission must contain a file
which contains definitions for
calloc but has no
and with all output (
The simple syntax for creating a .tgz archive is as follows:
tar -zvcf new_archive.tgz lab_directory
The syntax for unpacking a .tgz archive is:
tar -zvxf archive.tgz
Note that your archive must not include any binary executable files, meaning any compiled programs or intermediate build objects (.o files, for example). This will cause your email to be rejected by most services.