man 3
malloc):
#include
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
struct mallinfo mallinfo();
void malloc_stats();
Other relevant functions that are typically part of a malloc library
should also be implemented as you find appropriate. Mention any
additional functions that you have implement in your README.
You have to implement a library that implements these functions to
allocate and free dynamic memory.
You will need to know about memset and memcpy to
implememt calloc and realloc respectively.
sbrk() 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.
Per-Thread Malloc Arenas:
__thread to declare thread-specific
global data. For example, for keeping a per-thread arena, you can use:
__thread struct MyMallocArena *arena = NULL;
...
void *malloc(size_t size) {
...
if (arena == NULL) {
initialize_arena(arena);
}
...
pthread_mutex_lock(&arena->lock);
void *ptr = allocate_block(arena, size);
pthread_mutex_unlock(&arena->lock);
...
return ptr;
}
pthread_key_create with pthread_get_specific
and pthread_set_specific functions. The
pthread_key_create functions allows you to also register a
destructor that will be called when the thread exits. This
destruction can be used to perform any thread-specific cleanup and/or
memory-reclaimation. See the man pages for more details.
If a thread calls fork() to create a child process, can your
library handle it? Try running a process with one thread calling
fork() while other is calling malloc()
continuously.
The hw3 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):
struct mallinfo mallinfo() which would return the struct that
your library has been maintaining. For example, at the end of a malloc
call, before returning the pointer to the user, you'd do something like:
stats.uordblks += size;
Similarly for free, etc. If there are any member fields that are
unclear or irrelevant, you can ignore them and mention it in your
README. I hope this is useful.
Your submission should contain a file for each function wrapper (i.e.
malloc.c, free.c, realloc.c, calloc.c, mallinfo.c, etc.)
that contains definitions for malloc, free, realloc, calloc, mallinfo,
etc. 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: