Skip to content

Commit

Permalink
Implement Condemned for (A, B)
Browse files Browse the repository at this point in the history
OFFSET as a const parameter is blocked on rust-lang/rust#68436
  • Loading branch information
Avi-D-coder committed Jun 21, 2020
1 parent def5708 commit ad012d4
Showing 1 changed file with 55 additions and 7 deletions.
62 changes: 55 additions & 7 deletions src/mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ pub struct Handlers {

pub unsafe trait Condemned {
fn feilds(s: &Self, grey_feilds: u8, region: Range<usize>) -> u8;
fn evacuate<'e, const OFFSET: u8>(
fn evacuate<'e>(
s: &Self,
offset: u8,
grey_feilds: u8,
region: Range<usize>,
handlers: &mut Handlers,
);

const GC_COUNT: u8;
const PRE_CONDTION: bool;
}

Expand All @@ -52,8 +54,9 @@ unsafe impl<T> Condemned for T {
}
}

default fn evacuate<'e, const OFFSET: u8>(_: &Self, _: u8, _: Range<usize>, _: &mut Handlers) {}
default fn evacuate<'e>(_: &Self, _: u8, _: u8, _: Range<usize>, _: &mut Handlers) {}

default const GC_COUNT: u8 = 0;
default const PRE_CONDTION: bool = if T::HAS_GC {
// TODO When fmt is allowed in const s/your type/type_name::<T>()
panic!("You need to derive Condemned for your type. Required due to a direct Gc<T>");
Expand All @@ -66,17 +69,18 @@ unsafe impl<'r, T> Condemned for Gc<'r, T> {
fn feilds(_: &Self, _: u8, _: std::ops::Range<usize>) -> u8 {
0b0000_0000
}
const PRE_CONDTION: bool = true;
fn evacuate<'e, const OFFSET: u8>(

fn evacuate<'e>(
s: &Self,
offset: u8,
_: u8,
region: std::ops::Range<usize>,
handlers: &mut Handlers,
) {
let ptr = s.ptr as *const T;
let addr = ptr as usize;
if region.contains(&addr) {
let i = handlers.translation[OFFSET as usize];
let i = handlers.translation[offset as usize];
if let Some(next) = handlers.nexts.get_mut(i as usize) {
let next_addr = *next as usize;
let header_addr = next_addr - next_addr % ARENA_SIZE;
Expand Down Expand Up @@ -105,6 +109,9 @@ unsafe impl<'r, T> Condemned for Gc<'r, T> {
}
}
}

const GC_COUNT: u8 = 1;
const PRE_CONDTION: bool = T::PRE_CONDTION;
}

// std impls
Expand All @@ -117,14 +124,15 @@ unsafe impl<T> Condemned for Option<T> {
}
}

fn evacuate<'e, const OFFSET: u8>(
fn evacuate<'e>(
s: &Self,
offset: u8,
grey_feilds: u8,
region: Range<usize>,
handlers: &mut Handlers,
) {
match s {
Some(t) => Condemned::evacuate::<OFFSET>(t, grey_feilds, region, handlers),
Some(t) => Condemned::evacuate(t, offset, grey_feilds, region, handlers),
None => (),
}
}
Expand All @@ -135,3 +143,43 @@ unsafe impl<T> Condemned for Option<T> {
panic!("You need to derive Condemned for T. Required due to a direct Gc<A> in Option<T>");
};
}

unsafe impl<A, B> Condemned for (A, B) {
fn feilds((a, b): &Self, grey_feilds: u8, region: Range<usize>) -> u8 {
let mut r = 0b0000_0000;
if (grey_feilds & 0b1000_0000) == 0b1000_0000 {
r |= Condemned::feilds(a, grey_feilds, region.clone());
};

if (grey_feilds & 0b0100_0000) == 0b0100_0000 {
r |= Condemned::feilds(b, grey_feilds, region);
};

r
}

fn evacuate<'e>(
(a, b): &Self,
offset: u8,
grey_feilds: u8,
region: Range<usize>,
handlers: &mut Handlers,
) {
Condemned::evacuate(a, offset, grey_feilds, region.clone(), handlers);

Condemned::evacuate(
b,
offset + A::GC_COUNT,
grey_feilds,
region.clone(),
handlers,
);
}

const GC_COUNT: u8 = A::GC_COUNT + B::GC_COUNT;
const PRE_CONDTION: bool = if A::PRE_CONDTION && B::PRE_CONDTION {
true
} else {
panic!("You need to derive Condemned for A & B. Required due to a direct Gc<T> in (A, B)");
};
}

0 comments on commit ad012d4

Please sign in to comment.