From fa62eba92ad9a3d25b200835a5cd3ca48b700d75 Mon Sep 17 00:00:00 2001 From: C Jones Date: Tue, 1 May 2018 00:21:30 -0400 Subject: Don't drop the shared static node We modify the drop implementation in IntoIter to not drop the shared root --- src/liballoc/btree/map.rs | 8 ++++---- src/liballoc/btree/node.rs | 13 +++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'src/liballoc/btree') diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index 985a41722d7..a88631b1e67 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -686,10 +686,6 @@ impl BTreeMap { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn insert(&mut self, key: K, value: V) -> Option { - if self.root.is_shared_root() { - self.root = node::Root::new_leaf(); - } - match self.entry(key) { Occupied(mut entry) => Some(entry.insert(value)), Vacant(entry) => { @@ -1301,6 +1297,10 @@ impl Drop for IntoIter { self.for_each(drop); unsafe { let leaf_node = ptr::read(&self.front).into_node(); + if leaf_node.is_shared_root() { + return; + } + if let Some(first_parent) = leaf_node.deallocate_and_ascend() { let mut cur_node = first_parent.into_node(); while let Some(parent) = cur_node.deallocate_and_ascend() { diff --git a/src/liballoc/btree/node.rs b/src/liballoc/btree/node.rs index 6381006e7fc..79615e11c67 100644 --- a/src/liballoc/btree/node.rs +++ b/src/liballoc/btree/node.rs @@ -101,6 +101,10 @@ impl LeafNode { len: 0 } } + + fn is_shared_root(&self) -> bool { + self as *const _ == &EMPTY_ROOT_NODE as *const _ as *const LeafNode + } } // We need to implement Sync here in order to make a static instance @@ -185,10 +189,7 @@ unsafe impl Send for Root { } impl Root { pub fn is_shared_root(&self) -> bool { - ptr::eq( - self.node.as_ptr().as_ptr(), - &EMPTY_ROOT_NODE as *const _ as *const LeafNode, - ) + self.as_ref().is_shared_root() } pub fn shared_empty_root() -> Self { @@ -387,6 +388,10 @@ impl NodeRef { } } + pub fn is_shared_root(&self) -> bool { + self.as_leaf().is_shared_root() + } + pub fn keys(&self) -> &[K] { self.reborrow().into_slices().0 } -- cgit 1.4.1-3-g733a5