Skip to content

Commit 4558340

Browse files
committed
avoid as_leaf_mut asserting exclusive access
1 parent 0e70c26 commit 4558340

File tree

1 file changed

+33
-30
lines changed
  • src/liballoc/collections/btree

1 file changed

+33
-30
lines changed

src/liballoc/collections/btree/node.rs

+33-30
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl<K, V> Root<K, V> {
307307
.node)
308308
};
309309
self.height -= 1;
310-
self.as_mut().as_leaf_mut().parent = ptr::null();
310+
unsafe { (*self.as_mut().as_leaf_mut()).parent = ptr::null(); }
311311

312312
unsafe {
313313
Global.dealloc(NonNull::from(top).cast(), Layout::new::<InternalNode<K, V>>());
@@ -570,9 +570,10 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
570570
}
571571
}
572572

573-
fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
574-
// We are mutable, so we cannot be the root, so this is okay.
575-
unsafe { self.node.as_mut() }
573+
/// Returns a raw ptr to avoid asserting exclusive access to the entire node.
574+
fn as_leaf_mut(&mut self) -> *mut LeafNode<K, V> {
575+
// We are mutable, so we cannot be the root, so accessing this as a leaf is okay.
576+
self.node.as_ptr()
576577
}
577578

578579
fn keys_mut(&mut self) -> &mut [K] {
@@ -659,7 +660,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
659660
} else {
660661
unsafe {
661662
slice::from_raw_parts_mut(
662-
self.as_leaf_mut().keys.as_mut_ptr() as *mut K,
663+
(*self.as_leaf_mut()).keys.as_mut_ptr() as *mut K,
663664
self.len()
664665
)
665666
}
@@ -670,7 +671,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
670671
debug_assert!(!self.is_shared_root());
671672
unsafe {
672673
slice::from_raw_parts_mut(
673-
self.as_leaf_mut().vals.as_mut_ptr() as *mut V,
674+
(*self.as_leaf_mut()).vals.as_mut_ptr() as *mut V,
674675
self.len()
675676
)
676677
}
@@ -694,9 +695,9 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
694695
unsafe {
695696
ptr::write(self.keys_mut().get_unchecked_mut(idx), key);
696697
ptr::write(self.vals_mut().get_unchecked_mut(idx), val);
697-
}
698698

699-
self.as_leaf_mut().len += 1;
699+
(*self.as_leaf_mut()).len += 1;
700+
}
700701
}
701702

702703
/// Adds a key/value pair to the beginning of the node.
@@ -708,9 +709,9 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
708709
unsafe {
709710
slice_insert(self.keys_mut(), 0, key);
710711
slice_insert(self.vals_mut(), 0, val);
711-
}
712712

713-
self.as_leaf_mut().len += 1;
713+
(*self.as_leaf_mut()).len += 1;
714+
}
714715
}
715716
}
716717

@@ -729,7 +730,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
729730
ptr::write(self.vals_mut().get_unchecked_mut(idx), val);
730731
ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), edge.node);
731732

732-
self.as_leaf_mut().len += 1;
733+
(*self.as_leaf_mut()).len += 1;
733734

734735
Handle::new_edge(self.reborrow_mut(), idx + 1).correct_parent_link();
735736
}
@@ -765,7 +766,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
765766
edge.node
766767
);
767768

768-
self.as_leaf_mut().len += 1;
769+
(*self.as_leaf_mut()).len += 1;
769770

770771
self.correct_all_childrens_parent_links();
771772
}
@@ -789,12 +790,12 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
789790
ForceResult::Internal(internal) => {
790791
let edge = ptr::read(internal.as_internal().edges.get_unchecked(idx + 1));
791792
let mut new_root = Root { node: edge, height: internal.height - 1 };
792-
new_root.as_mut().as_leaf_mut().parent = ptr::null();
793+
(*new_root.as_mut().as_leaf_mut()).parent = ptr::null();
793794
Some(new_root)
794795
}
795796
};
796797

797-
self.as_leaf_mut().len -= 1;
798+
(*self.as_leaf_mut()).len -= 1;
798799
(key, val, edge)
799800
}
800801
}
@@ -822,7 +823,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
822823
);
823824

824825
let mut new_root = Root { node: edge, height: internal.height - 1 };
825-
new_root.as_mut().as_leaf_mut().parent = ptr::null();
826+
(*new_root.as_mut().as_leaf_mut()).parent = ptr::null();
826827

827828
for i in 0..old_len {
828829
Handle::new_edge(internal.reborrow_mut(), i).correct_parent_link();
@@ -832,7 +833,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
832833
}
833834
};
834835

