Commit 8b397589 authored by Haley Owsianko's avatar Haley Owsianko
Browse files

Attempt to add debugging tools

parent 47405c3b
......@@ -14,9 +14,16 @@ pub struct BaseRegisters {
pub ob: usize,
}
#[derive(Debug)]
struct Block {
pub start: usize,
pub end: usize,
}
pub struct Memory {
content: Vec<L3Value>,
heap_start_ix: usize,
allocated: Vec<Block>,
bitmap_start_ix: usize,
free_ix: usize,
}
......@@ -57,12 +64,67 @@ impl Memory {
pub fn new(word_size: usize) -> Memory {
Memory {
content: vec![0; word_size],
allocated: vec!(),
heap_start_ix: 0,
bitmap_start_ix: 0,
free_ix: 0,
}
}
fn mark_allocated(&mut self, block: Block) {
println!("marking allocated…");
println!("{:?}", block);
println!("{:?}", self.allocated);
let mut upper = self.allocated.len();
let mut lower = 0;
let mut mid = (lower + upper) / 2;
while lower + 1 < upper {
mid = (lower + upper) / 2;
if self.allocated[mid].start < block.start {
lower = mid
} else {
upper = mid
}
}
mid = mid + 1;
if mid > 0 && mid - 1 < self.allocated.len() {
let prev_block = &self.allocated[mid-1];
// sanity check
debug_assert!(prev_block.start < block.start, "prev: {:?}
current: {:?}", prev_block, block);
debug_assert!(prev_block.end <= block.start - Memory::HEADER_SIZE as usize, "prev: {:?}
current: {:?}", prev_block, block);
}
println!("{}", mid);
if mid < self.allocated.len() {
let next_block = &self.allocated[mid];
debug_assert!(block.start < next_block.start, "next: {:?}
current: {:?}", next_block, block);
debug_assert!(block.end <= next_block.start - Memory::HEADER_SIZE as usize, "next: {:?}
current: {:?}", next_block, block);
}
self.allocated.insert(mid, block);
println!("done");
}
fn mark_unallocated(&mut self, block: &Block) {
let len = self.allocated.len();
Memory::delete(block, &mut self.allocated, 0, len)
}
fn delete(block: &Block, v: &mut Vec<Block>, lower: usize, upper: usize) {
let mid = (lower + upper) / 2;
if lower == upper {
debug_assert!(block.start == v[mid].start && block.end == v[mid].end);
v.remove(mid);
} else if v[mid].start < block.start {
Memory::delete(block, v, mid, upper)
} else {
Memory::delete(block, v, lower, mid)
}
}
fn bitmap(&self) -> &[L3Value] {
&self.content[self.bitmap_start_ix..self.heap_start_ix]
}
......@@ -190,6 +252,7 @@ impl Memory {
self.set_block_tag_size(block_ix, tag, requested_size);
self.set_mark(block_ix);
self.mark_allocated(Block{start:block_ix, end:block_ix+size as usize});
debug_assert!(self.assignment_consistent(block_ix));
block_ix
}
......@@ -212,10 +275,29 @@ impl Memory {
}
block = next_block as usize;
}
let mut upper = self.allocated.len();
let mut lower = 0;
let mut mid = upper/2;
while upper != lower {
mid = (upper + lower) / 2;
if self.allocated[mid].start < just_allocated {
lower = mid;
} else {
upper = mid;
}
}
if mid > 0 {
// just_allocated isn't the first block in memory
// check previous block
}
true
}
fn memory_consistent(&self) -> bool {
// let just_alloc_size = self.internal_block_size(just_allocated) as usize;
// let just_alloc_end = just_alloc_size + just_allocated; // Excluded
......@@ -314,6 +396,9 @@ impl Memory {
let sweep_size = self.internal_block_size(sweep_ix);
if self.is_reclaimable(sweep_ix) {
if self.is_marked(sweep_ix) {
self.mark_unallocated(&Block{start:sweep_ix, end:sweep_ix+sweep_size as usize})
}
self.clear_mark(sweep_ix);
if let Some(pred_ix) = predecessor_ix {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment