Skip to content

Commit fa62eba

Browse files
committed
Don't drop the shared static node
We modify the drop implementation in IntoIter to not drop the shared root
1 parent ef6060c commit fa62eba

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/liballoc/btree/map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
686686
/// ```
687687
#[stable(feature = "rust1", since = "1.0.0")]
688688
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
689-
if self.root.is_shared_root() {
690-
self.root = node::Root::new_leaf();
691-
}
692-
693689
match self.entry(key) {
694690
Occupied(mut entry) => Some(entry.insert(value)),
695691
Vacant(entry) => {
@@ -1301,6 +1297,10 @@ impl<K, V> Drop for IntoIter<K, V> {
13011297
self.for_each(drop);
13021298
unsafe {
13031299
let leaf_node = ptr::read(&self.front).into_node();
1300+
if leaf_node.is_shared_root() {
1301+
return;
1302+
}
1303+
13041304
if let Some(first_parent) = leaf_node.deallocate_and_ascend() {
13051305
let mut cur_node = first_parent.into_node();
13061306
while let Some(parent) = cur_node.deallocate_and_ascend() {

src/liballoc/btree/node.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ impl<K, V> LeafNode<K, V> {
101101
len: 0
102102
}
103103
}
104+
105+
fn is_shared_root(&self) -> bool {
106+
self as *const _ == &EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>
107+
}
104108
}
105109

106110
// 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> { }
185189

186190
impl<K, V> Root<K, V> {
187191
pub fn is_shared_root(&self) -> bool {
188-
ptr::eq(
189-
self.node.as_ptr().as_ptr(),
190-
&EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>,
191-
)
192+
self.as_ref().is_shared_root()
192193
}
193194

194195
pub fn shared_empty_root() -> Self {
@@ -387,6 +388,10 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
387388
}
388389
}
389390

391+
pub fn is_shared_root(&self) -> bool {
392+
self.as_leaf().is_shared_root()
393+
}
394+
390395
pub fn keys(&self) -> &[K] {
391396
self.reborrow().into_slices().0
392397
}

0 commit comments

Comments
 (0)