Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Feb 16, 2024
1 parent 09a199c commit ab066c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
27 changes: 25 additions & 2 deletions core/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,41 @@ pub use rust::RustAllocator;
pub type RawMemPtr = *mut u8;

/// The allocator interface
///
/// # Safety
/// Failure to implement this trait correctly will result in undefined behavior.
/// - `alloc` must return a either a null pointer or a pointer to an available region of memory
/// atleast `size` bytes and aligned to the size of `usize`.
/// - `realloc` must either return a null pointer or return a pointer to an available region of
/// memory atleast `new_size` bytes and aligned to the size of `usize`.
/// - `usable_size` must return the amount of available memory for any allocation allocated with
/// this allocator.
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "allocator")))]
pub trait Allocator {
pub unsafe trait Allocator {
/// Allocate new memory
unsafe fn alloc(&mut self, size: usize) -> RawMemPtr;
///
///
fn alloc(&mut self, size: usize) -> RawMemPtr;

/// De-allocate previously allocated memory
///
/// # Safety
/// Caller must ensure that the pointer that is being deallocated was allocated by the same
/// Allocator instance.
unsafe fn dealloc(&mut self, ptr: RawMemPtr);

/// Re-allocate previously allocated memory
///
/// # Safety
/// Caller must ensure that the pointer points to an allocation that was allocated by the same
/// Allocator instance.
unsafe fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr;

/// Get usable size of allocated memory region
///
/// # Safety
/// Caller must ensure that the pointer handed to this function points to an allocation
/// allocated by the same allocator instance.
unsafe fn usable_size(ptr: RawMemPtr) -> usize
where
Self: Sized;
Expand Down
10 changes: 5 additions & 5 deletions core/src/allocator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn round_size(size: usize) -> usize {
/// The allocator which uses Rust global allocator
pub struct RustAllocator;

impl Allocator for RustAllocator {
unsafe fn alloc(&mut self, size: usize) -> RawMemPtr {
unsafe impl Allocator for RustAllocator {
fn alloc(&mut self, size: usize) -> RawMemPtr {
let size = round_size(size);
let alloc_size = size + HEADER_SIZE;
let layout = if let Ok(layout) = Layout::from_size_align(alloc_size, ALLOC_ALIGN) {
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Allocator for RustAllocator {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn usable_size(ptr: RawMemPtr) -> usize {
unsafe fn usable_size(ptr: RawMemPtr) -> usize {
let ptr = unsafe { ptr.sub(HEADER_SIZE) };
let header = unsafe { &*(ptr as *const Header) };
header.size
Expand All @@ -115,7 +115,7 @@ mod test {

struct TestAllocator;

impl Allocator for TestAllocator {
unsafe impl Allocator for TestAllocator {
unsafe fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr {
let res = RustAllocator.alloc(size);
ALLOC_SIZE.fetch_add(RustAllocator::usable_size(res), Ordering::AcqRel);
Expand Down Expand Up @@ -143,7 +143,7 @@ mod test {
res
}

fn usable_size(ptr: crate::allocator::RawMemPtr) -> usize
unsafe fn usable_size(ptr: crate::allocator::RawMemPtr) -> usize
where
Self: Sized,
{
Expand Down

0 comments on commit ab066c3

Please sign in to comment.