osiris/mem/
alloc.rs

1//! This module provides a simple allocator.
2//! One implementation is the BestFitAllocator, which uses the best fit strategy.
3
4use core::ptr::NonNull;
5
6use hal::mem::PhysAddr;
7
8use crate::error::Result;
9
10pub mod bestfit;
11
12#[cfg(target_pointer_width = "64")]
13pub const MAX_ADDR: usize = 2_usize.pow(48);
14
15#[cfg(target_pointer_width = "32")]
16pub const MAX_ADDR: usize = usize::MAX;
17
18/// Allocator trait that provides a way to allocate and free memory.
19/// Normally you don't need to use this directly, rather use the `boxed::Box` type.
20///
21/// # Safety
22///
23/// Every block returned by `malloc` must be freed by `free` exactly once.
24/// A pointer allocated by one allocator must not be freed by another allocator.
25/// Each range added to the allocator must be valid for the whole lifetime of the allocator and must not overlap with any other range.
26/// The lifetime of any allocation is only valid as long as the allocator is valid. (A pointer must not be used after the allocator is dropped.)
27pub trait Allocator {
28    fn malloc<T>(
29        &mut self,
30        size: usize,
31        align: usize,
32        request: Option<PhysAddr>,
33    ) -> Result<NonNull<T>>;
34    unsafe fn free<T>(&mut self, ptr: NonNull<T>, size: usize);
35}