Crate stupidalloc

source ·
Expand description

A very stupid but maybe educational memory allocator.

Behaviour

This Allocator will create, open and use a file for every single allocation performed through it. Obviously, doing this imples allocating stuff, which is kind of problematic. So, as a fallback, this allocator uses System when allocating during a memory allocation or de-allocation.

Interactivty

By default, the allocator will silently and automatically allocate memory (as you would expect), by opening files in a temporary folder (as dictated by std::env::temp_dir()). A feature flag, interactive, will enable confirmation and file picking dialogs to pop up during allocations and de-allocations. More specifically:

  • On allocation, a confirmation message detailling the Layout needed for the allocation, followed by file picking dialog. If the first confirmation message is denied, or if no file is provided, the allocation fails.
  • On de-allocation, a confirmation message showing the address of the thing that was de-allocated shows up. It doesn’t matter how it is handled, the de-allocation won’t fail because of it.

Multi-threading

Internally, the allocator uses a Mutex when allocating and de-allocating. As such, using this in a multi-threaded context will yield even more awful performance. Performance is not the goal, but be warned nonetheless.

Usage example

Use the allocator for a few items while keeping the global normal allocator

#![feature(allocator_api)] // You need this for the `new_in` functions. Requires nightly.
use stupidalloc::StupidAlloc;

let normal_box = Box::new(1);

let stupid_box = Box::new_in(1, StupidAlloc);

Use the allocator as the global allocator. Warning: funky stuff may happen, such as allocations before main!

use stupidalloc::StupidAlloc;

#[global_allocator]
static GLOBAL: StupidAlloc = StupidAlloc;

fn main() {
    // ...
}

Structs