+
+ ////////////////////////////////////////////////////////////////////////////////
+ // Undefined bytes
+ ////////////////////////////////////////////////////////////////////////////////
+
+ fn mark_definedness(&mut self, ptr: Pointer, size: usize, new_state: bool) -> EvalResult<()> {
+ let mut alloc = try!(self.get_mut(ptr.alloc_id));
+ alloc.mark_definedness(ptr.offset, ptr.offset + size, new_state);
+ Ok(())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Undefined byte tracking
+////////////////////////////////////////////////////////////////////////////////
+
+impl Allocation {
+ /// Mark the range `start..end` (end-exclusive) as defined or undefined, depending on
+ /// `new_state`.
+ fn mark_definedness(&mut self, start: usize, end: usize, new_state: bool) {
+ // There is no need to track undef masks for zero-sized allocations.
+ let len = self.bytes.len();
+ if len == 0 {
+ return;
+ }
+
+ // Returns whether the new state matches the state of a given undef mask index. The way
+ // undef masks are represented, boundaries at even indices are undefined and those at odd
+ // indices are defined.
+ let index_matches_new_state = |i| i % 2 == new_state as usize;
+
+ // Lookup the undef mask index where the given endpoint `i` is or should be inserted.
+ let lookup_endpoint = |undef_mask: &[usize], i: usize| -> (usize, bool) {
+ let (index, should_insert);
+ match undef_mask.binary_search(&i) {
+ // Region endpoint is on an undef mask boundary.
+ Ok(j) => {
+ // This endpoint's index must be incremented if the boundary's state matches
+ // the region's new state so that the boundary is:
+ // 1. Excluded from deletion when handling the inclusive left-hand endpoint.
+ // 2. Included for deletion when handling the exclusive right-hand endpoint.
+ index = j + index_matches_new_state(j) as usize;
+
+ // Don't insert a new mask boundary; simply reuse or delete the matched one.
+ should_insert = false;
+ }
+
+ // Region endpoint is not on a mask boundary.
+ Err(j) => {
+ // This is the index after the nearest mask boundary which has the same state.
+ index = j;
+
+ // Insert a new boundary if this endpoint's state doesn't match the state of
+ // this position.
+ should_insert = index_matches_new_state(j);
+ }
+ }
+ (index, should_insert)
+ };
+
+ match self.undef_mask {
+ // There is an existing undef mask, with arbitrary existing boundaries.
+ Some(ref mut undef_mask) => {
+ // Determine where the new range's endpoints fall within the current undef mask.
+ let (start_index, insert_start) = lookup_endpoint(undef_mask, start);
+ let (end_index, insert_end) = lookup_endpoint(undef_mask, end);
+
+ // Delete all the undef mask boundaries overwritten by the new range.
+ undef_mask.drain(start_index..end_index);
+
+ // Insert any new boundaries deemed necessary with two exceptions:
+ // 1. Never insert an endpoint equal to the allocation length; it's implicit.
+ // 2. Never insert a start boundary equal to the end boundary.
+ if insert_end && end != len {
+ undef_mask.insert(start_index, end);
+ }
+ if insert_start && start != end {
+ undef_mask.insert(start_index, start);
+ }
+ }
+
+ // There is no existing undef mask. This is taken as meaning the entire allocation is
+ // currently undefined. If the new state is false, meaning undefined, do nothing.
+ None => if new_state {
+ let mut mask = if start == 0 {
+ // 0..end is defined.
+ Vec::new()
+ } else {
+ // 0..0 is defined, 0..start is undefined, start..end is defined.
+ vec![0, start]
+ };
+
+ // Don't insert the end boundary if it's equal to the allocation length; that
+ // boundary is implicit.
+ if end != len {
+ mask.push(end);
+ }
+ self.undef_mask = Some(mask);
+ },
+ }
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use memory::Allocation;
+ use std::collections::BTreeMap;
+
+ fn alloc_with_mask(len: usize, undef_mask: Option<Vec<usize>>) -> Allocation {
+ Allocation {
+ bytes: vec![0; len].into_boxed_slice(),
+ relocations: BTreeMap::new(),
+ undef_mask: undef_mask,
+ }
+ }
+
+ #[test]
+ fn large_undef_mask() {
+ let mut alloc = alloc_with_mask(20, Some(vec![4, 8, 12, 16]));
+
+ alloc.mark_definedness(8, 11, false);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 11, 12, 16]));
+
+ alloc.mark_definedness(8, 11, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
+
+ alloc.mark_definedness(8, 12, false);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 16]));
+
+ alloc.mark_definedness(8, 12, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
+
+ alloc.mark_definedness(9, 11, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
+
+ alloc.mark_definedness(9, 11, false);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 8, 9, 11, 12, 16]));
+
+ alloc.mark_definedness(9, 10, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 8, 10, 11, 12, 16]));
+
+ alloc.mark_definedness(8, 12, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
+ }
+
+ #[test]
+ fn empty_undef_mask() {
+ let mut alloc = alloc_with_mask(0, None);
+
+ alloc.mark_definedness(0, 0, false);
+ assert_eq!(alloc.undef_mask, None);
+
+ alloc.mark_definedness(0, 0, true);
+ assert_eq!(alloc.undef_mask, None);
+ }
+
+ #[test]
+ fn small_undef_mask() {
+ let mut alloc = alloc_with_mask(8, None);
+
+ alloc.mark_definedness(0, 4, false);
+ assert_eq!(alloc.undef_mask, None);
+
+ alloc.mark_definedness(0, 4, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4]));
+
+ alloc.mark_definedness(4, 8, false);
+ assert_eq!(alloc.undef_mask, Some(vec![4]));
+
+ alloc.mark_definedness(4, 8, true);
+ assert_eq!(alloc.undef_mask, Some(vec![]));
+
+ alloc.mark_definedness(0, 8, true);
+ assert_eq!(alloc.undef_mask, Some(vec![]));
+
+ alloc.mark_definedness(0, 8, false);
+ assert_eq!(alloc.undef_mask, Some(vec![0]));
+
+ alloc.mark_definedness(0, 8, true);
+ assert_eq!(alloc.undef_mask, Some(vec![]));
+
+ alloc.mark_definedness(4, 8, false);
+ assert_eq!(alloc.undef_mask, Some(vec![4]));
+
+ alloc.mark_definedness(0, 8, false);
+ assert_eq!(alloc.undef_mask, Some(vec![0]));
+
+ alloc.mark_definedness(2, 5, true);
+ assert_eq!(alloc.undef_mask, Some(vec![0, 2, 5]));
+
+ alloc.mark_definedness(4, 6, false);
+ assert_eq!(alloc.undef_mask, Some(vec![0, 2, 4]));
+
+ alloc.mark_definedness(0, 3, true);
+ assert_eq!(alloc.undef_mask, Some(vec![4]));
+
+ alloc.mark_definedness(2, 6, true);
+ assert_eq!(alloc.undef_mask, Some(vec![6]));
+
+ alloc.mark_definedness(3, 7, false);
+ assert_eq!(alloc.undef_mask, Some(vec![3]));
+ }