Skip to content

Commit

Permalink
Use raw pointers instead of transmute for UnsafeFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Oct 5, 2017
1 parent 3f07cfe commit ae67eb5
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions components/layout/parallel.rs
Expand Up @@ -8,6 +8,7 @@

#![allow(unsafe_code)]

use block::BlockFlow;
use context::LayoutContext;
use flow::{self, Flow};
use flow_ref::FlowRef;
Expand All @@ -16,6 +17,7 @@ use rayon;
use servo_config::opts;
use smallvec::SmallVec;
use std::mem;
use std::ptr;
use std::sync::atomic::{AtomicIsize, Ordering};
use traversal::{AssignBSizes, AssignISizes, BubbleISizes};
use traversal::{PostorderFlowTraversal, PreorderFlowTraversal};
Expand All @@ -26,22 +28,18 @@ const CHUNK_SIZE: usize = 16;
pub type FlowList = SmallVec<[UnsafeFlow; CHUNK_SIZE]>;

/// Vtable + pointer representation of a Flow trait object.
pub type UnsafeFlow = (usize, usize);
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct UnsafeFlow(*const Flow);

unsafe impl Sync for UnsafeFlow {}
unsafe impl Send for UnsafeFlow {}

fn null_unsafe_flow() -> UnsafeFlow {
(0, 0)
UnsafeFlow(ptr::null::<BlockFlow>())
}

pub fn mut_owned_flow_to_unsafe_flow(flow: *mut FlowRef) -> UnsafeFlow {
unsafe {
mem::transmute::<&Flow, UnsafeFlow>(&**flow)
}
}

pub fn borrowed_flow_to_unsafe_flow(flow: &Flow) -> UnsafeFlow {
unsafe {
mem::transmute::<&Flow, UnsafeFlow>(flow)
}
unsafe { UnsafeFlow(&**flow) }
}

/// Information that we need stored in each flow.
Expand Down Expand Up @@ -103,7 +101,7 @@ fn bottom_up_flow(mut unsafe_flow: UnsafeFlow,
// of our parent to finish processing? If so, we can continue
// on with our parent; otherwise, we've gotta wait.
let parent: &mut Flow = unsafe {
mem::transmute(unsafe_parent)
&mut *(unsafe_parent.0 as *mut Flow)
};
let parent_base = flow::mut_base(parent);
if parent_base.parallel.children_count.fetch_sub(1, Ordering::Relaxed) == 1 {
Expand Down Expand Up @@ -140,7 +138,7 @@ fn top_down_flow<'scope>(unsafe_flows: &[UnsafeFlow],
// Possibly enqueue the children.
for kid in flow::child_iter_mut(flow) {
had_children = true;
discovered_child_flows.push(borrowed_flow_to_unsafe_flow(kid));
discovered_child_flows.push(UnsafeFlow(kid));
}
}

Expand Down Expand Up @@ -191,7 +189,7 @@ pub fn reflow(

let assign_isize_traversal = &AssignISizes { layout_context: &context };
let assign_bsize_traversal = &AssignBSizes { layout_context: &context };
let nodes = [borrowed_flow_to_unsafe_flow(root)];
let nodes = [UnsafeFlow(root)];

queue.install(move || {
rayon::scope(move |scope| {
Expand Down

0 comments on commit ae67eb5

Please sign in to comment.