GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

address_map.rs 1.93 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
qinsoon's avatar
qinsoon committed
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
3
4
5
// 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
qinsoon's avatar
qinsoon committed
6
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
qinsoon's avatar
qinsoon committed
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
9
10
11
12
13
14
// 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
use utils::LOG_POINTER_SIZE;
use utils::Address;
19
20
use utils::mem::malloc_zero;
use utils::mem::memsec::free;
21
22
23

#[derive(Clone)]
pub struct AddressMap<T: Copy> {
qinsoon's avatar
qinsoon committed
24
25
26
27
    start: Address,
    end: Address,

    pub ptr: *mut T,
28
    len: usize
29
30
}

qinsoon's avatar
qinsoon committed
31
32
impl<T> AddressMap<T>
where
33
    T: Copy
qinsoon's avatar
qinsoon committed
34
{
35
    pub fn new(start: Address, end: Address) -> AddressMap<T> {
36
        let len = (end - start) >> LOG_POINTER_SIZE;
qinsoon's avatar
qinsoon committed
37
38
39
40
41
42
        let ptr = unsafe { malloc_zero(mem::size_of::<T>() * len) } as *mut T;

        AddressMap {
            start: start,
            end: end,
            ptr: ptr,
43
            len: len
qinsoon's avatar
qinsoon committed
44
        }
45
    }
qinsoon's avatar
qinsoon committed
46
47

    pub fn init_all(&self, init: T) {
qinsoon's avatar
qinsoon committed
48
        let mut cursor = self.start;
qinsoon's avatar
qinsoon committed
49

qinsoon's avatar
qinsoon committed
50
51
        while cursor < self.end {
            self.set(cursor, init);
52
            cursor = cursor + POINTER_SIZE;
qinsoon's avatar
qinsoon committed
53
54
        }
    }
qinsoon's avatar
qinsoon committed
55

56
57
    #[inline(always)]
    pub fn set(&self, addr: Address, value: T) {
58
        let index = ((addr - self.start) >> LOG_POINTER_SIZE) as isize;
qinsoon's avatar
qinsoon committed
59
        unsafe { *self.ptr.offset(index) = value };
60
61
62
63
    }

    #[inline(always)]
    pub fn get(&self, addr: Address) -> T {
64
        let index = ((addr - self.start) >> LOG_POINTER_SIZE) as isize;
qinsoon's avatar
qinsoon committed
65
        unsafe { *self.ptr.offset(index) }
66
    }
67
}
68
69
70
71
72
73
74
75
76

impl<T> Drop for AddressMap<T>
where
    T: Copy
{
    fn drop(&mut self) {
        unsafe { free(self.ptr) }
    }
}