Skip to content

Commit

Permalink
Mark allocator functions as unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Feb 11, 2024
1 parent 343b21b commit 0ac0dc0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions core/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub type RawMemPtr = *mut u8;
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "allocator")))]
pub trait Allocator {
/// Allocate new memory
fn alloc(&mut self, size: usize) -> RawMemPtr;
unsafe fn alloc(&mut self, size: usize) -> RawMemPtr;

/// De-allocate previously allocated memory
fn dealloc(&mut self, ptr: RawMemPtr);
unsafe fn dealloc(&mut self, ptr: RawMemPtr);

/// Re-allocate previously allocated memory
fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr;
unsafe fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr;

/// Get usable size of allocated memory region
fn usable_size(ptr: RawMemPtr) -> usize
Expand Down
12 changes: 6 additions & 6 deletions core/src/allocator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn round_size(size: usize) -> usize {
pub struct RustAllocator;

impl Allocator for RustAllocator {
fn alloc(&mut self, size: usize) -> RawMemPtr {
unsafe 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 All @@ -61,7 +61,7 @@ impl Allocator for RustAllocator {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn dealloc(&mut self, ptr: RawMemPtr) {
unsafe fn dealloc(&mut self, ptr: RawMemPtr) {
let ptr = unsafe { ptr.sub(HEADER_SIZE) };
let alloc_size = {
let header = unsafe { &*(ptr as *const Header) };
Expand All @@ -73,7 +73,7 @@ impl Allocator for RustAllocator {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr {
unsafe fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr {
let new_size = round_size(new_size);
let ptr = unsafe { ptr.sub(HEADER_SIZE) };
let alloc_size = {
Expand Down Expand Up @@ -116,18 +116,18 @@ mod test {
struct TestAllocator;

impl Allocator for TestAllocator {
fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr {
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);
res
}

fn dealloc(&mut self, ptr: crate::allocator::RawMemPtr) {
unsafe fn dealloc(&mut self, ptr: crate::allocator::RawMemPtr) {
ALLOC_SIZE.fetch_sub(RustAllocator::usable_size(ptr), Ordering::AcqRel);
RustAllocator.dealloc(ptr);
}

fn realloc(
unsafe fn realloc(
&mut self,
ptr: crate::allocator::RawMemPtr,
new_size: usize,
Expand Down

0 comments on commit 0ac0dc0

Please sign in to comment.