зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1568446 - Replace uses of mem::uninitialized with MaybeUninit. r=emilio
Cherry-picked from: https://github.com/servo/servo/pull/23824
This commit is contained in:
Родитель
ed49247220
Коммит
98ddcb49bf
|
@ -52,25 +52,6 @@ use std::sync::atomic;
|
|||
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
|
||||
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`.
|
||||
///
|
||||
/// 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> 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> {
|
||||
/// Construct an `Arc<T>`
|
||||
#[inline]
|
||||
|
@ -251,7 +240,7 @@ impl<T> Arc<T> {
|
|||
unsafe fn from_raw(ptr: *const T) -> Self {
|
||||
// To find the corresponding pointer to the `ArcInner` we need
|
||||
// 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 {
|
||||
p: ptr::NonNull::new_unchecked(ptr as *mut ArcInner<T>),
|
||||
phantom: PhantomData,
|
||||
|
|
|
@ -29,15 +29,15 @@ impl<'a> AutoProfilerLabel<'a> {
|
|||
/// stack.
|
||||
#[inline]
|
||||
pub unsafe fn new(
|
||||
label: &mut structs::AutoProfilerLabel,
|
||||
label: &mut std::mem::MaybeUninit<structs::AutoProfilerLabel>,
|
||||
label_type: ProfilerLabel,
|
||||
) -> AutoProfilerLabel {
|
||||
let category_pair = match label_type {
|
||||
ProfilerLabel::Style => structs::JS::ProfilingCategoryPair_LAYOUT_StyleComputation,
|
||||
ProfilerLabel::Parse => structs::JS::ProfilingCategoryPair_LAYOUT_CSSParsing,
|
||||
};
|
||||
structs::Gecko_Construct_AutoProfilerLabel(label, category_pair);
|
||||
AutoProfilerLabel(label)
|
||||
structs::Gecko_Construct_AutoProfilerLabel(label.as_mut_ptr(), category_pair);
|
||||
AutoProfilerLabel(&mut *label.as_mut_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -202,7 +202,8 @@ impl WeakAtom {
|
|||
where
|
||||
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 slice length (each utf16 character is going to take at least one
|
||||
|
@ -271,7 +272,8 @@ impl WeakAtom {
|
|||
}
|
||||
|
||||
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 mutable_slice = if let Some(buffer_prefix) = buffer.get_mut(..slice.len()) {
|
||||
buffer_prefix.copy_from_slice(slice);
|
||||
|
|
|
@ -112,8 +112,8 @@ macro_rules! define_keyword_type {
|
|||
#[macro_export]
|
||||
macro_rules! profiler_label {
|
||||
($label_type:ident) => {
|
||||
let mut _profiler_label: $crate::gecko_bindings::structs::AutoProfilerLabel =
|
||||
unsafe { ::std::mem::uninitialized() };
|
||||
let mut _profiler_label =
|
||||
::std::mem::MaybeUninit::<$crate::gecko_bindings::structs::AutoProfilerLabel>::uninit();
|
||||
let _profiler_label = if $crate::gecko::profiler::profiler_is_active() {
|
||||
unsafe {
|
||||
Some($crate::gecko::profiler::AutoProfilerLabel::new(
|
||||
|
|
|
@ -44,9 +44,9 @@ use crate::properties::computed_value_flags::*;
|
|||
use crate::properties::longhands;
|
||||
use crate::rule_tree::StrongRuleNode;
|
||||
use crate::selector_parser::PseudoElement;
|
||||
use servo_arc::{Arc, RawOffsetArc};
|
||||
use servo_arc::{Arc, RawOffsetArc, UniqueArc};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::{forget, uninitialized, zeroed, ManuallyDrop};
|
||||
use std::mem::{forget, zeroed, ManuallyDrop};
|
||||
use std::{cmp, ops, ptr};
|
||||
use crate::values::{self, CustomIdent, Either, KeyframesName, None_};
|
||||
use crate::values::computed::{Percentage, TransitionProperty};
|
||||
|
@ -215,19 +215,18 @@ impl ComputedValuesInner {
|
|||
Some(p) => p.pseudo_type(),
|
||||
None => structs::PseudoStyleType::NotPseudo,
|
||||
};
|
||||
let arc = unsafe {
|
||||
let arc: Arc<ComputedValues> = Arc::new(uninitialized());
|
||||
unsafe {
|
||||
let mut arc = UniqueArc::<ComputedValues>::new_uninit();
|
||||
bindings::Gecko_ComputedStyle_Init(
|
||||
&arc.0 as *const _ as *mut _,
|
||||
&mut (*arc.as_mut_ptr()).0,
|
||||
&self,
|
||||
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.
|
||||
forget(self);
|
||||
arc
|
||||
};
|
||||
arc
|
||||
UniqueArc::assume_init(arc).shareable()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ use crate::properties::LonghandId;
|
|||
use servo_arc::Arc;
|
||||
use smallvec::SmallVec;
|
||||
use std::ptr;
|
||||
use std::mem::{self, ManuallyDrop};
|
||||
use std::mem;
|
||||
use crate::hash::FxHashMap;
|
||||
use super::ComputedValues;
|
||||
use crate::values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero};
|
||||
|
@ -252,12 +252,12 @@ impl Clone for AnimationValue {
|
|||
}
|
||||
|
||||
unsafe {
|
||||
let mut out = mem::uninitialized();
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut CopyVariants,
|
||||
out.as_mut_ptr() as *mut 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())
|
||||
% else:
|
||||
unsafe {
|
||||
let mut out = ManuallyDrop::new(mem::uninitialized());
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut AnimationValueVariantRepr<${ty}>,
|
||||
out.as_mut_ptr() as *mut AnimationValueVariantRepr<${ty}>,
|
||||
AnimationValueVariantRepr {
|
||||
tag: *(self as *const _ as *const u16),
|
||||
value: value.clone(),
|
||||
},
|
||||
);
|
||||
ManuallyDrop::into_inner(out)
|
||||
out.assume_init()
|
||||
}
|
||||
% endif
|
||||
}
|
||||
|
@ -356,15 +356,15 @@ impl AnimationValue {
|
|||
PropertyDeclaration::${props[0].camel_case}(value)
|
||||
% else:
|
||||
unsafe {
|
||||
let mut out = mem::uninitialized();
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut PropertyDeclarationVariantRepr<${specified}>,
|
||||
out.as_mut_ptr() as *mut PropertyDeclarationVariantRepr<${specified}>,
|
||||
PropertyDeclarationVariantRepr {
|
||||
tag: *(self as *const _ as *const u16),
|
||||
value,
|
||||
},
|
||||
);
|
||||
out
|
||||
out.assume_init()
|
||||
}
|
||||
% endif
|
||||
}
|
||||
|
@ -424,15 +424,15 @@ impl AnimationValue {
|
|||
% endif
|
||||
|
||||
unsafe {
|
||||
let mut out = mem::uninitialized();
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut AnimationValueVariantRepr<${ty}>,
|
||||
out.as_mut_ptr() as *mut AnimationValueVariantRepr<${ty}>,
|
||||
AnimationValueVariantRepr {
|
||||
tag: longhand_id.to_physical(context.builder.writing_mode) as u16,
|
||||
value,
|
||||
},
|
||||
);
|
||||
out
|
||||
out.assume_init()
|
||||
}
|
||||
}
|
||||
% endfor
|
||||
|
@ -579,15 +579,15 @@ impl Animate for AnimationValue {
|
|||
let value = this.animate(&other_repr.value, procedure)?;
|
||||
% endif
|
||||
|
||||
let mut out = mem::uninitialized();
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut AnimationValueVariantRepr<${ty}>,
|
||||
out.as_mut_ptr() as *mut AnimationValueVariantRepr<${ty}>,
|
||||
AnimationValueVariantRepr {
|
||||
tag: this_tag,
|
||||
value,
|
||||
},
|
||||
);
|
||||
out
|
||||
out.assume_init()
|
||||
}
|
||||
% endfor
|
||||
${" |\n".join("{}(void)".format(prop.camel_case) for prop in unanimated)} => {
|
||||
|
|
|
@ -367,15 +367,16 @@ ${helpers.predefined_type(
|
|||
% endfor
|
||||
};
|
||||
|
||||
let mut system: nsFont = unsafe { mem::uninitialized() };
|
||||
unsafe {
|
||||
let mut system = mem::MaybeUninit::<nsFont>::uninit();
|
||||
let system = unsafe {
|
||||
bindings::Gecko_nsFont_InitSystem(
|
||||
&mut system,
|
||||
system.as_mut_ptr(),
|
||||
id as i32,
|
||||
cx.style().get_font().gecko(),
|
||||
cx.device().document()
|
||||
)
|
||||
}
|
||||
);
|
||||
&mut *system.as_mut_ptr()
|
||||
};
|
||||
let font_weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight);
|
||||
let font_stretch = FontStretch(NonNegative(Percentage(unsafe {
|
||||
bindings::Gecko_FontStretch_ToFloat(system.stretch)
|
||||
|
@ -413,7 +414,7 @@ ${helpers.predefined_type(
|
|||
system_font: *self,
|
||||
default_font_type: system.fontlist.mDefaultFontType,
|
||||
};
|
||||
unsafe { bindings::Gecko_nsFont_Destroy(&mut system); }
|
||||
unsafe { bindings::Gecko_nsFont_Destroy(system); }
|
||||
ret
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ use servo_arc::{Arc, UniqueArc};
|
|||
use std::borrow::Cow;
|
||||
use std::{ops, ptr};
|
||||
use std::fmt::{self, Write};
|
||||
use std::mem::{self, ManuallyDrop};
|
||||
use std::mem;
|
||||
|
||||
use cssparser::{Parser, RGBA, TokenSerializationType};
|
||||
use cssparser::ParserInput;
|
||||
|
@ -294,12 +294,12 @@ impl Clone for PropertyDeclaration {
|
|||
}
|
||||
|
||||
unsafe {
|
||||
let mut out = mem::uninitialized();
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut CopyVariants,
|
||||
out.as_mut_ptr() as *mut CopyVariants,
|
||||
*(self as *const _ as *const CopyVariants),
|
||||
);
|
||||
return out;
|
||||
return out.assume_init();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -333,15 +333,15 @@ impl Clone for PropertyDeclaration {
|
|||
% else:
|
||||
${" |\n".join("{}(ref value)".format(v["name"]) for v in vs)} => {
|
||||
unsafe {
|
||||
let mut out = ManuallyDrop::new(mem::uninitialized());
|
||||
let mut out = mem::MaybeUninit::uninit();
|
||||
ptr::write(
|
||||
&mut out as *mut _ as *mut PropertyDeclarationVariantRepr<${ty}>,
|
||||
out.as_mut_ptr() as *mut PropertyDeclarationVariantRepr<${ty}>,
|
||||
PropertyDeclarationVariantRepr {
|
||||
tag: *(self as *const _ as *const u16),
|
||||
value: value.clone(),
|
||||
},
|
||||
);
|
||||
ManuallyDrop::into_inner(out)
|
||||
out.assume_init()
|
||||
}
|
||||
}
|
||||
% endif
|
||||
|
|
|
@ -412,9 +412,10 @@ impl<T: ToShmem, A: Array<Item = T>> ToShmem for SmallVec<A> {
|
|||
SmallVec::from_raw_parts(dest, self.len(), self.len())
|
||||
} else {
|
||||
// Place the items inline.
|
||||
let mut inline: A = mem::uninitialized();
|
||||
to_shmem_slice_ptr(self.iter(), inline.ptr_mut(), builder);
|
||||
SmallVec::from_buf_and_len(inline, self.len())
|
||||
let mut s = SmallVec::new();
|
||||
to_shmem_slice_ptr(self.iter(), s.as_mut_ptr(), builder);
|
||||
s.set_len(self.len());
|
||||
s
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче