To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

address_map.rs 1.75 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
use std::mem;
qinsoon's avatar
qinsoon committed
16
use utils::POINTER_SIZE;
qinsoon's avatar
qinsoon committed
17
18
19
use utils::LOG_POINTER_SIZE;
use utils::Address;
use heap::gc::malloc_zero;
20
21
22
23
24
25
26
27
28
29
30
31

#[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> {
32
        let len = (end - start) >> LOG_POINTER_SIZE;
33
34
35
36
37
        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
38
39
40
41
42
    pub fn init_all (&self, init: T) {
        let mut cursor = self.start;
        
        while cursor < self.end {
            self.set(cursor, init);
43
            cursor = cursor + POINTER_SIZE;
qinsoon's avatar
qinsoon committed
44
45
46
        }
    }
    
47
48
    #[inline(always)]
    pub fn set(&self, addr: Address, value: T) {
49
        let index = ((addr - self.start) >> LOG_POINTER_SIZE) as isize;
50
51
52
53
54
        unsafe{*self.ptr.offset(index) = value};
    }

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