Skip to content

Commit

Permalink
Update for experimental allocator_api
Browse files Browse the repository at this point in the history
- Incorporates changes for:
  - rust-lang/rust#76993
  - rust-lang/rust#77315
  • Loading branch information
Jay Oster committed Oct 21, 2020
1 parent 0173a95 commit 60b7791
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 95 deletions.
4 changes: 2 additions & 2 deletions src/adaptors/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
macro_rules! alloc_ref {
() => {
#[inline(always)]
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let size = layout.size();
let ptr = unsafe { self.alloc_alloc_zeroed(layout) }?;
Ok(NonNull::slice_from_raw_parts(ptr, size))
}

#[inline(always)]
unsafe fn dealloc(&mut self, ptr: MemoryAddress, layout: Layout) {
unsafe fn dealloc(&self, ptr: MemoryAddress, layout: Layout) {
self.alloc_dealloc(ptr, layout)
}
};
Expand Down
14 changes: 7 additions & 7 deletions src/adaptors/alloc_to_allocator_adaptor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::extensions::prelude::*;
use crate::allocators::allocator::Allocator;
use crate::memory_address::MemoryAddress;
use std::alloc::{AllocErr, GlobalAlloc, Layout};
use std::alloc::{AllocError, GlobalAlloc, Layout};
use std::cell::UnsafeCell;
use std::fmt;
use std::fmt::Debug;
Expand Down Expand Up @@ -33,12 +33,12 @@ impl<A: GlobalAlloc> Allocator for AllocToAllocatorAdaptor<A> {
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
NonNull::new(unsafe {
self.mutable_reference()
.alloc(Self::layout(non_zero_size, non_zero_power_of_two_alignment))
})
.ok_or(AllocErr)
.ok_or(AllocError)
}

#[inline(always)]
Expand All @@ -63,15 +63,15 @@ impl<A: GlobalAlloc> Allocator for AllocToAllocatorAdaptor<A> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
NonNull::new(unsafe {
self.mutable_reference().realloc(
current_memory.as_ptr(),
Self::layout(non_zero_current_size, non_zero_power_of_two_alignment),
non_zero_new_size.get(),
)
})
.ok_or(AllocErr)
.ok_or(AllocError)
}

#[inline(always)]
Expand All @@ -81,15 +81,15 @@ impl<A: GlobalAlloc> Allocator for AllocToAllocatorAdaptor<A> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
NonNull::new(unsafe {
self.mutable_reference().realloc(
current_memory.as_ptr(),
Self::layout(non_zero_current_size, non_zero_power_of_two_alignment),
non_zero_new_size.get(),
)
})
.ok_or(AllocErr)
.ok_or(AllocError)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/adaptors/allocator_adaptor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::allocators::allocator::Allocator;
use crate::memory_address::MemoryAddress;
use core::ptr::NonNull;
use std::alloc::{AllocErr, AllocRef, GlobalAlloc, Layout};
use std::alloc::{AllocError, AllocRef, GlobalAlloc, Layout};
use std::ops::Deref;

use std::num::NonZeroUsize;
Expand Down Expand Up @@ -44,14 +44,14 @@ unsafe impl<'a, A: 'a + Allocator> GlobalAlloc for AllocatorAdaptor<'a, A> {

unsafe impl<'a, A: 'a + Allocator> AllocRef for AllocatorAdaptor<'a, A> {
#[inline(always)]
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let size = layout.size();
let ptr = unsafe { self.alloc_alloc_zeroed(layout) }?;
Ok(NonNull::slice_from_raw_parts(ptr, size))
}

