diff --git a/api/arceos_api/src/imp/mem.rs b/api/arceos_api/src/imp/mem.rs index bd61f0daaa..3dd1ded12e 100644 --- a/api/arceos_api/src/imp/mem.rs +++ b/api/arceos_api/src/imp/mem.rs @@ -3,14 +3,10 @@ cfg_alloc! { use core::ptr::NonNull; pub fn ax_alloc(layout: Layout) -> Option> { - if let Ok(vaddr) = axalloc::global_allocator().alloc(layout) { - Some(unsafe { NonNull::new_unchecked(vaddr.get() as _) }) - } else { - None - } + axalloc::global_allocator().alloc(layout).ok() } pub fn ax_dealloc(ptr: NonNull, layout: Layout) { - axalloc::global_allocator().dealloc(ptr.addr(), layout) + axalloc::global_allocator().dealloc(ptr, layout) } } diff --git a/api/arceos_api/src/lib.rs b/api/arceos_api/src/lib.rs index 5e76fbd870..69fec6eaf4 100644 --- a/api/arceos_api/src/lib.rs +++ b/api/arceos_api/src/lib.rs @@ -4,7 +4,6 @@ #![no_std] #![feature(ip_in_core)] -#![feature(strict_provenance)] #![feature(doc_auto_cfg)] #![feature(doc_cfg)] #![allow(unused_imports)] diff --git a/crates/allocator/src/buddy.rs b/crates/allocator/src/buddy.rs index 7b33b1d99d..872f540a46 100644 --- a/crates/allocator/src/buddy.rs +++ b/crates/allocator/src/buddy.rs @@ -4,7 +4,7 @@ use buddy_system_allocator::Heap; use core::alloc::Layout; -use core::num::NonZeroUsize; +use core::ptr::NonNull; use crate::{AllocError, AllocResult, BaseAllocator, ByteAllocator}; @@ -36,18 +36,12 @@ impl BaseAllocator for BuddyByteAllocator { } impl ByteAllocator for BuddyByteAllocator { - fn alloc(&mut self, layout: Layout) -> AllocResult { - self.inner - .alloc(layout) - .map(|ptr| ptr.addr()) - .map_err(|_| AllocError::NoMemory) + fn alloc(&mut self, layout: Layout) -> AllocResult> { + self.inner.alloc(layout).map_err(|_| AllocError::NoMemory) } - fn dealloc(&mut self, pos: NonZeroUsize, layout: Layout) { - self.inner.dealloc( - unsafe { core::ptr::NonNull::new_unchecked(pos.get() as _) }, - layout, - ) + fn dealloc(&mut self, pos: NonNull, layout: Layout) { + self.inner.dealloc(pos, layout) } fn total_bytes(&self) -> usize { diff --git a/crates/allocator/src/lib.rs b/crates/allocator/src/lib.rs index f8a62e6b02..5ced1b9921 100644 --- a/crates/allocator/src/lib.rs +++ b/crates/allocator/src/lib.rs @@ -9,7 +9,6 @@ //! - [`IdAllocator`]: Used to allocate unique IDs. #![no_std] -#![feature(strict_provenance)] #![feature(result_option_inspect)] #![cfg_attr(feature = "allocator_api", feature(allocator_api))] @@ -34,7 +33,7 @@ mod tlsf; pub use tlsf::TlsfByteAllocator; use core::alloc::Layout; -use core::num::NonZeroUsize; +use core::ptr::NonNull; /// The error type used for allocation. #[derive(Debug)] @@ -64,10 +63,10 @@ pub trait BaseAllocator { /// Byte-granularity allocator. pub trait ByteAllocator: BaseAllocator { /// Allocate memory with the given size (in bytes) and alignment. - fn alloc(&mut self, layout: Layout) -> AllocResult; + fn alloc(&mut self, layout: Layout) -> AllocResult>; /// Deallocate memory at the given position, size, and alignment. - fn dealloc(&mut self, pos: NonZeroUsize, layout: Layout); + fn dealloc(&mut self, pos: NonNull, layout: Layout); /// Returns total memory size in bytes. fn total_bytes(&self) -> usize; @@ -161,14 +160,13 @@ mod allocator_api { 0 => Ok(NonNull::slice_from_raw_parts(NonNull::dangling(), 0)), size => { let raw_addr = self.0.borrow_mut().alloc(layout).map_err(|_| AllocError)?; - let ptr = unsafe { NonNull::new_unchecked(raw_addr.get() as _) }; - Ok(NonNull::slice_from_raw_parts(ptr, size)) + Ok(NonNull::slice_from_raw_parts(raw_addr, size)) } } } unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - self.0.borrow_mut().dealloc(ptr.addr(), layout) + self.0.borrow_mut().dealloc(ptr, layout) } } diff --git a/crates/allocator/src/slab.rs b/crates/allocator/src/slab.rs index ba643c9542..17cb164b32 100644 --- a/crates/allocator/src/slab.rs +++ b/crates/allocator/src/slab.rs @@ -4,7 +4,7 @@ use super::{AllocError, AllocResult, BaseAllocator, ByteAllocator}; use core::alloc::Layout; -use core::num::NonZeroUsize; +use core::ptr::NonNull; use slab_allocator::Heap; /// A byte-granularity memory allocator based on the [slab allocator]. @@ -43,15 +43,15 @@ impl BaseAllocator for SlabByteAllocator { } impl ByteAllocator for SlabByteAllocator { - fn alloc(&mut self, layout: Layout) -> AllocResult { + fn alloc(&mut self, layout: Layout) -> AllocResult> { self.inner_mut() .allocate(layout) - .map(|addr| NonZeroUsize::new(addr).unwrap()) + .map(|addr| unsafe { NonNull::new_unchecked(addr as *mut u8) }) .map_err(|_| AllocError::NoMemory) } - fn dealloc(&mut self, pos: NonZeroUsize, layout: Layout) { - unsafe { self.inner_mut().deallocate(pos.get(), layout) } + fn dealloc(&mut self, pos: NonNull, layout: Layout) { + unsafe { self.inner_mut().deallocate(pos.as_ptr() as usize, layout) } } fn total_bytes(&self) -> usize { diff --git a/crates/allocator/src/tlsf.rs b/crates/allocator/src/tlsf.rs index 72448a113d..3d2cd99d92 100644 --- a/crates/allocator/src/tlsf.rs +++ b/crates/allocator/src/tlsf.rs @@ -4,7 +4,6 @@ use super::{AllocError, AllocResult, BaseAllocator, ByteAllocator}; use core::alloc::Layout; -use core::num::NonZeroUsize; use core::ptr::NonNull; use rlsf::Tlsf; @@ -53,17 +52,14 @@ impl BaseAllocator for TlsfByteAllocator { } impl ByteAllocator for TlsfByteAllocator { - fn alloc(&mut self, layout: Layout) -> AllocResult { + fn alloc(&mut self, layout: Layout) -> AllocResult> { let ptr = self.inner.allocate(layout).ok_or(AllocError::NoMemory)?; self.used_bytes += layout.size(); - Ok(ptr.addr()) + Ok(ptr) } - fn dealloc(&mut self, pos: NonZeroUsize, layout: Layout) { - unsafe { - self.inner - .deallocate(NonNull::new_unchecked(pos.get() as _), layout.align()) - } + fn dealloc(&mut self, pos: NonNull, layout: Layout) { + unsafe { self.inner.deallocate(pos, layout.align()) } self.used_bytes -= layout.size(); } diff --git a/crates/allocator/tests/allocator.rs b/crates/allocator/tests/allocator.rs index d1f8ce66b4..58962b50d2 100644 --- a/crates/allocator/tests/allocator.rs +++ b/crates/allocator/tests/allocator.rs @@ -1,6 +1,5 @@ #![feature(btreemap_alloc)] #![feature(allocator_api)] -#![feature(strict_provenance)] use std::alloc::{Allocator, Layout}; use std::collections::BTreeMap; diff --git a/modules/axalloc/src/lib.rs b/modules/axalloc/src/lib.rs index 64ea1de243..096ce95ae7 100644 --- a/modules/axalloc/src/lib.rs +++ b/modules/axalloc/src/lib.rs @@ -15,7 +15,7 @@ mod page; use allocator::{AllocResult, BaseAllocator, BitmapPageAllocator, ByteAllocator, PageAllocator}; use core::alloc::{GlobalAlloc, Layout}; -use core::num::NonZeroUsize; +use core::ptr::NonNull; use spinlock::SpinNoIrq; const PAGE_SIZE: usize = 0x1000; @@ -102,7 +102,7 @@ impl GlobalAllocator { /// /// `align_pow2` must be a power of 2, and the returned region bound will be /// aligned to it. - pub fn alloc(&self, layout: Layout) -> AllocResult { + pub fn alloc(&self, layout: Layout) -> AllocResult> { // simple two-level allocator: if no heap memory, allocate from the page allocator. let mut balloc = self.balloc.lock(); loop { @@ -132,7 +132,7 @@ impl GlobalAllocator { /// undefined. /// /// [`alloc`]: GlobalAllocator::alloc - pub fn dealloc(&self, pos: NonZeroUsize, layout: Layout) { + pub fn dealloc(&self, pos: NonNull, layout: Layout) { self.balloc.lock().dealloc(pos, layout) } @@ -181,18 +181,14 @@ impl GlobalAllocator { unsafe impl GlobalAlloc for GlobalAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { if let Ok(ptr) = GlobalAllocator::alloc(self, layout) { - ptr.get() as _ + ptr.as_ptr() } else { alloc::alloc::handle_alloc_error(layout) } } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - GlobalAllocator::dealloc( - self, - NonZeroUsize::new(ptr as _).expect("dealloc null ptr"), - layout, - ) + GlobalAllocator::dealloc(self, NonNull::new(ptr).expect("dealloc null ptr"), layout) } } diff --git a/modules/axdriver/src/ixgbe.rs b/modules/axdriver/src/ixgbe.rs index acd3441280..34d9267190 100644 --- a/modules/axdriver/src/ixgbe.rs +++ b/modules/axdriver/src/ixgbe.rs @@ -9,18 +9,17 @@ unsafe impl IxgbeHal for IxgbeHalImpl { fn dma_alloc(size: usize) -> (IxgbePhysAddr, NonNull) { let layout = Layout::from_size_align(size, 8).unwrap(); let vaddr = if let Ok(vaddr) = global_allocator().alloc(layout) { - vaddr.get() + vaddr } else { return (0, NonNull::dangling()); }; - let paddr = virt_to_phys(vaddr.into()); - let ptr = NonNull::new(vaddr as _).unwrap(); - (paddr.as_usize(), ptr) + let paddr = virt_to_phys((vaddr.as_ptr() as usize).into()); + (paddr.as_usize(), vaddr) } unsafe fn dma_dealloc(_paddr: IxgbePhysAddr, vaddr: NonNull, size: usize) -> i32 { let layout = Layout::from_size_align(size, 8).unwrap(); - global_allocator().dealloc(vaddr.addr(), layout); + global_allocator().dealloc(vaddr, layout); 0 } diff --git a/modules/axdriver/src/lib.rs b/modules/axdriver/src/lib.rs index 43db35283b..05b5d98524 100644 --- a/modules/axdriver/src/lib.rs +++ b/modules/axdriver/src/lib.rs @@ -57,7 +57,6 @@ #![no_std] #![feature(doc_auto_cfg)] #![feature(associated_type_defaults)] -#![feature(strict_provenance)] #[macro_use] extern crate log;