diff --git a/components/servo_arc/lib.rs b/components/servo_arc/lib.rs index 03f502df26e..cf4578d0eb0 100644 --- a/components/servo_arc/lib.rs +++ b/components/servo_arc/lib.rs @@ -527,12 +527,16 @@ impl Arc> { // // To avoid alignment issues, we allocate words rather than bytes, // rounding up to the nearest word size. - assert!(mem::align_of::() <= mem::align_of::(), - "We don't handle over-aligned types"); - let words_to_allocate = divide_rounding_up(size, size_of::()); - let mut vec = Vec::::with_capacity(words_to_allocate); - vec.set_len(words_to_allocate); - let buffer = Box::into_raw(vec.into_boxed_slice()) as *mut usize as *mut u8; + let buffer = if mem::align_of::() <= mem::align_of::() { + Self::allocate_buffer::(size) + } else if mem::align_of::() <= mem::align_of::() { + // On 32-bit platforms may have 8 byte alignment while usize has 4 byte aligment. + // Use u64 to avoid over-alignment. + // This branch will compile away in optimized builds. + Self::allocate_buffer::(size) + } else { + panic!("Over-aligned type not handled"); + }; // Synthesize the fat pointer. We do this by claiming we have a direct // pointer to a [T], and then changing the type of the borrow. The key @@ -564,6 +568,14 @@ impl Arc> { assert_eq!(size_of::(), size_of::() * 2, "The Arc will be fat"); Arc { p: NonZeroPtrMut::new(ptr) } } + + #[inline] + unsafe fn allocate_buffer(size: usize) -> *mut u8 { + let words_to_allocate = divide_rounding_up(size, mem::size_of::()); + let mut vec = Vec::::with_capacity(words_to_allocate); + vec.set_len(words_to_allocate); + Box::into_raw(vec.into_boxed_slice()) as *mut W as *mut u8 + } } /// Header data with an inline length. Consumers that use HeaderWithLength as the