address_map.rs 1.16 KB
Newer Older
1
use std::mem;
qinsoon's avatar
qinsoon committed
2
use utils::POINTER_SIZE;
qinsoon's avatar
qinsoon committed
3 4 5
use utils::LOG_POINTER_SIZE;
use utils::Address;
use heap::gc::malloc_zero;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

#[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}
    }
    
qinsoon's avatar
qinsoon committed
24 25 26 27 28 29 30 31 32
    pub fn init_all (&self, init: T) {
        let mut cursor = self.start;
        
        while cursor < self.end {
            self.set(cursor, init);
            cursor = cursor.plus(POINTER_SIZE);	
        }
    }
    
33 34 35 36 37 38 39 40 41 42 43
    #[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)}
    }
44
}