Rollup merge of #125982 - xTachyon:fix-linked-list, r=jhpratt

Make deleting on LinkedList aware of the allocator

Fixed #125950
This commit is contained in:
Jubilee 2024-06-05 01:14:33 -07:00 committed by GitHub
commit 448159c8e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View File

@ -1705,7 +1705,7 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
unsafe { unsafe {
self.current = unlinked_node.as_ref().next; self.current = unlinked_node.as_ref().next;
self.list.unlink_node(unlinked_node); 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) Some(unlinked_node.element)
} }
} }
@ -1946,7 +1946,7 @@ where
if (self.pred)(&mut node.as_mut().element) { if (self.pred)(&mut node.as_mut().element) {
// `unlink_node` is okay with aliasing `element` references. // `unlink_node` is okay with aliasing `element` references.
self.list.unlink_node(node); 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);
} }
} }
} }

View File

@ -1164,3 +1164,42 @@ fn test_drop_panic() {
assert_eq!(unsafe { DROPS }, 8); 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());
}