The project was created just for fun following a cool article here in my free time to brush up my C skills.
tmalloc
is a shared library (.so
). Its full filename is libtmalloc.so
.
The library implements 4 main memory management functions:
The implementation is very inefficient due to tracking and managing memory with a linked list. But again, the library is for education purpose and has no real-world application whatsoever.
You can use any Linux distro with GCC toolchain available. Even WSL2 works completely fine.
Compilation process is pretty straightforward and uses Makefile
.
You can compile everything with a good old well-known command:
make all
Or you can simply run the command bellow to build shared library and run main executable to verify that everything works fine.
make run
tmalloc
can be easily used as a memory allocator for every program you run from your current terminal session.
- Compile shared library
make all
- Run the following command from project's root to force OS to load your functions instead of system's default ones
export LD_PRELOAD=$PWD/out/libtmalloc.so
- Run any program you want in the current terminal session, even the simplest one like
ls
will do - Revert your changes by resetting
LD_PRELOAD
unset LD_PRELOAD
- Compile shared library
make all
- Copy shared library to your project
- Compile your program linking custom allocator and hinting loader where to look for it during runtime:
gcc my_program.c -o my_program.out -L. -ltmalloc -Wl,-rpath='$ORIGIN'
- To verify that custom allocators loads before default one, run your executable in a specific way:
LD_DEBUG=libs ./my_program.out
Thanks one more time to this awesome article and its repo that can be found over here.
The article is listed in Project Based Learning GitHub repo.