stylo: Use AtomicUsize to store our initial computed values.

This commit is contained in:
Bobby Holley 2016-08-05 13:42:38 -07:00
parent fc4fc773b4
commit 8f991f76bd

View file

@ -36,6 +36,8 @@ use properties::CascadePropertyFn;
use properties::longhands; use properties::longhands;
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
use std::mem::{transmute, uninitialized, zeroed}; use std::mem::{transmute, uninitialized, zeroed};
use std::ptr;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::cmp; use std::cmp;
@ -102,14 +104,14 @@ impl ComputedValues {
pub fn initial_values() -> &'static Self { pub fn initial_values() -> &'static Self {
unsafe { unsafe {
debug_assert!(!INITIAL_GECKO_VALUES.is_null()); debug_assert!(!raw_initial_values().is_null());
&*INITIAL_GECKO_VALUES &*raw_initial_values()
} }
} }
pub unsafe fn initialize() { pub unsafe fn initialize() {
debug_assert!(INITIAL_GECKO_VALUES.is_null()); debug_assert!(raw_initial_values().is_null());
INITIAL_GECKO_VALUES = Box::into_raw(Box::new(ComputedValues { set_raw_initial_values(Box::into_raw(Box::new(ComputedValues {
% for style_struct in data.style_structs: % for style_struct in data.style_structs:
${style_struct.ident}: style_structs::${style_struct.name}::initial(), ${style_struct.ident}: style_structs::${style_struct.name}::initial(),
% endfor % endfor
@ -117,12 +119,13 @@ impl ComputedValues {
shareable: true, shareable: true,
writing_mode: WritingMode::empty(), writing_mode: WritingMode::empty(),
root_font_size: longhands::font_size::get_initial_value(), root_font_size: longhands::font_size::get_initial_value(),
})); })));
} }
pub unsafe fn shutdown() { pub unsafe fn shutdown() {
debug_assert!(!INITIAL_GECKO_VALUES.is_null()); debug_assert!(!raw_initial_values().is_null());
let _ = Box::from_raw(INITIAL_GECKO_VALUES); let _ = Box::from_raw(raw_initial_values());
set_raw_initial_values(ptr::null_mut());
} }
#[inline] #[inline]
@ -1362,7 +1365,16 @@ ${impl_style_struct(style_struct)}
${define_ffi_struct_accessor(style_struct)} ${define_ffi_struct_accessor(style_struct)}
% endfor % endfor
static mut INITIAL_GECKO_VALUES: *mut ComputedValues = 0 as *mut ComputedValues; // To avoid UB, we store the initial values as a atomic. It would be nice to
// store them as AtomicPtr, but we can't have static AtomicPtr without const
// fns, which aren't in stable Rust.
static INITIAL_VALUES_STORAGE: AtomicUsize = ATOMIC_USIZE_INIT;
unsafe fn raw_initial_values() -> *mut ComputedValues {
INITIAL_VALUES_STORAGE.load(Ordering::Relaxed) as *mut ComputedValues
}
unsafe fn set_raw_initial_values(v: *mut ComputedValues) {
INITIAL_VALUES_STORAGE.store(v as usize, Ordering::Relaxed);
}
static CASCADE_PROPERTY: [CascadePropertyFn; ${len(data.longhands)}] = [ static CASCADE_PROPERTY: [CascadePropertyFn; ${len(data.longhands)}] = [
% for property in data.longhands: % for property in data.longhands: