man 3
malloc
):
#includeYou have to implement a library that implements these functions to allocate and free dynamic memory.void *malloc(size_t size); void free(void *ptr); void *calloc(size_t nmemb, size_t size); void *realloc(void *ptr, size_t size);
You will need to know about memset
and memcpy
to
implememt calloc and realloc respectively.
free
put a block back on
the free-list for the corresponding bin.
There should be four separate bins for the following allocation sizes:
Allocations of size 32, 128, and 512 bytes should be done from
within the heap, whereas, allocations greater than 512 bytes should be
done using mmap
system call. To manipulate heap, you can
use the sbrk
interface. Alternatively, you can use mmap
to manage the memory regions directly.
sbrk()
or mmap systemcall). The memory request must always be
multiple of PAGE_SIZE
(use the
sysconf(_SC_PAGESIZE)
syscall). If sbrk
fails, you must set errno
to ENOMEM
and
return NULL;
The library should be thread-safe, i.e., it should use proper locks, etc., to allow multiple threads to allocation/free memory.
The malloc directory contains a skeleton malloc.c, Makefile, and a basic test case.
Once you have everything ready, you can use the following benchmarks/tools to test out your malloc library.
You must print the following statistics for each arena when the
program calls malloc_stats()
(see the manpage for more
details):
Your submission should contain a file for each function wrapper (i.e.
malloc.c, free.c, realloc.c, calloc.c) that contains definitions
for malloc, free, realloc, and calloc. These file must not have
main()
and shouldn't have any debugging output. There should
be separate header/C files for all utility functions. For example, all
code to manage a linked list should be in a separate .c file with a
corresponding .h file. A Makefile
should be provided with a rule lib
that generates
libmalloc.so
from this file.
You must also provide a README file that contains: