test_gc_linked_list.rs 4.72 KB
Newer Older
1 2 3 4 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
extern crate gc;
extern crate utils;

use std::ptr;
extern crate simple_logger;
extern crate log;
use self::log::LogLevel;

use std::fmt;

pub fn start_logging() {
    match simple_logger::init_with_level(LogLevel::Trace) {
        Ok(_) => {},
        Err(_) => {}
    }
}

#[derive(Copy, Clone)]
struct Node {
    next   : *mut Node,
    payload: usize
}

struct LinkedList<'a> {
    head: *mut Node,
    tail: *mut Node,
    len : usize,

    allocator: &'a mut ImmixMutatorLocal
}

impl <'a> LinkedList<'a> {
    fn new(allocator: &mut ImmixMutatorLocal) -> LinkedList {
        LinkedList {
            head: ptr::null_mut(),
            tail: ptr::null_mut(),
            len : 0,

            allocator: allocator
        }
    }

    fn add(&mut self, val: usize) {
        if self.head.is_null() {
            let node = Node::new(self.allocator, val);

            self.head = node;
            self.tail = node;
            self.len  = 1;
        } else {
            let node = Node::new(self.allocator, val);

            unsafe {
                (*self.tail).next = node;
            }
            self.tail = node;
            self.len += 1;
        }
    }

    fn verify(&self, expect: Vec<usize>) {
        if self.len != expect.len() {
            panic!("Linked List length: {}, expected length: {}", self.len, expect.len());
        }

        let mut i = 0;
        let mut cursor = self.head;

        while cursor != self.tail {
            println!("-verifying {:?}-", cursor);
            println!("{:?}", unsafe {*cursor});

            let val = unsafe {(*cursor).payload};
            let expect_val = expect[i];

            if val != expect_val {
                panic!("Linked List[{}] = {}, expect[{}] = {}", i, val, i, expect_val);
            }

            cursor = unsafe {(*cursor).next};
            i += 1;
        }
    }
}

impl <'a> fmt::Debug for LinkedList<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut cursor = self.head;

        // non-tail
        while cursor != self.tail {
            write!(f, "{:?}, ", unsafe {*cursor}).unwrap();
            cursor = unsafe{*cursor}.next;
        }

        write!(f, "{:?}", unsafe {*cursor}).unwrap();

        Ok(())
    }
}

use self::gc::heap;
use self::gc::heap::immix::ImmixMutatorLocal;
use self::gc::heap::immix::ImmixSpace;
use self::gc::heap::freelist;
use self::gc::heap::freelist::FreeListSpace;
use self::gc::objectmodel;
use self::utils::{ObjectReference, Address};
use std::mem::size_of;

#[cfg(feature = "use-sidemap")]
const NODE_ENCODE : u64 = 0b1100_0001u64;
#[cfg(not(feature = "use-sidemap"))]
const NODE_ENCODE : u64 = 0xb000000000000001u64;

impl Node {
    fn new(mutator: &mut ImmixMutatorLocal, val: usize) -> *mut Node {
        println!("-allocating Node({})-", val);

        let addr = mutator.alloc(size_of::<Node>(), 8);
        println!("returns address {}", addr);

        mutator.init_object(addr, NODE_ENCODE);

        let ptr = addr.to_ptr_mut::<Node>();
        println!("as pointer {:?}", ptr);

        unsafe {
            (*ptr).payload = val;
        }
        println!("result: {:?}", unsafe {*ptr});

        ptr
    }
}

impl fmt::Debug for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Node({})", self.payload)
    }
}

const IMMIX_SPACE_SIZE : usize = 40 << 20;
const LO_SPACE_SIZE    : usize = 0 << 20;

#[test]
fn create_linked_list() {
    unsafe {heap::gc::set_low_water_mark();}

    start_logging();

    gc::gc_init(IMMIX_SPACE_SIZE, LO_SPACE_SIZE, 1);
    gc::gc_stats();

    let mut mutator = gc::new_mutator();

    {
        let mut linked_list = LinkedList::new(&mut mutator);

        const N: usize = 5;

        for i in 0..N {
            linked_list.add(i);

            println!("after add: {:?}", linked_list);
        }

        linked_list.verify((0..N).collect());
    }

    mutator.destroy();
}

#[test]
#[ignore]
// disable this test because it will cause gcbench fail for unknown reason
fn linked_list_survive_gc() {
    unsafe {heap::gc::set_low_water_mark();}

    start_logging();

    gc::gc_init(IMMIX_SPACE_SIZE, LO_SPACE_SIZE, 1);
    gc::gc_stats();

    let mut mutator = gc::new_mutator();

    {
        let mut linked_list = LinkedList::new(&mut mutator);

        const N: usize = 5;

        for i in 0..N {
            linked_list.add(i);

            println!("after add: {:?}", linked_list);
        }

        // check
        linked_list.verify((0..N).collect());

        // put head as gc root
        let head_addr = Address::from_mut_ptr(linked_list.head);
        gc::add_to_root(unsafe { head_addr.to_object_reference() });

        // force gc
        gc::force_gc();

        // check
        linked_list.verify((0..N).collect());
    }

    mutator.destroy();
213
}