Skip to content

Commit

Permalink
Merge pull request arceos-org#115 from pymongo/main
Browse files Browse the repository at this point in the history
alloc: update memory allocation pointer type from NonZeroUsize to NonNull<u8> in ByteAllocator trait
  • Loading branch information
equation314 authored Aug 23, 2023
2 parents 93c1e47 + 6357d60 commit e526753
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 54 deletions.
8 changes: 2 additions & 6 deletions api/arceos_api/src/imp/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ cfg_alloc! {
use core::ptr::NonNull;

pub fn ax_alloc(layout: Layout) -> Option<NonNull<u8>> {
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<u8>, layout: Layout) {
axalloc::global_allocator().dealloc(ptr.addr(), layout)
axalloc::global_allocator().dealloc(ptr, layout)
}
}
1 change: 0 additions & 1 deletion api/arceos_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![no_std]
#![feature(ip_in_core)]
#![feature(strict_provenance)]
#![feature(doc_auto_cfg)]
#![feature(doc_cfg)]
#![allow(unused_imports)]
Expand Down
16 changes: 5 additions & 11 deletions crates/allocator/src/buddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -36,18 +36,12 @@ impl BaseAllocator for BuddyByteAllocator {
}

impl ByteAllocator for BuddyByteAllocator {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonZeroUsize> {
self.inner
.alloc(layout)
.map(|ptr| ptr.addr())
.map_err(|_| AllocError::NoMemory)
fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>> {
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<u8>, layout: Layout) {
self.inner.dealloc(pos, layout)
}

fn total_bytes(&self) -> usize {
Expand Down
12 changes: 5 additions & 7 deletions crates/allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]

Expand All @@ -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)]
Expand Down Expand Up @@ -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<NonZeroUsize>;
fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>>;

/// Deallocate memory at the given position, size, and alignment.
fn dealloc(&mut self, pos: NonZeroUsize, layout: Layout);
fn dealloc(&mut self, pos: NonNull<u8>, layout: Layout);

/// Returns total memory size in bytes.
fn total_bytes(&self) -> usize;
Expand Down Expand Up @@ -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<u8>, layout: Layout) {
self.0.borrow_mut().dealloc(ptr.addr(), layout)
self.0.borrow_mut().dealloc(ptr, layout)
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/allocator/src/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -43,15 +43,15 @@ impl BaseAllocator for SlabByteAllocator {
}

impl ByteAllocator for SlabByteAllocator {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonZeroUsize> {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>> {
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<u8>, layout: Layout) {
unsafe { self.inner_mut().deallocate(pos.as_ptr() as usize, layout) }
}

fn total_bytes(&self) -> usize {
Expand Down
12 changes: 4 additions & 8 deletions crates/allocator/src/tlsf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,17 +52,14 @@ impl BaseAllocator for TlsfByteAllocator {
}

impl ByteAllocator for TlsfByteAllocator {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonZeroUsize> {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>> {
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<u8>, layout: Layout) {
unsafe { self.inner.deallocate(pos, layout.align()) }
self.used_bytes -= layout.size();
}

Expand Down
1 change: 0 additions & 1 deletion crates/allocator/tests/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![feature(btreemap_alloc)]
#![feature(allocator_api)]
#![feature(strict_provenance)]

use std::alloc::{Allocator, Layout};
use std::collections::BTreeMap;
Expand Down
14 changes: 5 additions & 9 deletions modules/axalloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<NonZeroUsize> {
pub fn alloc(&self, layout: Layout) -> AllocResult<NonNull<u8>> {
// simple two-level allocator: if no heap memory, allocate from the page allocator.
let mut balloc = self.balloc.lock();
loop {
Expand Down Expand Up @@ -132,7 +132,7 @@ impl GlobalAllocator {
/// undefined.
///
/// [`alloc`]: GlobalAllocator::alloc
pub fn dealloc(&self, pos: NonZeroUsize, layout: Layout) {
pub fn dealloc(&self, pos: NonNull<u8>, layout: Layout) {
self.balloc.lock().dealloc(pos, layout)
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
9 changes: 4 additions & 5 deletions modules/axdriver/src/ixgbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ unsafe impl IxgbeHal for IxgbeHalImpl {
fn dma_alloc(size: usize) -> (IxgbePhysAddr, NonNull<u8>) {
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<u8>, size: usize) -> i32 {
let layout = Layout::from_size_align(size, 8).unwrap();
global_allocator().dealloc(vaddr.addr(), layout);
global_allocator().dealloc(vaddr, layout);
0
}

Expand Down
1 change: 0 additions & 1 deletion modules/axdriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#![no_std]
#![feature(doc_auto_cfg)]
#![feature(associated_type_defaults)]
#![feature(strict_provenance)]

#[macro_use]
extern crate log;
Expand Down

0 comments on commit e526753

Please sign in to comment.