GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (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.

global_type_table.rs 7.36 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

use std::sync::Mutex;
qinsoon's avatar
qinsoon committed
16
use std::sync::RwLock;
qinsoon's avatar
qinsoon committed
17
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
qinsoon's avatar
qinsoon committed
18
use std::collections::HashMap;
qinsoon's avatar
qinsoon committed
19
use std::mem;
20
use utils::mem::*;
qinsoon's avatar
qinsoon committed
21
use utils::math;
22
use utils::*;
qinsoon's avatar
qinsoon committed
23 24 25

use objectmodel::sidemap::TypeID;
use objectmodel::sidemap::N_TYPES;
qinsoon's avatar
qinsoon committed
26
use objectmodel::sidemap::type_encode::*;
qinsoon's avatar
qinsoon committed
27 28 29 30 31
use objectmodel::sidemap::object_encode::SMALL_ID_WIDTH;

/// represents a chunk of memory as global type table, which contains some metadata for the
/// type table and all the type encoding entries.
#[repr(C, packed)]
32
pub struct GlobalTypeTable {
qinsoon's avatar
qinsoon committed
33 34 35
    /// current index for small entries
    small_entry_i: usize,
    /// current index for large entries
qinsoon's avatar
qinsoon committed
36 37
    large_entry_i: usize,
    /// full entries
38 39 40 41
    full_entry_i: usize,
    full_entries: RwLock<HashMap<usize, FullTypeEncode>>,

    #[allow(dead_code)]
42 43
    mmap_start: Address,
    mmap_size: ByteSize,
qinsoon's avatar
qinsoon committed
44

45
    table: [ShortTypeEncode; N_TYPES]
qinsoon's avatar
qinsoon committed
46 47 48 49
}

const SMALL_ENTRY_CAP: usize = 1 << SMALL_ID_WIDTH;
const LARGE_ENTRY_CAP: usize = N_TYPES;
qinsoon's avatar
fix  
qinsoon committed
50
const FULL_ENTRY_START: usize = LARGE_ENTRY_CAP + 1;
qinsoon's avatar
qinsoon committed
51 52

/// storing a pointer to the actual type table
qinsoon's avatar
qinsoon committed
53
static GLOBAL_TYPE_TABLE_PTR: AtomicUsize = ATOMIC_USIZE_INIT;
qinsoon's avatar
qinsoon committed
54
/// storing a pointer to the metadata of the type table
qinsoon's avatar
qinsoon committed
55
static GLOBAL_TYPE_TABLE_META: AtomicUsize = ATOMIC_USIZE_INIT;
qinsoon's avatar
qinsoon committed
56 57 58

impl GlobalTypeTable {
    pub fn init() {
59
        debug!("Init GlobalTypeTable...");
60 61
        let mmap_size = mem::size_of::<GlobalTypeTable>();
        let mmap = mmap_large(mmap_size);
qinsoon's avatar
qinsoon committed
62

63
        info!("Global Type Table allocated at {}", mmap);
qinsoon's avatar
qinsoon committed
64 65

        // start address of metadata
66
        let meta_addr = mmap;
qinsoon's avatar
qinsoon committed
67
        GLOBAL_TYPE_TABLE_META.store(meta_addr.as_usize(), Ordering::Relaxed);
68 69 70

        let mut meta: &mut GlobalTypeTable = unsafe { meta_addr.to_ref_mut() };

qinsoon's avatar
qinsoon committed
71
        // actual table
72
        let table_addr = Address::from_ptr(&meta.table as *const [ShortTypeEncode; N_TYPES]);
qinsoon's avatar
qinsoon committed
73
        GLOBAL_TYPE_TABLE_PTR.store(table_addr.as_usize(), Ordering::Relaxed);
qinsoon's avatar
qinsoon committed
74 75 76 77

        // initialize meta
        meta.small_entry_i = 0;
        meta.large_entry_i = SMALL_ENTRY_CAP;
qinsoon's avatar
fix  
qinsoon committed
78
        meta.full_entry_i = FULL_ENTRY_START;
qinsoon's avatar
qinsoon committed
79 80 81
        unsafe {
            use std::ptr;
            ptr::write(
82
                &mut meta.full_entries as *mut RwLock<HashMap<usize, FullTypeEncode>>,
qinsoon's avatar
qinsoon committed
83 84 85
                RwLock::new(HashMap::new())
            )
        }
86 87
        meta.mmap_start = mmap;
        meta.mmap_size = mmap_size;
qinsoon's avatar
qinsoon committed
88 89 90 91 92

        // save mmap
        trace!("Global Type Table initialization done");
    }

93
    pub fn cleanup() {
94 95 96 97 98 99
        // unmap the table
        let mmap_start = GlobalTypeTable::table_meta().mmap_start;
        let mmap_size = GlobalTypeTable::table_meta().mmap_size;
        munmap(mmap_start, mmap_size);

        // set pointers to zero
100 101 102 103
        GLOBAL_TYPE_TABLE_PTR.store(0, Ordering::Relaxed);
        GLOBAL_TYPE_TABLE_META.store(0, Ordering::Relaxed);
    }

qinsoon's avatar
qinsoon committed
104 105
    #[inline(always)]
    fn table_meta() -> &'static mut GlobalTypeTable {
qinsoon's avatar
qinsoon committed
106
        unsafe { mem::transmute(GLOBAL_TYPE_TABLE_META.load(Ordering::Relaxed)) }
qinsoon's avatar
qinsoon committed
107 108 109
    }

    #[inline(always)]
110
    pub fn table() -> &'static mut [ShortTypeEncode; N_TYPES] {
qinsoon's avatar
qinsoon committed
111
        unsafe { mem::transmute(GLOBAL_TYPE_TABLE_PTR.load(Ordering::Relaxed)) }
qinsoon's avatar
qinsoon committed
112 113
    }