#[inline(always)]
unsafe fn dealloc(&mut self, ptr: MemoryAddress, layout: Layout) {
unsafe fn dealloc(&self, ptr: MemoryAddress, layout: Layout) {
self.alloc_dealloc(ptr, layout)
}
}
Expand All @@ -62,7 +62,7 @@ impl<'a, A: 'a + Allocator> Allocator for AllocatorAdaptor<'a, A> {
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
self.0
.allocate(non_zero_size, non_zero_power_of_two_alignment)
}
Expand All @@ -88,7 +88,7 @@ impl<'a, A: 'a + Allocator> Allocator for AllocatorAdaptor<'a, A> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
self.0.growing_reallocate(
non_zero_new_size,
non_zero_power_of_two_alignment,
Expand All @@ -104,7 +104,7 @@ impl<'a, A: 'a + Allocator> Allocator for AllocatorAdaptor<'a, A> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
self.0.shrinking_reallocate(
non_zero_new_size,
non_zero_power_of_two_alignment,
Expand Down
8 changes: 4 additions & 4 deletions src/adaptors/global_alloc_to_allocator_adaptor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::allocators::allocator::Allocator;
use crate::memory_address::MemoryAddress;
use std::alloc::{AllocErr, GlobalAlloc, Layout};
use std::alloc::{AllocError, GlobalAlloc, Layout};
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
Expand Down Expand Up @@ -32,7 +32,7 @@ impl<GA: GlobalAlloc> Allocator for GlobalAllocToAllocatorAdaptor<GA> {
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
unsafe {
transmute(
self.0
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<GA: GlobalAlloc> Allocator for GlobalAllocToAllocatorAdaptor<GA> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
unsafe {
transmute(self.0.realloc(
current_memory.as_ptr(),
Expand All @@ -80,7 +80,7 @@ impl<GA: GlobalAlloc> Allocator for GlobalAllocToAllocatorAdaptor<GA> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
unsafe {
transmute(self.0.realloc(
current_memory.as_ptr(),
Expand Down
20 changes: 10 additions & 10 deletions src/allocators/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::extensions::non_null_pointer::non_null_pointer;
use crate::extensions::prelude::*;
use crate::extensions::usize_ext::UsizeExt;
use crate::memory_address::MemoryAddress;
use std::alloc::{AllocErr, Layout};
use std::alloc::{AllocError, Layout};
use std::fmt::Debug;
use std::intrinsics::transmute;
use std::num::NonZeroUsize;
Expand All @@ -20,7 +20,7 @@ pub trait Allocator: Debug + Sized {
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr>;
) -> Result<MemoryAddress, AllocError>;

/// Deallocate (free) memory.
///
Expand All @@ -42,7 +42,7 @@ pub trait Allocator: Debug + Sized {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr>;
) -> Result<MemoryAddress, AllocError>;

/// Reallocate memory by shrinking it.
///
Expand All @@ -54,7 +54,7 @@ pub trait Allocator: Debug + Sized {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr>;
) -> Result<MemoryAddress, AllocError>;

/// Adapts to a `GlobalAlloc` and `Alloc`.
#[inline(always)]
Expand All @@ -70,7 +70,7 @@ pub trait Allocator: Debug + Sized {

#[doc(hidden)]
#[inline(always)]
fn allocate_zeroed(&self, layout: Layout) -> Result<MemoryAddress, AllocErr> {
fn allocate_zeroed(&self, layout: Layout) -> Result<MemoryAddress, AllocError> {
let maybe_zero_size = layout.size();

if unlikely!(maybe_zero_size == 0) {
Expand All @@ -81,7 +81,7 @@ pub trait Allocator: Debug + Sized {
let non_zero_align = layout.align().non_zero();
let result = self.allocate(non_zero_size, non_zero_align);

// NOTE: AllocErr does not implement `Copy`, but is zero-sized - seems like a Rust API oversight.
// NOTE: AllocError does not implement `Copy`, but is zero-sized - seems like a Rust API oversight.
// Hence the logic transmuting it to a pointer (for an efficient null check), then back to a result.
let pointer = unsafe { transmute::<_, *mut u8>(result) };

Expand All @@ -99,7 +99,7 @@ pub trait Allocator: Debug + Sized {
current_memory: MemoryAddress,
layout: Layout,
new_size: usize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
let current_size = layout.size();

if unlikely!(current_size == new_size) {
Expand Down Expand Up @@ -197,7 +197,7 @@ pub trait Allocator: Debug + Sized {

#[doc(hidden)]
#[inline(always)]
unsafe fn alloc_alloc(&self, layout: Layout) -> Result<MemoryAddress, AllocErr> {
unsafe fn alloc_alloc(&self, layout: Layout) -> Result<MemoryAddress, AllocError> {
if unlikely!(layout.size() == 0) {
return Ok(Self::ZERO_SIZED_ALLOCATION);
}
Expand All @@ -208,7 +208,7 @@ pub trait Allocator: Debug + Sized {

#[doc(hidden)]
#[inline(always)]
unsafe fn alloc_alloc_zeroed(&self, layout: Layout) -> Result<MemoryAddress, AllocErr> {
unsafe fn alloc_alloc_zeroed(&self, layout: Layout) -> Result<MemoryAddress, AllocError> {
self.allocate_zeroed(layout)
}

Expand All @@ -234,7 +234,7 @@ pub trait Allocator: Debug + Sized {
ptr: MemoryAddress,
layout: Layout,
new_size: usize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
self.reallocate(ptr, layout, new_size)
}
}
24 changes: 12 additions & 12 deletions src/allocators/bit_set/bit_set_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::extensions::prelude::*;
use crate::memory_address::MemoryAddress;
use crate::memory_sources::memory_source::MemorySource;
use either::*;
use std::alloc::AllocErr;
use std::alloc::AllocError;
use std::cell::Cell;
use std::num::NonZeroUsize;

Expand Down Expand Up @@ -45,7 +45,7 @@ impl<MS: MemorySource> Allocator for BitSetAllocator<MS> {
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
let number_of_bits_required = self.number_of_bits_required(non_zero_size);

let power_of_two_exponent = if self
Expand All @@ -60,7 +60,7 @@ impl<MS: MemorySource> Allocator for BitSetAllocator<MS> {
let alignment_exceeds_that_which_can_be_accommodated_in_one_bit_set_word =
power_of_two_exponent > BitSetWord::SIZE_IN_BITS;
if unlikely!(alignment_exceeds_that_which_can_be_accommodated_in_one_bit_set_word) {
return Err(AllocErr);
return Err(AllocError);
}

power_of_two_exponent
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<MS: MemorySource> Allocator for BitSetAllocator<MS> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
let current_number_of_bits_required = self.number_of_bits_required(non_zero_current_size);
let new_number_of_bits_required = self.number_of_bits_required(non_zero_new_size);

Expand Down Expand Up @@ -192,7 +192,7 @@ impl<MS: MemorySource> Allocator for BitSetAllocator<MS> {
non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
let current_number_of_bits_required = self.number_of_bits_required(non_zero_current_size);
let new_number_of_bits_required = self.number_of_bits_required(non_zero_new_size);

Expand Down Expand Up @@ -227,7 +227,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
pub fn new_by_amount_8(
memory_source: MS,
memory_source_size: NonZeroUsize,
) -> Result<Self, AllocErr> {
) -> Result<Self, AllocError> {
Self::new_by_amount(memory_source, 8usize.non_zero(), memory_source_size)
}

Expand All @@ -236,7 +236,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
pub fn new_by_amount_16(
memory_source: MS,
memory_source_size: NonZeroUsize,
) -> Result<Self, AllocErr> {
) -> Result<Self, AllocError> {
Self::new_by_amount(memory_source, 16usize.non_zero(), memory_source_size)
}

Expand All @@ -245,7 +245,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
pub fn new_by_amount_32(
memory_source: MS,
memory_source_size: NonZeroUsize,
) -> Result<Self, AllocErr> {
) -> Result<Self, AllocError> {
Self::new_by_amount(memory_source, 32usize.non_zero(), memory_source_size)
}

Expand All @@ -255,7 +255,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
memory_source: MS,
block_size: NonZeroUsize,
memory_source_size: NonZeroUsize,
) -> Result<Self, AllocErr> {
) -> Result<Self, AllocError> {
let number_of_blocks =
((memory_source_size.get() + (block_size.get() - 1)) / block_size.get()).non_zero();

Expand All @@ -268,7 +268,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
memory_source: MS,
block_size: NonZeroUsize,
number_of_blocks: NonZeroUsize,
) -> Result<Self, AllocErr> {
) -> Result<Self, AllocError> {
debug_assert!(
block_size.is_power_of_two(),
"block_size `{:?}` must be a power of 2",
Expand Down Expand Up @@ -342,7 +342,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
&self,
number_of_bits_required: NumberOfBits,
power_of_two_exponent: usize,
) -> Result<MemoryAddress, AllocErr> {
) -> Result<MemoryAddress, AllocError> {
debug_assert!(number_of_bits_required.is_not_zero());

macro_rules! scan
Expand Down Expand Up @@ -404,7 +404,7 @@ impl<MS: MemorySource> BitSetAllocator<MS> {
callback
);

Err(AllocErr)
Err(AllocError)
}

#[inline(always)]
Expand Down
Loading

0 comments on commit 60b7791

Please sign in to comment.