Memory Allocator Written in Rust

93 points antoniosarosi

I wrote a memory allocator in Rust for learning purposes. The allocator implements std::alloc::Allocator and can be configured with multiple allocation buckets of different sizes at compile time. I carefully documented all the code, especially the data structures, so that anybody who's interested in memory allocators can understand how they work without much extra research. Pretty much all the code uses unsafe because allocators rely heavily on pointer arithmetic (also linked lists, which cannot allocate memory themselves). But the tests can be run with Miri by mocking mmap system calls using the global allocator, since Miri doesn't work with FFI. That really helped me catch a lot of bugs. It also works with multiple threads because I just dropped everything behind a Mutex, but I have ideas for optimizing that such as providing each thread with its own independent allocator. Check it out:
https://github.com/antoniosarosi/memalloc-rust