#![feature(unique)]
#![feature(const_fn)]
#![no_std]
#[cfg(test)]
#[macro_use]
extern crate std;
use hole::HoleList;
mod hole;
#[cfg(test)]
mod test;
pub struct Heap {
bottom: usize,
size: usize,
holes: HoleList,
}
impl Heap {
pub const fn empty() -> Heap {
Heap {
bottom: 0,
size: 0,
holes: HoleList::empty(),
}
}
pub unsafe fn new(heap_bottom: usize, heap_size: usize) -> Heap {
Heap {
bottom: heap_bottom,
size: heap_size,
holes: HoleList::new(heap_bottom, heap_size),
}
}
pub fn allocate_first_fit(&mut self, mut size: usize, align: usize) -> Option<*mut u8> {
if size < HoleList::min_size() {
size = HoleList::min_size();
}
self.holes.allocate_first_fit(size, align)
}
pub unsafe fn deallocate(&mut self, ptr: *mut u8, mut size: usize, _align: usize) {
if size < HoleList::min_size() {
size = HoleList::min_size();
}
self.holes.deallocate(ptr, size);
}
pub fn bottom(&self) -> usize {
self.bottom
}
pub fn size(&self) -> usize {
self.size
}
}
pub fn align_down(addr: usize, align: usize) -> usize {
if align.is_power_of_two() {
addr & !(align - 1)
} else if align == 0 {
addr
} else {
panic!("`align` must be a power of 2");
}
}
pub fn align_up(addr: usize, align: usize) -> usize {
align_down(addr + align - 1, align)
}