mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Address followup review comments.
MozReview-Commit-ID: AghLMEcbUVC
This commit is contained in:
parent
f388c0ab1e
commit
817a2c6cca
1 changed files with 16 additions and 16 deletions
|
@ -481,6 +481,8 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
|
||||||
//
|
//
|
||||||
// To avoid alignment issues, we allocate words rather than bytes,
|
// To avoid alignment issues, we allocate words rather than bytes,
|
||||||
// rounding up to the nearest word size.
|
// rounding up to the nearest word size.
|
||||||
|
assert!(mem::align_of::<T>() <= mem::align_of::<usize>(),
|
||||||
|
"We don't handle over-aligned types");
|
||||||
let words_to_allocate = divide_rounding_up(size, size_of::<usize>());
|
let words_to_allocate = divide_rounding_up(size, size_of::<usize>());
|
||||||
let mut vec = Vec::<usize>::with_capacity(words_to_allocate);
|
let mut vec = Vec::<usize>::with_capacity(words_to_allocate);
|
||||||
vec.set_len(words_to_allocate);
|
vec.set_len(words_to_allocate);
|
||||||
|
@ -496,6 +498,9 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
|
||||||
ptr = fake_slice as *mut [T] as *mut ArcInner<HeaderSlice<H, [T]>>;
|
ptr = fake_slice as *mut [T] as *mut ArcInner<HeaderSlice<H, [T]>>;
|
||||||
|
|
||||||
// Write the data.
|
// Write the data.
|
||||||
|
//
|
||||||
|
// Note that any panics here (i.e. from the iterator) are safe, since
|
||||||
|
// we'll just leak the uninitialized memory.
|
||||||
ptr::write(&mut ((*ptr).count), atomic::AtomicUsize::new(1));
|
ptr::write(&mut ((*ptr).count), atomic::AtomicUsize::new(1));
|
||||||
ptr::write(&mut ((*ptr).data.header), header);
|
ptr::write(&mut ((*ptr).data.header), header);
|
||||||
let mut current: *mut T = &mut (*ptr).data.slice[0];
|
let mut current: *mut T = &mut (*ptr).data.slice[0];
|
||||||
|
@ -536,8 +541,9 @@ impl<H> HeaderWithLength<H> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HeaderSliceWithLength<H, T> = HeaderSlice<HeaderWithLength<H>, T>;
|
||||||
pub struct ThinArc<H, T> {
|
pub struct ThinArc<H, T> {
|
||||||
ptr: *mut ArcInner<HeaderSlice<HeaderWithLength<H>, [T; 1]>>,
|
ptr: *mut ArcInner<HeaderSliceWithLength<H, [T; 1]>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<H: Sync + Send, T: Sync + Send> Send for ThinArc<H, T> {}
|
unsafe impl<H: Sync + Send, T: Sync + Send> Send for ThinArc<H, T> {}
|
||||||
|
@ -546,15 +552,15 @@ unsafe impl<H: Sync + Send, T: Sync + Send> Sync for ThinArc<H, T> {}
|
||||||
// Synthesize a fat pointer from a thin pointer.
|
// Synthesize a fat pointer from a thin pointer.
|
||||||
//
|
//
|
||||||
// See the comment around the analogous operation in from_header_and_iter.
|
// See the comment around the analogous operation in from_header_and_iter.
|
||||||
fn thin_to_thick<H, T>(thin: *mut ArcInner<HeaderSlice<HeaderWithLength<H>, [T; 1]>>)
|
fn thin_to_thick<H, T>(thin: *mut ArcInner<HeaderSliceWithLength<H, [T; 1]>>)
|
||||||
-> *mut ArcInner<HeaderSlice<HeaderWithLength<H>, [T]>>
|
-> *mut ArcInner<HeaderSliceWithLength<H, [T]>>
|
||||||
{
|
{
|
||||||
let len = unsafe { (*thin).data.header.length };
|
let len = unsafe { (*thin).data.header.length };
|
||||||
let fake_slice: *mut [T] = unsafe {
|
let fake_slice: *mut [T] = unsafe {
|
||||||
slice::from_raw_parts_mut(thin as *mut T, len)
|
slice::from_raw_parts_mut(thin as *mut T, len)
|
||||||
};
|
};
|
||||||
|
|
||||||
fake_slice as *mut ArcInner<HeaderSlice<HeaderWithLength<H>, [T]>>
|
fake_slice as *mut ArcInner<HeaderSliceWithLength<H, [T]>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H, T> ThinArc<H, T> {
|
impl<H, T> ThinArc<H, T> {
|
||||||
|
@ -562,7 +568,7 @@ impl<H, T> ThinArc<H, T> {
|
||||||
/// provided callback. The refcount is not modified.
|
/// provided callback. The refcount is not modified.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn with_arc<F, U>(&self, f: F) -> U
|
pub fn with_arc<F, U>(&self, f: F) -> U
|
||||||
where F: FnOnce(&Arc<HeaderSlice<HeaderWithLength<H>, [T]>>) -> U
|
where F: FnOnce(&Arc<HeaderSliceWithLength<H, [T]>>) -> U
|
||||||
{
|
{
|
||||||
// Synthesize transient Arc, which never touches the refcount of the ArcInner.
|
// Synthesize transient Arc, which never touches the refcount of the ArcInner.
|
||||||
let transient = NoDrop::new(Arc {
|
let transient = NoDrop::new(Arc {
|
||||||
|
@ -581,7 +587,7 @@ impl<H, T> ThinArc<H, T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H, T> Deref for ThinArc<H, T> {
|
impl<H, T> Deref for ThinArc<H, T> {
|
||||||
type Target = HeaderSlice<HeaderWithLength<H>, [T]>;
|
type Target = HeaderSliceWithLength<H, [T]>;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
unsafe { &(*thin_to_thick(self.ptr)).data }
|
unsafe { &(*thin_to_thick(self.ptr)).data }
|
||||||
}
|
}
|
||||||
|
@ -599,17 +605,17 @@ impl<H, T> Drop for ThinArc<H, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H, T> Arc<HeaderSlice<HeaderWithLength<H>, [T]>> {
|
impl<H, T> Arc<HeaderSliceWithLength<H, [T]>> {
|
||||||
/// Converts an Arc into a ThinArc. This consumes the Arc, so the refcount
|
/// Converts an Arc into a ThinArc. This consumes the Arc, so the refcount
|
||||||
/// is not modified.
|
/// is not modified.
|
||||||
pub fn into_thin(a: Self) -> ThinArc<H, T> {
|
pub fn into_thin(a: Self) -> ThinArc<H, T> {
|
||||||
assert!(a.header.length == a.slice.len(),
|
assert!(a.header.length == a.slice.len(),
|
||||||
"Length needs to be correct for ThinArc to work");
|
"Length needs to be correct for ThinArc to work");
|
||||||
let fat_ptr: *mut ArcInner<HeaderSlice<HeaderWithLength<H>, [T]>> = a.ptr;
|
let fat_ptr: *mut ArcInner<HeaderSliceWithLength<H, [T]>> = a.ptr;
|
||||||
mem::forget(a);
|
mem::forget(a);
|
||||||
let thin_ptr = fat_ptr as *mut [usize] as *mut usize;
|
let thin_ptr = fat_ptr as *mut [usize] as *mut usize;
|
||||||
ThinArc {
|
ThinArc {
|
||||||
ptr: thin_ptr as *mut ArcInner<HeaderSlice<HeaderWithLength<H>, [T; 1]>>
|
ptr: thin_ptr as *mut ArcInner<HeaderSliceWithLength<H, [T; 1]>>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -649,13 +655,7 @@ mod tests {
|
||||||
|
|
||||||
impl Drop for Canary {
|
impl Drop for Canary {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe { (*self.0).fetch_add(1, SeqCst); }
|
||||||
match *self {
|
|
||||||
Canary(c) => {
|
|
||||||
(*c).fetch_add(1, SeqCst);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue