StupidAlloc: what if memory allocation was bad actually

406 points FennecAuNaturel

I made a very bad memory allocator that creates and maps a file into memory for every single allocation made. The crate even has a feature that enables graphical dialogues to confirm and provide a file path, if you want even more interactivity and annoyance!

Find all relevant info on GitHub and on crates.io.

Why?

Multiple reasons! I was bored and since I've been working with memory allocators during my day job, I got this very cursed idea as I drifted to sleep. Jolting awake, I rushed to my computer and made this monstrosity, to share with everyone!

While it's incredibly inefficient and definitely not something you want in production, it has its uses: since every single allocation has an associated file, you can pretty much debug raw memory with a common hex editor, instead of having to tinker with /proc/mem or a debugger! Inspect your structures' memory layout, and even change the memory on the fly!

While testing it, I even learned that the process of initializing a Rust program allocates memory for a Thread object, as well as a CStr for the thread's name! It even takes one more allocation on Windows because an intermediate buffer is used to convert the string to UTF-16!

An example, if you don't want to click on the links

use stupidalloc::StupidAlloc;

#[global_allocator]
static GLOBAL: StupidAlloc = StupidAlloc;

fn main() {
    let boxed_vec = Box::new(vec![1, 2, 3]);

    println!("{}", StupidAlloc.file_of(&*boxed_vec).unwrap().display());

    // Somehow pause execution
}

Since the allocator provides helper functions to find the file associated to a value, you can try and pause the program and go inspect a specific memory file! Here, you get the path to the file that contains the Vec struct (and not the Vec's elements!).