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.

§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(1u32);

let stupid_box = Box::new_in(1u32, 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() {
    // ...
}

§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.

§Graphics

Enabling the graphics feature will allow you to create interactive graphical windows that will visually show the contents of the memory you allocate with this allocator. The data will be represented as rows of bytes, that are themselves represented as consecutive 8 bits. Graphically, each bit is shown as a black or white square, where black represents a 0, and white represents a 1.

§Modifying memory contents with the mouse

Clicking with the left mouse button on a square will set the corresponding bit in memory, while using the right mouse button will clear it. You can thus directly modify memory using only your mouse, and directly see the results!

§Creation

By default, allocating memory doesn’t create a corresponding graphical window. To create a window, you can use StupidAlloc::open_window_of(), and you can adjust the number of bytes displayed on each row using StupidAlloc::set_columns_of:

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

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

#[cfg(feature = "graphics")]
{
    // Start with 8 columns.
    StupidAlloc.open_window_of(&*stupid_box, 8);

    // I changed my mind, I want 4 columns instead.
    StupidAlloc.set_columns_of(&*stupid_box, 4);
}

If the always-graphics feature is enabled, then every allocation will be displayed automatically, without the need to call open_window_of().

§Logging

If the logging feature is enabled, each allocation will be accompanied by a companion log file, with the same path and name as the allocation file, but with a .md extension. Inside, information about the allocation will be written as the allocation is interacted with:

  • Metadata, such as corresponding allocation file, the Layout, …
  • Allocation and deallocation backtraces (requires the RUST_BACKTRACE environment variable to be set accordingly)
  • Every grow or shrink, with new Layout and corresponding backtrace

Log files won’t be deleted when the corresponding memory is freed, but they might get overwritten, either by you when using the interactive feature and specifying the same file name as a previous allocation’s, or by subsequent executions of a program that uses this allocator.

§Multi-threading

Internally, the allocator uses a RwLock 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.

Structs§

StupidAlloc
The stupid allocator.