aboutsummaryrefslogtreecommitdiffstats
path: root/components/servo_arc/lib.rs
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2019-05-08 18:03:37 +0000
committerEmilio Cobos Álvarez <emilio@crisal.io>2019-05-10 12:42:59 +0200
commit81e706469d7138d693775e7d4c381b2e2c28f93e (patch)
tree2377639ba986e3f62cc624b778f987ef16f1ec7e /components/servo_arc/lib.rs
parent99b97737fea30228eea35214029d4e454b5d6e41 (diff)
downloadservo-81e706469d7138d693775e7d4c381b2e2c28f93e.tar.gz
servo-81e706469d7138d693775e7d4c381b2e2c28f93e.zip
style: Fix an assertion that doesn't account for alignment padding.
I'm hitting this when trying to add bindings for box shadows, which are 32-bit aligned. Differential Revision: https://phabricator.services.mozilla.com/D30353
Diffstat (limited to 'components/servo_arc/lib.rs')
-rw-r--r--components/servo_arc/lib.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/components/servo_arc/lib.rs b/components/servo_arc/lib.rs
index 3cc2b0e7fcc..40a8ff3d193 100644
--- a/components/servo_arc/lib.rs
+++ b/components/servo_arc/lib.rs
@@ -602,7 +602,7 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
F: FnOnce(Layout) -> *mut u8,
I: Iterator<Item = T> + ExactSizeIterator,
{
- use std::mem::size_of;
+ use std::mem::{align_of, size_of};
assert_ne!(size_of::<T>(), 0, "Need to think about ZST");
// Compute the required size for the allocation.
@@ -678,8 +678,9 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
);
current = current.offset(1);
}
- // We should have consumed the buffer exactly.
- debug_assert_eq!(current as *mut u8, buffer.offset(size as isize));
+ // We should have consumed the buffer exactly, maybe accounting
+ // for some padding from the alignment.
+ debug_assert!((buffer.offset(size as isize) as usize - current as *mut u8 as usize) < align_of::<Self>());
}
assert!(
items.next().is_none(),
@@ -1335,6 +1336,23 @@ mod tests {
}
#[test]
+ fn thin_assert_padding() {
+ #[derive(Clone, Default)]
+ #[repr(C)]
+ struct Padded {
+ i: u16,
+ }
+
+ // The header will have more alignment than `Padded`
+ let header = HeaderWithLength::new(0i32, 2);
+ let items = vec![Padded { i: 0xdead }, Padded { i: 0xbeef }];
+ let a = ThinArc::from_header_and_iter(header, items.into_iter());
+ assert_eq!(a.slice.len(), 2);
+ assert_eq!(a.slice[0].i, 0xdead);
+ assert_eq!(a.slice[1].i, 0xbeef);
+ }
+
+ #[test]
fn slices_and_thin() {
let mut canary = atomic::AtomicUsize::new(0);
let c = Canary(&mut canary as *mut atomic::AtomicUsize);