From e1a199227635deddbdf010b3a79c2c96112909d7 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 13 May 2013 19:46:20 -0400 Subject: [PATCH] revert PriorityQueue to using init() uninit() would result in potentially running a destructor on arbitrary memory if the Ord implementation throws --- src/libstd/priority_queue.rs | 60 +++--------------------------------- 1 file changed, 4 insertions(+), 56 deletions(-) diff --git a/src/libstd/priority_queue.rs b/src/libstd/priority_queue.rs index c5ab1a7719c..3c96a8e145d 100644 --- a/src/libstd/priority_queue.rs +++ b/src/libstd/priority_queue.rs @@ -12,14 +12,7 @@ use core::old_iter::BaseIter; use core::util::{replace, swap}; - -#[abi = "rust-intrinsic"] -extern "rust-intrinsic" { - fn move_val_init(dst: &mut T, src: T); - fn init() -> T; - #[cfg(not(stage0))] - fn uninit() -> T; -} +use core::unstable::intrinsics::{init, move_val_init}; pub struct PriorityQueue { priv data: ~[T], @@ -141,33 +134,13 @@ pub impl PriorityQueue { // The implementations of siftup and siftdown use unsafe blocks in // order to move an element out of the vector (leaving behind a - // junk element), shift along the others and move it back into the + // zeroed element), shift along the others and move it back into the // vector over the junk element. This reduces the constant factor // compared to using swaps, which involves twice as many moves. - #[cfg(not(stage0))] priv fn siftup(&mut self, start: uint, mut pos: uint) { unsafe { - let new = *ptr::to_unsafe_ptr(&self.data[pos]); - - while pos > start { - let parent = (pos - 1) >> 1; - if new > self.data[parent] { - let x = replace(&mut self.data[parent], uninit()); - move_val_init(&mut self.data[pos], x); - pos = parent; - loop - } - break - } - move_val_init(&mut self.data[pos], new); - } - } - - #[cfg(stage0)] - priv fn siftup(&mut self, start: uint, mut pos: uint) { - unsafe { - let new = *ptr::to_unsafe_ptr(&self.data[pos]); + let new = replace(&mut self.data[pos], init()); while pos > start { let parent = (pos - 1) >> 1; @@ -183,35 +156,10 @@ pub impl PriorityQueue { } } - - #[cfg(not(stage0))] priv fn siftdown_range(&mut self, mut pos: uint, end: uint) { unsafe { let start = pos; - let new = *ptr::to_unsafe_ptr(&self.data[pos]); - - let mut child = 2 * pos + 1; - while child < end { - let right = child + 1; - if right < end && !(self.data[child] > self.data[right]) { - child = right; - } - let x = replace(&mut self.data[child], uninit()); - move_val_init(&mut self.data[pos], x); - pos = child; - child = 2 * pos + 1; - } - - move_val_init(&mut self.data[pos], new); - self.siftup(start, pos); - } - } - - #[cfg(stage0)] - priv fn siftdown_range(&mut self, mut pos: uint, end: uint) { - unsafe { - let start = pos; - let new = *ptr::to_unsafe_ptr(&self.data[pos]); + let new = replace(&mut self.data[pos], init()); let mut child = 2 * pos + 1; while child < end {