114
    pub fn insert_small_entry(entry: ShortTypeEncode) -> TypeID {
qinsoon's avatar
qinsoon committed
115 116 117 118 119 120 121 122 123 124 125 126 127
        let mut meta = GlobalTypeTable::table_meta();
        let mut table = GlobalTypeTable::table();

        if meta.small_entry_i < SMALL_ENTRY_CAP {
            let id = meta.small_entry_i;
            table[id] = entry;
            meta.small_entry_i += 1;
            id
        } else {
            panic!("small type entries overflow the global type table")
        }
    }

128
    pub fn insert_large_entry(entry: ShortTypeEncode) -> TypeID {
qinsoon's avatar
qinsoon committed
129 130 131 132 133 134 135 136 137 138 139 140
        let mut meta = GlobalTypeTable::table_meta();
        let mut table = GlobalTypeTable::table();

        if meta.large_entry_i < LARGE_ENTRY_CAP {
            let id = meta.large_entry_i;
            table[id] = entry;
            meta.large_entry_i += 1;
            id
        } else {
            panic!("large type entries overflow the global type table")
        }
    }
qinsoon's avatar
qinsoon committed
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    pub fn force_set_short_entry(index: TypeID, entry: ShortTypeEncode) {
        let mut meta = GlobalTypeTable::table_meta();
        let mut table = GlobalTypeTable::table();

        table[index] = entry;

        if index < SMALL_ENTRY_CAP {
            if meta.small_entry_i < index {
                meta.small_entry_i = index;
            }
        } else if index < LARGE_ENTRY_CAP {
            if meta.large_entry_i < index {
                meta.large_entry_i = index;
            }
        } else {
            panic!(
                "TypeID {} exceeds LARGE_ENTRY_CAP, try use insert_full_entry() instead",
                index
            )
        }
    }

    pub fn insert_full_entry(entry: FullTypeEncode) -> TypeID {
qinsoon's avatar
qinsoon committed
165 166 167
        let meta = GlobalTypeTable::table_meta();

        let mut lock = meta.full_entries.write().unwrap();
168
        let id = meta.full_entry_i;
qinsoon's avatar
qinsoon committed
169
        lock.insert(id, entry);
170
        meta.full_entry_i += 1;
qinsoon's avatar
qinsoon committed
171 172 173 174

        id
    }

175 176 177 178 179 180 181 182 183 184 185 186
    pub fn force_set_full_entry(index: TypeID, entry: FullTypeEncode) {
        let mut meta = GlobalTypeTable::table_meta();
        let mut lock = meta.full_entries.write().unwrap();
        assert!(!lock.contains_key(&index));
        lock.insert(index, entry);

        if meta.full_entry_i < index {
            meta.full_entry_i = index;
        }
    }

    pub fn get_full_type(id: usize) -> FullTypeEncode {
qinsoon's avatar
qinsoon committed
187 188 189 190 191
        let meta = GlobalTypeTable::table_meta();
        let lock = meta.full_entries.read().unwrap();
        debug_assert!(lock.contains_key(&id));
        lock.get(&id).unwrap().clone()
    }
qinsoon's avatar
qinsoon committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

#[cfg(test)]
mod global_type_table_test {
    use super::*;
    use objectmodel::sidemap::type_encode::WordType::*;
    use start_logging_trace;

    #[test]
    fn test_insert() {
        start_logging_trace();

        GlobalTypeTable::init();
        let ty = {
            let mut fix_ty = [0; 63];
            fix_ty[0] = 0b11100100u8;
            fix_ty[1] = 0b00011011u8;
            fix_ty[2] = 0b11100100u8;
210
            ShortTypeEncode::new(8, 12, fix_ty, 0, [0; 63])
qinsoon's avatar
qinsoon committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        };
        let tyid = GlobalTypeTable::insert_small_entry(ty) as usize;

        let ref loaded_ty = GlobalTypeTable::table()[tyid];
        assert_eq!(loaded_ty.fix_ty(0), NonRef);
        assert_eq!(loaded_ty.fix_ty(1), Ref);
        assert_eq!(loaded_ty.fix_ty(2), WeakRef);
        assert_eq!(loaded_ty.fix_ty(3), TaggedRef);
        assert_eq!(loaded_ty.fix_ty(4), TaggedRef);
        assert_eq!(loaded_ty.fix_ty(5), WeakRef);
        assert_eq!(loaded_ty.fix_ty(6), Ref);
        assert_eq!(loaded_ty.fix_ty(7), NonRef);
        assert_eq!(loaded_ty.fix_ty(8), NonRef);
        assert_eq!(loaded_ty.fix_ty(9), Ref);
        assert_eq!(loaded_ty.fix_ty(10), WeakRef);
        assert_eq!(loaded_ty.fix_ty(11), TaggedRef);
    }
}