Stylo: replace uses of mem::uninitialized with MaybeUninit

MozReview-Commit-ID: KGhYL6DJRaR
This commit is contained in:
Simon Sapin 2019-07-06 18:16:30 +02:00
parent 00b3fb49c4
commit 17ec774a49
9 changed files with 60 additions and 68 deletions

View file

@ -52,25 +52,6 @@ use std::sync::atomic;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release}; use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::{isize, usize}; use std::{isize, usize};
// Private macro to get the offset of a struct field in bytes from the address of the struct.
macro_rules! offset_of {
($container:path, $field:ident) => {{
// Make sure the field actually exists. This line ensures that a compile-time error is
// generated if $field is accessed through a Deref impl.
let $container { $field: _, .. };
// Create an (invalid) instance of the container and calculate the offset to its
// field. Using a null pointer might be UB if `&(*(0 as *const T)).field` is interpreted to
// be nullptr deref.
let invalid: $container = ::std::mem::uninitialized();
let offset = &invalid.$field as *const _ as usize - &invalid as *const _ as usize;
// Do not run destructors on the made up invalid instance.
::std::mem::forget(invalid);
offset as isize
}};
}
/// A soft limit on the amount of references that may be made to an `Arc`. /// A soft limit on the amount of references that may be made to an `Arc`.
/// ///
/// Going above this limit will abort your program (although not /// Going above this limit will abort your program (although not
@ -196,6 +177,14 @@ struct ArcInner<T: ?Sized> {
unsafe impl<T: ?Sized + Sync + Send> Send for ArcInner<T> {} unsafe impl<T: ?Sized + Sync + Send> Send for ArcInner<T> {}
unsafe impl<T: ?Sized + Sync + Send> Sync for ArcInner<T> {} unsafe impl<T: ?Sized + Sync + Send> Sync for ArcInner<T> {}
/// Computes the offset of the data field within ArcInner.
fn data_offset<T>() -> usize {
let size = size_of::<ArcInner<()>>();
let align = align_of::<T>();
// https://github.com/rust-lang/rust/blob/1.36.0/src/libcore/alloc.rs#L187-L207
size.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1)
}
impl<T> Arc<T> { impl<T> Arc<T> {
/// Construct an `Arc<T>` /// Construct an `Arc<T>`
#[inline] #[inline]
@ -251,7 +240,7 @@ impl<T> Arc<T> {
unsafe fn from_raw(ptr: *const T) -> Self { unsafe fn from_raw(ptr: *const T) -> Self {
// To find the corresponding pointer to the `ArcInner` we need // To find the corresponding pointer to the `ArcInner` we need
// to subtract the offset of the `data` field from the pointer. // to subtract the offset of the `data` field from the pointer.
let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner<T>, data)); let ptr = (ptr as *const u8).sub(data_offset::<T>());
Arc { Arc {
p: ptr::NonNull::new_unchecked(ptr as *mut ArcInner<T>), p: ptr::NonNull::new_unchecked(ptr as *mut ArcInner<T>),
phantom: PhantomData, phantom: PhantomData,

View file

@ -29,15 +29,15 @@ impl<'a> AutoProfilerLabel<'a> {
/// stack. /// stack.
#[inline] #[inline]
pub unsafe fn new( pub unsafe fn new(
label: &mut structs::AutoProfilerLabel, label: &mut std::mem::MaybeUninit<structs::AutoProfilerLabel>,
label_type: ProfilerLabel, label_type: ProfilerLabel,
) -> AutoProfilerLabel { ) -> AutoProfilerLabel {
let category_pair = match label_type { let category_pair = match label_type {
ProfilerLabel::Style => structs::JS::ProfilingCategoryPair_LAYOUT_StyleComputation, ProfilerLabel::Style => structs::JS::ProfilingCategoryPair_LAYOUT_StyleComputation,
ProfilerLabel::Parse => structs::JS::ProfilingCategoryPair_LAYOUT_CSSParsing, ProfilerLabel::Parse => structs::JS::ProfilingCategoryPair_LAYOUT_CSSParsing,
}; };
structs::Gecko_Construct_AutoProfilerLabel(label, category_pair); structs::Gecko_Construct_AutoProfilerLabel(label.as_mut_ptr(), category_pair);
AutoProfilerLabel(label) AutoProfilerLabel(&mut *label.as_mut_ptr())
} }
} }

View file

@ -202,7 +202,8 @@ impl WeakAtom {
where where
F: FnOnce(&str) -> Output, F: FnOnce(&str) -> Output,
{ {
let mut buffer: [u8; 64] = unsafe { mem::uninitialized() }; let mut buffer = mem::MaybeUninit::<[u8; 64]>::uninit();
let buffer = unsafe { &mut *buffer.as_mut_ptr() };
// The total string length in utf16 is going to be less than or equal // The total string length in utf16 is going to be less than or equal
// the slice length (each utf16 character is going to take at least one // the slice length (each utf16 character is going to take at least one
@ -271,7 +272,8 @@ impl WeakAtom {
} }
let slice = self.as_slice(); let slice = self.as_slice();
let mut buffer: [u16; 64] = unsafe { mem::uninitialized() }; let mut buffer = mem::MaybeUninit::<[u16; 64]>::uninit();
let buffer = unsafe { &mut *buffer.as_mut_ptr() };
let mut vec; let mut vec;
let mutable_slice = if let Some(buffer_prefix) = buffer.get_mut(..slice.len()) { let mutable_slice = if let Some(buffer_prefix) = buffer.get_mut(..slice.len()) {
buffer_prefix.copy_from_slice(slice); buffer_prefix.copy_from_slice(slice);

View file

@ -112,8 +112,8 @@ macro_rules! define_keyword_type {
#[macro_export] #[macro_export]
macro_rules! profiler_label { macro_rules! profiler_label {
($label_type:ident) => { ($label_type:ident) => {
let mut _profiler_label: $crate::gecko_bindings::structs::AutoProfilerLabel = let mut _profiler_label =
unsafe { ::std::mem::uninitialized() }; ::std::mem::MaybeUninit::<$crate::gecko_bindings::structs::AutoProfilerLabel>::uninit();
let _profiler_label = if $crate::gecko::profiler::profiler_is_active() { let _profiler_label = if $crate::gecko::profiler::profiler_is_active() {
unsafe { unsafe {
Some($crate::gecko::profiler::AutoProfilerLabel::new( Some($crate::gecko::profiler::AutoProfilerLabel::new(

View file

@ -44,9 +44,9 @@ use crate::properties::computed_value_flags::*;
use crate::properties::longhands; use crate::properties::longhands;
use crate::rule_tree::StrongRuleNode; use crate::rule_tree::StrongRuleNode;
use crate::selector_parser::PseudoElement; use crate::selector_parser::PseudoElement;
use servo_arc::{Arc, RawOffsetArc}; use servo_arc::{Arc, RawOffsetArc, UniqueArc};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem::{forget, uninitialized, zeroed, ManuallyDrop}; use std::mem::{forget, zeroed, ManuallyDrop};
use std::{cmp, ops, ptr}; use std::{cmp, ops, ptr};
use crate::values::{self, CustomIdent, Either, KeyframesName, None_}; use crate::values::{self, CustomIdent, Either, KeyframesName, None_};
use crate::values::computed::{Percentage, TransitionProperty}; use crate::values::computed::{Percentage, TransitionProperty};
@ -210,19 +210,18 @@ impl ComputedValuesInner {
Some(p) => p.pseudo_type(), Some(p) => p.pseudo_type(),
None => structs::PseudoStyleType::NotPseudo, None => structs::PseudoStyleType::NotPseudo,
}; };
let arc = unsafe { unsafe {
let arc: Arc<ComputedValues> = Arc::new(uninitialized()); let mut arc = UniqueArc::<ComputedValues>::new_uninit();
bindings::Gecko_ComputedStyle_Init( bindings::Gecko_ComputedStyle_Init(
&arc.0 as *const _ as *mut _, &mut (*arc.as_mut_ptr()).0,
&self, &self,
pseudo_ty, pseudo_ty,
); );
// We're simulating a move by having C++ do a memcpy and then forgetting // We're simulating move semantics by having C++ do a memcpy and then forgetting
// it on this end. // it on this end.
forget(self); forget(self);
arc UniqueArc::assume_init(arc).shareable()
}; }
arc
} }
} }

View file

@ -18,7 +18,7 @@ use crate::properties::LonghandId;
use servo_arc::Arc; use servo_arc::Arc;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::ptr; use std::ptr;
use std::mem::{self, ManuallyDrop}; use std::mem;
use crate::hash::FxHashMap; use crate::hash::FxHashMap;
use super::ComputedValues; use super::ComputedValues;
use crate::values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero}; use crate::values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero};
@ -252,12 +252,12 @@ impl Clone for AnimationValue {
} }
unsafe { unsafe {
let mut out = mem::uninitialized(); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut CopyVariants, out.as_mut_ptr() as *mut CopyVariants,
*(self as *const _ as *const CopyVariants), *(self as *const _ as *const CopyVariants),
); );
return out; return out.assume_init();
} }
} }
@ -269,15 +269,15 @@ impl Clone for AnimationValue {
${props[0].camel_case}(value.clone()) ${props[0].camel_case}(value.clone())
% else: % else:
unsafe { unsafe {
let mut out = ManuallyDrop::new(mem::uninitialized()); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut AnimationValueVariantRepr<${ty}>, out.as_mut_ptr() as *mut AnimationValueVariantRepr<${ty}>,
AnimationValueVariantRepr { AnimationValueVariantRepr {
tag: *(self as *const _ as *const u16), tag: *(self as *const _ as *const u16),
value: value.clone(), value: value.clone(),
}, },
); );
ManuallyDrop::into_inner(out) out.assume_init()
} }
% endif % endif
} }
@ -356,15 +356,15 @@ impl AnimationValue {
PropertyDeclaration::${props[0].camel_case}(value) PropertyDeclaration::${props[0].camel_case}(value)
% else: % else:
unsafe { unsafe {
let mut out = mem::uninitialized(); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut PropertyDeclarationVariantRepr<${specified}>, out.as_mut_ptr() as *mut PropertyDeclarationVariantRepr<${specified}>,
PropertyDeclarationVariantRepr { PropertyDeclarationVariantRepr {
tag: *(self as *const _ as *const u16), tag: *(self as *const _ as *const u16),
value, value,
}, },
); );
out out.assume_init()
} }
% endif % endif
} }
@ -424,15 +424,15 @@ impl AnimationValue {
% endif % endif
unsafe { unsafe {
let mut out = mem::uninitialized(); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut AnimationValueVariantRepr<${ty}>, out.as_mut_ptr() as *mut AnimationValueVariantRepr<${ty}>,
AnimationValueVariantRepr { AnimationValueVariantRepr {
tag: longhand_id.to_physical(context.builder.writing_mode) as u16, tag: longhand_id.to_physical(context.builder.writing_mode) as u16,
value, value,
}, },
); );
out out.assume_init()
} }
} }
% endfor % endfor
@ -579,15 +579,15 @@ impl Animate for AnimationValue {
let value = this.animate(&other_repr.value, procedure)?; let value = this.animate(&other_repr.value, procedure)?;
% endif % endif
let mut out = mem::uninitialized(); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut AnimationValueVariantRepr<${ty}>, out.as_mut_ptr() as *mut AnimationValueVariantRepr<${ty}>,
AnimationValueVariantRepr { AnimationValueVariantRepr {
tag: this_tag, tag: this_tag,
value, value,
}, },
); );
out out.assume_init()
} }
% endfor % endfor
${" |\n".join("{}(void)".format(prop.camel_case) for prop in unanimated)} => { ${" |\n".join("{}(void)".format(prop.camel_case) for prop in unanimated)} => {

View file

@ -390,15 +390,16 @@ ${helpers.predefined_type(
% endfor % endfor
}; };
let mut system: nsFont = unsafe { mem::uninitialized() }; let mut system = mem::MaybeUninit::<nsFont>::uninit();
unsafe { let system = unsafe {
bindings::Gecko_nsFont_InitSystem( bindings::Gecko_nsFont_InitSystem(
&mut system, system.as_mut_ptr(),
id as i32, id as i32,
cx.style().get_font().gecko(), cx.style().get_font().gecko(),
cx.device().document() cx.device().document()
) );
} &mut *system.as_mut_ptr()
};
let font_weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight); let font_weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight);
let font_stretch = FontStretch(NonNegative(Percentage(unsafe { let font_stretch = FontStretch(NonNegative(Percentage(unsafe {
bindings::Gecko_FontStretch_ToFloat(system.stretch) bindings::Gecko_FontStretch_ToFloat(system.stretch)
@ -436,7 +437,7 @@ ${helpers.predefined_type(
system_font: *self, system_font: *self,
default_font_type: system.fontlist.mDefaultFontType, default_font_type: system.fontlist.mDefaultFontType,
}; };
unsafe { bindings::Gecko_nsFont_Destroy(&mut system); } unsafe { bindings::Gecko_nsFont_Destroy(system); }
ret ret
} }

View file

@ -17,7 +17,7 @@ use servo_arc::{Arc, UniqueArc};
use std::borrow::Cow; use std::borrow::Cow;
use std::{ops, ptr}; use std::{ops, ptr};
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use std::mem::{self, ManuallyDrop}; use std::mem;
use cssparser::{Parser, RGBA, TokenSerializationType}; use cssparser::{Parser, RGBA, TokenSerializationType};
use cssparser::ParserInput; use cssparser::ParserInput;
@ -294,12 +294,12 @@ impl Clone for PropertyDeclaration {
} }
unsafe { unsafe {
let mut out = mem::uninitialized(); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut CopyVariants, out.as_mut_ptr() as *mut CopyVariants,
*(self as *const _ as *const CopyVariants), *(self as *const _ as *const CopyVariants),
); );
return out; return out.assume_init();
} }
} }
@ -333,15 +333,15 @@ impl Clone for PropertyDeclaration {
% else: % else:
${" |\n".join("{}(ref value)".format(v["name"]) for v in vs)} => { ${" |\n".join("{}(ref value)".format(v["name"]) for v in vs)} => {
unsafe { unsafe {
let mut out = ManuallyDrop::new(mem::uninitialized()); let mut out = mem::MaybeUninit::uninit();
ptr::write( ptr::write(
&mut out as *mut _ as *mut PropertyDeclarationVariantRepr<${ty}>, out.as_mut_ptr() as *mut PropertyDeclarationVariantRepr<${ty}>,
PropertyDeclarationVariantRepr { PropertyDeclarationVariantRepr {
tag: *(self as *const _ as *const u16), tag: *(self as *const _ as *const u16),
value: value.clone(), value: value.clone(),
}, },
); );
ManuallyDrop::into_inner(out) out.assume_init()
} }
} }
% endif % endif

View file

@ -412,9 +412,10 @@ impl<T: ToShmem, A: Array<Item = T>> ToShmem for SmallVec<A> {
SmallVec::from_raw_parts(dest, self.len(), self.len()) SmallVec::from_raw_parts(dest, self.len(), self.len())
} else { } else {
// Place the items inline. // Place the items inline.
let mut inline: A = mem::uninitialized(); let mut s = SmallVec::new();
to_shmem_slice_ptr(self.iter(), inline.ptr_mut(), builder); to_shmem_slice_ptr(self.iter(), s.as_mut_ptr(), builder);
SmallVec::from_buf_and_len(inline, self.len()) s.set_len(self.len());
s
} }
}; };