Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/alloc/mem_descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use core::{mem, cmp};
use core::num::Int;

#[deriving(Copy, Clone, PartialEq, Eq, Hash)]
pub struct MemDescriptor {
pub size: uint,
pub alignment: uint
}

impl MemDescriptor {
pub fn from_ty<T>() -> MemDescriptor {
MemDescriptor {
size: mem::size_of::<T>(),
alignment: mem::min_align_of::<T>()
}
}

pub fn empty() -> MemDescriptor {
MemDescriptor {
size: 0,
alignment: 1
}
}

pub fn then(self, other: MemDescriptor) -> MemDescriptor {
MemDescriptor {
size: self.offset_to(other) + other.size,
alignment: cmp::max(self.alignment, other.alignment)
}
}

pub fn offset_to(self, other: MemDescriptor) -> uint {
round_up_to_next(self.size, other.alignment)
}

pub fn tail_pad(self) -> MemDescriptor {
MemDescriptor {
size: round_up_to_next(self.size, self.alignment),
alignment: self.alignment
}
}

pub fn array(self, length: uint) -> MemDescriptor {
let this = self.tail_pad();
MemDescriptor {
size: this.size.checked_mul(length).expect("capacity overflow"),
alignment: this.alignment
}
}
}

fn round_up_to_next(value: uint, power_of_two: uint) -> uint {
value + power_of_two
}
39 changes: 39 additions & 0 deletions src/alloc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub use self::mem_descriptor::MemDescriptor;

use rust_alloc::heap;

pub mod vec;
pub mod mem_descriptor;

pub trait Allocator {
unsafe fn allocate(&mut self, descriptor: MemDescriptor) -> *mut u8;
unsafe fn deallocate(&mut self, ptr: *mut u8, descriptor: MemDescriptor);
unsafe fn shrink(&mut self, ptr: *mut u8, old_descriptor: MemDescriptor, new_descriptor: MemDescriptor) -> bool;
unsafe fn grow(&mut self, ptr: *mut u8, old_descriptor: MemDescriptor, new_descriptor: MemDescriptor) -> bool;
fn usable_size(&self, descriptor: MemDescriptor) -> uint;
}

#[deriving(Copy, Clone, Default)]
pub struct RustAllocator;

impl Allocator for RustAllocator {
unsafe fn allocate(&mut self, descriptor: MemDescriptor) -> *mut u8 {
heap::allocate(descriptor.size, descriptor.alignment)
}

unsafe fn deallocate(&mut self, ptr: *mut u8, descriptor: MemDescriptor) {
heap::deallocate(ptr, descriptor.size, descriptor.alignment)
}

unsafe fn shrink(&mut self, ptr: *mut u8, old_descriptor: MemDescriptor, new_descriptor: MemDescriptor) -> bool {
heap::reallocate_inplace(ptr, old_descriptor.size, new_descriptor.size, old_descriptor.alignment) == self.usable_size(new_descriptor)
}

unsafe fn grow(&mut self, ptr: *mut u8, old_descriptor: MemDescriptor, new_descriptor: MemDescriptor) -> bool {
heap::reallocate_inplace(ptr, old_descriptor.size, new_descriptor.size, old_descriptor.alignment) == self.usable_size(new_descriptor)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just check if new size >= desired size here.

}

fn usable_size(&self, descriptor: MemDescriptor) -> uint {
heap::usable_size(descriptor.size, descriptor.alignment)
}
}
Loading