The Malloc Library is a custom implementation of standard memory management functions in C: malloc, free, calloc, and realloc. This project aims to deepen the understanding of dynamic memory allocation, pointer manipulation, and system-level programming.
- malloc: Allocates a block of memory of a specified size.
- free: Releases a previously allocated block of memory.
- calloc: Allocates and zeroes multiple blocks of memory.
- realloc: Resizes an existing memory block while preserving its data if possible.
- Bucket-based allocation: Uses fixed-size buckets to minimize fragmentation.
- Optimized alignment: Ensures blocks are aligned to
long doublefor better performance. - Automatic page release: Unmaps empty pages using
munmap. LD_PRELOADcompatible: Can replacemallocin external programs.
malloc/
├── src/
│ ├── bucket.c # Bucket management for memory blocks
│ ├── malloc.c # Public interface for malloc, free, realloc, calloc
│ ├── my_malloc.c # Core implementation of the memory allocator
│ ├── utils.c # Utility functions (alignment, page management, etc.)
│ ├── bucket.h # Header for bucket structures
│ ├── my_malloc.h # Header for memory functions
│ ├── utils.h # Header for utility functions
│ ├── Makefile # Build system for compiling the library
├── tests/ # Provided test binaries for verification
│ ├── corruptionproof # Tests metadata integrity
│ ├── memoryfootprint # Tests memory efficiency
│ ├── speed # Tests allocator performance
└── README.md # This file!- GCC or another compatible C compiler.
- A Unix-based system is recommended for testing (Linux, macOS).
git clone https://github.com/remicku/Malloc-Library.git
cd Malloc-LibraryA Makefile is provided to simplify the build process. Compile the code by running:
make allAvailable targets:
all: Compiles the library and examples.library: Compiles the libraryclean: Removes all compiled files.debug: Enables debugging with gdb.
To clean the project:
make cleanTo use this implementation instead of the standard malloc:
LD_PRELOAD=./libmalloc.so ./your_programExamples:
LD_PRELOAD=./libmalloc.so ls
LD_PRELOAD=./libmalloc.so ./tests/memoryfootprintYou can test the library with various programs by preloading it:
factor 10 20 30 40 50
cat Makefile
ip a
ls
ls -a
find /
tree /
clang --help
firefox
discordThis allocator is based on a bucket system, where each bucket manages blocks of a fixed size.
- Determines the nearest power-of-two block size.
- Checks for an existing bucket with available space.
- If none is found, creates a new bucket using
mmap. - Allocates a free block from the selected bucket.
- Marks the block as free.
- If the entire bucket is empty, it is
munmapped to free system memory.
- If the requested size fits in the same bucket, reuse the block.
- If not, allocate a new block, copy data, and free the old one.
A set of precompiled test binaries is provided to validate your implementation.
Run tests using:
LD_PRELOAD=./libmalloc.so ./tests/<test_binary>| Test Binary | Description |
|---|---|
speed |
Ensures the allocator runs within a fixed time limit. |
memoryfootprint |
Evaluates memory fragmentation and efficiency. |
corruptionproof |
Checks for metadata corruption resistance. |
LD_PRELOAD=./libmalloc.so ./tests/speed; echo $?
# Expected output: 0 (success)LD_PRELOAD=./libmalloc.so ./tests/memoryfootprint 512; echo $?
# Expected output: 50%+ memory efficiency (varies by implementation)LD_PRELOAD=./libmalloc.so ./tests/corruptionproof; echo $?
# Expected output: 0 (success)Debugging a shared library requires special handling.
make debug
gdb ./your_program
(gdb) set env LD_LIBRARY_PATH=.
(gdb) runmake debug
gdb ls
(gdb) set exec-wrapper env 'LD_PRELOAD=./libmalloc.so'
(gdb) runContributions are welcome! Feel free to open an issue or submit a pull request if you have ideas for improvements or new features.