about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorC Jones <code@calebjones.net>2018-05-01 00:21:30 -0400
committerC Jones <code@calebjones.net>2018-05-07 22:14:34 -0400
commitfa62eba92ad9a3d25b200835a5cd3ca48b700d75 (patch)
tree2768bec247d65703d575f03ba7052dcbe6f9dc18 /src/liballoc
parentef6060c863c86e1422baa2cc85ae75af22feaf51 (diff)
downloadrust-fa62eba92ad9a3d25b200835a5cd3ca48b700d75.tar.gz
rust-fa62eba92ad9a3d25b200835a5cd3ca48b700d75.zip
Don't drop the shared static node
We modify the drop implementation in IntoIter to not drop the shared root
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/btree/map.rs8
-rw-r--r--src/liballoc/btree/node.rs13
2 files changed, 13 insertions, 8 deletions
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<K: Ord, V> BTreeMap<K, V> {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn insert(&mut self, key: K, value: V) -> Option<V> {
-        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<K, V> Drop for IntoIter<K, V> {
         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<K, V> LeafNode<K, V> {
             len: 0
         }
     }
+
+    fn is_shared_root(&self) -> bool {
+        self as *const _ == &EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>
+    }
 }
 
 // We need to implement Sync here in order to make a static instance
@@ -185,10 +189,7 @@ unsafe impl<K: Send, V: Send> Send for Root<K, V> { }
 
 impl<K, V> Root<K, V> {
     pub fn is_shared_root(&self) -> bool {
-        ptr::eq(
-            self.node.as_ptr().as_ptr(),
-            &EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>,
-        )
+        self.as_ref().is_shared_root()
     }
 
     pub fn shared_empty_root() -> Self {
@@ -387,6 +388,10 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
         }
     }
 
+    pub fn is_shared_root(&self) -> bool {
+        self.as_leaf().is_shared_root()
+    }
+
     pub fn keys(&self) -> &[K] {
         self.reborrow().into_slices().0
     }