address_map.rs 938 Bytes
Newer Older
1
use std::mem;
qinsoon's avatar
qinsoon committed
2 3 4
use utils::LOG_POINTER_SIZE;
use utils::Address;
use heap::gc::malloc_zero;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#[derive(Clone)]
pub struct AddressMap<T: Copy> {
    start : Address,
    end   : Address,
    
    pub ptr   : *mut T,
    len   : usize
}

impl <T> AddressMap<T> where T: Copy{
    pub fn new(start: Address, end: Address) -> AddressMap<T> {
        let len = end.diff(start) >> LOG_POINTER_SIZE;
        let ptr = unsafe{malloc_zero(mem::size_of::<T>() * len)} as *mut T;
        
        AddressMap{start: start, end: end, ptr: ptr, len: len}
    }
    
    #[inline(always)]
    pub fn set(&self, addr: Address, value: T) {
        let index = (addr.diff(self.start) >> LOG_POINTER_SIZE) as isize;
        unsafe{*self.ptr.offset(index) = value};
    }

    #[inline(always)]
    pub fn get(&self, addr: Address) -> T {
        let index = (addr.diff(self.start) >> LOG_POINTER_SIZE) as isize;
        unsafe {*self.ptr.offset(index)}
    }
}