Skip to content

Latest commit

 

History

History
81 lines (55 loc) · 2.62 KB

File metadata and controls

81 lines (55 loc) · 2.62 KB

Tiny Memory Allocator

The project was created just for fun following a cool article here in my free time to brush up my C skills.

⚠️ You should NEVER use this as a real memory allocator in your project.

Implementation details

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.

Compilation

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

Usage

Usage 1. Current terminal session

tmalloc can be easily used as a memory allocator for every program you run from your current terminal session.

  1. Compile shared library
make all
  1. 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
  1. Run any program you want in the current terminal session, even the simplest one like ls will do
  2. Revert your changes by resetting LD_PRELOAD
unset LD_PRELOAD

Usage 2. Link to any of your C programs

  1. Compile shared library
make all
  1. Copy shared library to your project
  2. 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'
  1. To verify that custom allocators loads before default one, run your executable in a specific way:
LD_DEBUG=libs ./my_program.out

Acknowledgments

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.