Skip to content

Commit

Permalink
Unrolled build for rust-lang#125982
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#125982 - xTachyon:fix-linked-list, r=jhpratt

Make deleting on LinkedList aware of the allocator

Fixed rust-lang#125950
  • Loading branch information
rust-timer committed Jun 5, 2024
2 parents 5ee2dfd + 2bad3d1 commit 97827ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
unsafe {
self.current = unlinked_node.as_ref().next;
self.list.unlink_node(unlinked_node);
let unlinked_node = Box::from_raw(unlinked_node.as_ptr());
let unlinked_node = Box::from_raw_in(unlinked_node.as_ptr(), &self.list.alloc);
Some(unlinked_node.element)
}
}
Expand Down Expand Up @@ -1946,7 +1946,7 @@ where
if (self.pred)(&mut node.as_mut().element) {
// `unlink_node` is okay with aliasing `element` references.
self.list.unlink_node(node);
return Some(Box::from_raw(node.as_ptr()).element);
return Some(Box::from_raw_in(node.as_ptr(), &self.list.alloc).element);
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions library/alloc/src/collections/linked_list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,3 +1164,42 @@ fn test_drop_panic() {

assert_eq!(unsafe { DROPS }, 8);
}

#[test]
fn test_allocator() {
use core::alloc::AllocError;
use core::alloc::Allocator;
use core::alloc::Layout;
use core::cell::Cell;

struct A {
has_allocated: Cell<bool>,
has_deallocated: Cell<bool>,
}

unsafe impl Allocator for A {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
assert!(!self.has_allocated.get());
self.has_allocated.set(true);

Global.allocate(layout)
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
assert!(!self.has_deallocated.get());
self.has_deallocated.set(true);

unsafe { Global.deallocate(ptr, layout) }
}
}

let alloc = &A { has_allocated: Cell::new(false), has_deallocated: Cell::new(false) };
{
let mut list = LinkedList::new_in(alloc);
list.push_back(5u32);
list.remove(0);
}

assert!(alloc.has_allocated.get());
assert!(alloc.has_deallocated.get());
}

0 comments on commit 97827ca

Please sign in to comment.