835-
self.as_leaf_mut().len -= 1;
836+
(*self.as_leaf_mut()).len -= 1;
836837

837838
(key, val, edge)
838839
}
@@ -1023,7 +1024,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
10231024
slice_insert(self.node.keys_mut(), self.idx, key);
10241025
slice_insert(self.node.vals_mut(), self.idx, val);
10251026

1026-
self.node.as_leaf_mut().len += 1;
1027+
(*self.node.as_leaf_mut()).len += 1;
10271028

10281029
self.node.vals_mut().get_unchecked_mut(self.idx)
10291030
}
@@ -1066,8 +1067,10 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
10661067
let idx = self.idx as u16;
10671068
let ptr = self.node.as_internal_mut() as *mut _;
10681069
let mut child = self.descend();
1069-
child.as_leaf_mut().parent = ptr;
1070-
child.as_leaf_mut().parent_idx.set(idx);
1070+
unsafe {
1071+
(*child.as_leaf_mut()).parent = ptr;
1072+
(*child.as_leaf_mut()).parent_idx.set(idx);
1073+
}
10711074
}
10721075

10731076
/// Unsafely asserts to the compiler some static information about whether the underlying
@@ -1215,7 +1218,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
12151218
new_len
12161219
);
12171220

1218-
self.node.as_leaf_mut().len = self.idx as u16;
1221+
(*self.node.as_leaf_mut()).len = self.idx as u16;
12191222
new_node.len = new_len as u16;
12201223

12211224
(
@@ -1237,7 +1240,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
12371240
unsafe {
12381241
let k = slice_remove(self.node.keys_mut(), self.idx);
12391242
let v = slice_remove(self.node.vals_mut(), self.idx);
1240-
self.node.as_leaf_mut().len -= 1;
1243+
(*self.node.as_leaf_mut()).len -= 1;
12411244
(self.left_edge(), k, v)
12421245
}
12431246
}
@@ -1278,7 +1281,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12781281
new_len + 1
12791282
);
12801283

1281-
self.node.as_leaf_mut().len = self.idx as u16;
1284+
(*self.node.as_leaf_mut()).len = self.idx as u16;
12821285
new_node.data.len = new_len as u16;
12831286

12841287
let mut new_root = Root {
@@ -1352,9 +1355,9 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
13521355
for i in self.idx+1..self.node.len() {
13531356
Handle::new_edge(self.node.reborrow_mut(), i).correct_parent_link();
13541357
}
1355-
self.node.as_leaf_mut().len -= 1;
1358+
(*self.node.as_leaf_mut()).len -= 1;
13561359

1357-
left_node.as_leaf_mut().len += right_len as u16 + 1;
1360+
(*left_node.as_leaf_mut()).len += right_len as u16 + 1;
13581361

13591362
if self.node.height > 1 {
13601363
ptr::copy_nonoverlapping(
@@ -1464,8 +1467,8 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
14641467
move_kv(left_kv, new_left_len, parent_kv, 0, 1);
14651468
}
14661469

1467-
left_node.reborrow_mut().as_leaf_mut().len -= count as u16;
1468-
right_node.reborrow_mut().as_leaf_mut().len += count as u16;
1470+
(*left_node.reborrow_mut().as_leaf_mut()).len -= count as u16;
1471+
(*right_node.reborrow_mut().as_leaf_mut()).len += count as u16;
14691472

14701473
match (left_node.force(), right_node.force()) {
14711474
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
@@ -1525,8 +1528,8 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
15251528
new_right_len);
15261529
}
15271530

1528-
left_node.reborrow_mut().as_leaf_mut().len += count as u16;
1529-
right_node.reborrow_mut().as_leaf_mut().len -= count as u16;
1531+
(*left_node.reborrow_mut().as_leaf_mut()).len += count as u16;
1532+
(*right_node.reborrow_mut().as_leaf_mut()).len -= count as u16;
15301533

15311534
match (left_node.force(), right_node.force()) {
15321535
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
@@ -1617,8 +1620,8 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
16171620

16181621
move_kv(left_kv, left_new_len, right_kv, 0, right_new_len);
16191622

1620-
left_node.reborrow_mut().as_leaf_mut().len = left_new_len as u16;
1621-
right_node.reborrow_mut().as_leaf_mut().len = right_new_len as u16;
1623+
(*left_node.reborrow_mut().as_leaf_mut()).len = left_new_len as u16;
1624+
(*right_node.reborrow_mut().as_leaf_mut()).len = right_new_len as u16;
16221625

16231626
match (left_node.force(), right_node.force()) {
16241627
(ForceResult::Internal(left), ForceResult::Internal(right)) => {

0 commit comments

Comments
 (0)