Skip to content

Commit

Permalink
Basic renames and fixing signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty122 committed Feb 10, 2021
1 parent f26c431 commit ccae9a3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions wee_alloc/src/imp_static_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::AllocErr;
use super::AllocError;
use const_init::ConstInit;
#[cfg(feature = "extra_assertions")]
use core::cell::Cell;
Expand All @@ -17,16 +17,16 @@ struct ScratchHeap([u8; SCRATCH_LEN_BYTES]);
static mut SCRATCH_HEAP: ScratchHeap = ScratchHeap([0; SCRATCH_LEN_BYTES]);
static mut OFFSET: Mutex<usize> = Mutex::new(0);

pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocError> {
let bytes: Bytes = pages.into();
let mut offset = OFFSET.lock();
let end = bytes.0.checked_add(*offset).ok_or(AllocErr)?;
let end = bytes.0.checked_add(*offset).ok_or(AllocError)?;
if end < SCRATCH_LEN_BYTES {
let ptr = SCRATCH_HEAP.0[*offset..end].as_mut_ptr() as *mut u8;
*offset = end;
NonNull::new(ptr).ok_or_else(|| AllocErr)
NonNull::new(ptr).ok_or_else(|| AllocError)
} else {
Err(AllocErr)
Err(AllocError)
}
}

Expand Down
8 changes: 4 additions & 4 deletions wee_alloc/src/imp_unix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::AllocErr;
use super::AllocError;
use const_init::ConstInit;
use core::cell::UnsafeCell;
use core::ptr;
use libc;
use memory_units::{Bytes, Pages};

