Crate memac

source ·
Expand description

A Custom memory allocator using slab and buddy allocators.

Use buddy and slab allocators

use memac::{Allocator, buddy::Buddy32M};
use core::alloc::GlobalAlloc;

let mut alloc = Allocator::<Buddy32M>::new(); // Use 32M memory space.

let heap_size = 32 * 1024 * 1024;
let layout = std::alloc::Layout::from_size_align(heap_size, memac::ALIGNMENT).unwrap();
let ptr = unsafe { std::alloc::alloc(layout) };

alloc.init(ptr as usize, heap_size); // Initialize the allocator.

let layout = std::alloc::Layout::from_size_align(128, 32).unwrap();
let mem = unsafe { alloc.alloc(layout) }; // Allocation.
unsafe { alloc.dealloc(mem, layout) };    // Deallocation.

Use slab allocator only

use memac::{Allocator, pager::PageManager};
use core::alloc::GlobalAlloc;

let mut alloc = Allocator::<PageManager>::new(); // Use a pager.

let heap_size = 32 * 1024 * 1024;
let layout = std::alloc::Layout::from_size_align(heap_size, memac::ALIGNMENT).unwrap();
let ptr = unsafe { std::alloc::alloc(layout) };

alloc.init(ptr as usize, heap_size); // Initialize the allocator.

let layout = std::alloc::Layout::from_size_align(128, 32).unwrap();
let mem = unsafe { alloc.alloc(layout) }; // Allocation.
unsafe { alloc.dealloc(mem, layout) };    // Deallocation.

Modules

Structs

A custom memory allocator.

Constants

Traits