#![no_std]
use core::cmp;
use core::fmt;
use core::mem;
use core::usize;
use core::ptr::{self, NonNull};
#[derive(Debug)]
pub struct Excess(pub *mut u8, pub usize);
fn size_align<T>() -> (usize, usize) {
(mem::size_of::<T>(), mem::align_of::<T>())
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Layout {
size: usize,
align: usize,
}
impl Layout {
#[inline]
pub fn from_size_align(size: usize, align: usize) -> Option<Layout> {
if !align.is_power_of_two() {
return None;
}
if align > (1 << 31) {
return None;
}
if size > usize::MAX - (align - 1) {
return None;
}
unsafe {
Some(Layout::from_size_align_unchecked(size, align))
}
}
#[inline]
pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Layout {
Layout { size: size, align: align }
}
#[inline]
pub fn size(&self) -> usize { self.size }
#[inline]
pub fn align(&self) -> usize { self.align }
pub fn new<T>() -> Self {
let (size, align) = size_align::<T>();
Layout::from_size_align(size, align).unwrap()
}
pub fn for_value<T: ?Sized>(t: &T) -> Self {
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
Layout::from_size_align(size, align).unwrap()
}
#[inline]
pub fn align_to(&self, align: usize) -> Self {
Layout::from_size_align(self.size, cmp::max(self.align, align)).unwrap()
}
#[inline]
pub fn padding_needed_for(&self, align: usize) -> usize {
let len = self.size();
let len_rounded_up = len.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
return len_rounded_up.wrapping_sub(len);
}
#[inline]
pub fn repeat(&self, n: usize) -> Option<(Self, usize)> {
let padded_size = self.size.checked_add(self.padding_needed_for(self.align))?;
let alloc_size = padded_size.checked_mul(n)?;
Some((Layout::from_size_align(alloc_size, self.align).unwrap(), padded_size))
}
pub fn extend(&self, next: Self) -> Option<(Self, usize)> {
let new_align = cmp::max(self.align, next.align);
let realigned = Layout::from_size_align(self.size, new_align)?;
let pad = realigned.padding_needed_for(next.align);
let offset = self.size.checked_add(pad)?;
let new_size = offset.checked_add(next.size)?;
let layout = Layout::from_size_align(new_size, new_align)?;
Some((layout, offset))
}
pub fn repeat_packed(&self, n: usize) -> Option<Self> {
let size = self.size().checked_mul(n)?;
Layout::from_size_align(size, self.align)
}
pub fn extend_packed(&self, next: Self) -> Option<(Self, usize)> {
let new_size = self.size().checked_add(next.size())?;
let layout = Layout::from_size_align(new_size, self.align)?;
Some((layout, self.size()))
}
pub fn array<T>(n: usize) -> Option<Self> {
Layout::new::<T>()
.repeat(n)
.map(|(k, offs)| {
debug_assert!(offs == mem::size_of::<T>());
k
})
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum AllocErr {
Exhausted { request: Layout },
Unsupported { details: &'static str },
}
impl AllocErr {
#[inline]
pub fn invalid_input(details: &'static str) -> Self {
AllocErr::Unsupported { details: details }
}
#[inline]
pub fn is_memory_exhausted(&self) -> bool {
if let AllocErr::Exhausted { .. } = *self { true } else { false }
}
#[inline]
pub fn is_request_unsupported(&self) -> bool {
if let AllocErr::Unsupported { .. } = *self { true } else { false }
}
#[inline]
pub fn description(&self) -> &str {
match *self {
AllocErr::Exhausted { .. } => "allocator memory exhausted",
AllocErr::Unsupported { .. } => "unsupported allocator request",
}
}
}
impl fmt::Display for AllocErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CannotReallocInPlace;
impl CannotReallocInPlace {
pub fn description(&self) -> &str {
"cannot reallocate allocator's memory in place"
}
}
impl fmt::Display for CannotReallocInPlace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum CollectionAllocErr {
CapacityOverflow,
AllocErr(AllocErr),
}
impl From<AllocErr> for CollectionAllocErr {
fn from(err: AllocErr) -> Self {
CollectionAllocErr::AllocErr(err)
}
}
pub unsafe trait Alloc {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout);
fn oom(&mut self, _: AllocErr) -> ! {
panic!("oom");
}
#[inline]
fn usable_size(&self, layout: &Layout) -> (usize, usize) {
(layout.size(), layout.size())
}
unsafe fn realloc(&mut self,
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<*mut u8, AllocErr> {
let new_size = new_layout.size();
let old_size = layout.size();
let aligns_match = layout.align == new_layout.align;
if new_size >= old_size && aligns_match {
if let Ok(()) = self.grow_in_place(ptr, layout.clone(), new_layout.clone()) {
return Ok(ptr);
}
} else if new_size < old_size && aligns_match {
if let Ok(()) = self.shrink_in_place(ptr, layout.clone(), new_layout.clone()) {
return Ok(ptr);
}
}
let result = self.alloc(new_layout);
if let Ok(new_ptr) = result {
ptr::copy_nonoverlapping(ptr as *const u8, new_ptr, cmp::min(old_size, new_size));
self.dealloc(ptr, layout);
}
result
}
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
let size = layout.size();
let p = self.alloc(layout);
if let Ok(p) = p {
ptr::write_bytes(p, 0, size);
}
p
}
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
let usable_size = self.usable_size(&layout);
self.alloc(layout).map(|p| Excess(p, usable_size.1))
}
unsafe fn realloc_excess(&mut self,
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<Excess, AllocErr> {
let usable_size = self.usable_size(&new_layout);
self.realloc(ptr, layout, new_layout)
.map(|p| Excess(p, usable_size.1))
}
unsafe fn grow_in_place(&mut self,
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
let _ = ptr; debug_assert!(new_layout.size >= layout.size);
debug_assert!(new_layout.align == layout.align);
let (_l, u) = self.usable_size(&layout);
if new_layout.size <= u {
return Ok(());
} else {
return Err(CannotReallocInPlace);
}
}
unsafe fn shrink_in_place(&mut self,
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
let _ = ptr; debug_assert!(new_layout.size <= layout.size);
debug_assert!(new_layout.align == layout.align);
let (l, _u) = self.usable_size(&layout);
if l <= new_layout.size {
return Ok(());
} else {
return Err(CannotReallocInPlace);
}
}
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
where Self: Sized
{
let k = Layout::new::<T>();
if k.size() > 0 {
unsafe { self.alloc(k).map(|p| NonNull::new_unchecked(p as *mut T)) }
} else {
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
}
}
unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>)
where Self: Sized
{
let raw_ptr = ptr.as_ptr() as *mut u8;
let k = Layout::new::<T>();
if k.size() > 0 {
self.dealloc(raw_ptr, k);
}
}
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
where Self: Sized
{
match Layout::array::<T>(n) {
Some(ref layout) if layout.size() > 0 => {
unsafe {
self.alloc(layout.clone())
.map(|p| {
NonNull::new_unchecked(p as *mut T)
})
}
}
_ => Err(AllocErr::invalid_input("invalid layout for alloc_array")),
}
}
unsafe fn realloc_array<T>(&mut self,
ptr: NonNull<T>,
n_old: usize,
n_new: usize) -> Result<NonNull<T>, AllocErr>
where Self: Sized
{
match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), ptr.as_ptr()) {
(Some(ref k_old), Some(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
self.realloc(ptr as *mut u8, k_old.clone(), k_new.clone())
.map(|p| NonNull::new_unchecked(p as *mut T))
}
_ => {
Err(AllocErr::invalid_input("invalid layout for realloc_array"))
}
}
}
unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), AllocErr>
where Self: Sized
{
let raw_ptr = ptr.as_ptr() as *mut u8;
match Layout::array::<T>(n) {
Some(ref k) if k.size() > 0 => {
Ok(self.dealloc(raw_ptr, k.clone()))
}
_ => {
Err(AllocErr::invalid_input("invalid layout for dealloc_array"))
}
}
}
}