pub(crate) fn alloc_pages(pages: Pages) -> Result<ptr::NonNull<u8>, AllocErr> {
pub(crate) fn alloc_pages(pages: Pages) -> Result<ptr::NonNull<u8>, AllocError> {
unsafe {
let bytes: Bytes = pages.into();
let addr = libc::mmap(
Expand All @@ -17,9 +17,9 @@ pub(crate) fn alloc_pages(pages: Pages) -> Result<ptr::NonNull<u8>, AllocErr> {
0,
);
if addr == libc::MAP_FAILED {
Err(AllocErr)
Err(AllocError)
} else {
ptr::NonNull::new(addr as *mut u8).ok_or(AllocErr)
ptr::NonNull::new(addr as *mut u8).ok_or(AllocError)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions wee_alloc/src/imp_wasm32.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use super::{assert_is_word_aligned, PAGE_SIZE, unchecked_unwrap};
use const_init::ConstInit;
use super::AllocErr;
use super::AllocError;
use core::arch::wasm32;
use core::cell::UnsafeCell;
use core::ptr::NonNull;
use memory_units::Pages;

pub(crate) unsafe fn alloc_pages(n: Pages) -> Result<NonNull<u8>, AllocErr> {
pub(crate) unsafe fn alloc_pages(n: Pages) -> Result<NonNull<u8>, AllocError> {
let ptr = wasm32::memory_grow(0, n.0);
if ptr != usize::max_value() {
let ptr = (ptr * PAGE_SIZE.0) as *mut u8;
assert_is_word_aligned(ptr as *mut u8);
Ok(unchecked_unwrap(NonNull::new(ptr)))
} else {
Err(AllocErr)
Err(AllocError)
}
}

Expand Down
6 changes: 3 additions & 3 deletions wee_alloc/src/imp_windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use const_init::ConstInit;
use super::AllocErr;
use super::AllocError;
use core::cell::UnsafeCell;
use core::ptr::NonNull;
use memory_units::{Bytes, Pages};
Expand All @@ -11,10 +11,10 @@ use winapi::um::synchapi::{
};
use winapi::um::winnt::{MEM_COMMIT, PAGE_READWRITE};

pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
pub(crate) fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocError> {
let bytes: Bytes = pages.into();
let ptr = unsafe { VirtualAlloc(NULL, bytes.0, MEM_COMMIT, PAGE_READWRITE) };
NonNull::new(ptr as *mut u8).ok_or(AllocErr)
NonNull::new(ptr as *mut u8).ok_or(AllocError)
}

// Align to the cache line size on an i7 to avoid false sharing.
Expand Down
28 changes: 14 additions & 14 deletions wee_alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ mod size_classes;

cfg_if! {
if #[cfg(feature = "nightly")] {
use core::alloc::{Alloc, AllocErr};
use core::alloc::{Allocator, AllocError};
} else {
pub(crate) struct AllocErr;
pub(crate) struct AllocError;
}
}

Expand Down Expand Up @@ -759,7 +759,7 @@ trait AllocPolicy<'a> {
&self,
size: Words,
align: Bytes,
) -> Result<*const FreeCell<'a>, AllocErr>;
) -> Result<*const FreeCell<'a>, AllocError>;

fn min_cell_size(&self, alloc_size: Words) -> Words;

Expand All @@ -785,7 +785,7 @@ impl<'a> AllocPolicy<'a> for LargeAllocPolicy {
&self,
size: Words,
align: Bytes,
) -> Result<*const FreeCell<'a>, AllocErr> {
) -> Result<*const FreeCell<'a>, AllocError> {
// To assure that an allocation will always succeed after refilling the
// free list with this new cell, make sure that we allocate enough to
// fulfill the requested alignment, and still have the minimum cell size
Expand Down Expand Up @@ -846,7 +846,7 @@ unsafe fn walk_free_list<'a, F, T>(
head: &Cell<*const FreeCell<'a>>,
policy: &dyn AllocPolicy<'a>,
mut f: F,
) -> Result<T, AllocErr>
) -> Result<T, AllocError>
where
F: FnMut(&Cell<*const FreeCell<'a>>, &FreeCell<'a>) -> Option<T>,
{
Expand All @@ -859,7 +859,7 @@ where
assert_local_cell_invariants(&(*current_free).header);

if current_free.is_null() {
return Err(AllocErr);
return Err(AllocError);
}

let current_free = Cell::new(current_free);
Expand Down Expand Up @@ -914,7 +914,7 @@ unsafe fn alloc_first_fit<'a>(
align: Bytes,
head: &Cell<*const FreeCell<'a>>,
policy: &dyn AllocPolicy<'a>,
) -> Result<NonNull<u8>, AllocErr> {
) -> Result<NonNull<u8>, AllocError> {
extra_assert!(size.0 > 0);

walk_free_list(head, policy, |previous, current| {
Expand All @@ -934,7 +934,7 @@ unsafe fn alloc_with_refill<'a, 'b>(
align: Bytes,
head: &'b Cell<*const FreeCell<'a>>,
policy: &dyn AllocPolicy<'a>,
) -> Result<NonNull<u8>, AllocErr> {
) -> Result<NonNull<u8>, AllocError> {
if let Ok(result) = alloc_first_fit(size, align, head, policy) {
return Ok(result);
}
Expand Down Expand Up @@ -1027,7 +1027,7 @@ impl<'a> WeeAlloc<'a> {
})
}

unsafe fn alloc_impl(&self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
unsafe fn alloc_impl(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
let size = Bytes(layout.size());
let align = if layout.align() == 0 {
Bytes(1)
Expand All @@ -1042,7 +1042,7 @@ impl<'a> WeeAlloc<'a> {
return Ok(NonNull::new_unchecked(align.0 as *mut u8));
}

let word_size: Words = checked_round_up_to(size).ok_or(AllocErr)?;
let word_size: Words = checked_round_up_to(size).ok_or(AllocError)?;

self.with_free_list_and_policy_for_size(word_size, align, |head, policy| {
assert_is_valid_free_list(head.get(), policy);
Expand Down Expand Up @@ -1142,15 +1142,15 @@ impl<'a> WeeAlloc<'a> {
}

#[cfg(feature = "nightly")]
unsafe impl<'a, 'b> Alloc for &'b WeeAlloc<'a>
unsafe impl<'a, 'b> Allocator for &'b WeeAlloc<'a>
where
'a: 'b,
{
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> {
self.alloc_impl(layout)
}

unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
self.dealloc_impl(ptr, layout)
}
}
Expand All @@ -1159,7 +1159,7 @@ unsafe impl GlobalAlloc for WeeAlloc<'static> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
match self.alloc_impl(layout) {
Ok(ptr) => ptr.as_ptr(),
Err(AllocErr) => ptr::null_mut(),
Err(AllocError) => ptr::null_mut(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions wee_alloc/src/size_classes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{alloc_with_refill, AllocErr, AllocPolicy, CellHeader, FreeCell, LargeAllocPolicy};
use super::{alloc_with_refill, AllocError, AllocPolicy, CellHeader, FreeCell, LargeAllocPolicy};
use const_init::ConstInit;
use core::cell::Cell;
use core::cmp;
Expand Down Expand Up @@ -40,7 +40,7 @@ where
&self,
size: Words,
align: Bytes,
) -> Result<*const FreeCell<'a>, AllocErr> {
) -> Result<*const FreeCell<'a>, AllocError> {
extra_assert!(align.0 > 0);
extra_assert!(align.0.is_power_of_two());
extra_assert!(align <= size_of::<usize>());
Expand Down

0 comments on commit ccae9a3

Please sign in to comment.