Rollup merge of #125982 - xTachyon:fix-linked-list, r=jhpratt · model-checking/verify-rust-std@2c5f1ac

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -1705,7 +1705,7 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {

17051705

unsafe {

17061706

self.current = unlinked_node.as_ref().next;

17071707

self.list.unlink_node(unlinked_node);

1708-

let unlinked_node = Box::from_raw(unlinked_node.as_ptr());

1708+

let unlinked_node = Box::from_raw_in(unlinked_node.as_ptr(), &self.list.alloc);

17091709

Some(unlinked_node.element)

17101710

}

17111711

}

@@ -1946,7 +1946,7 @@ where

19461946

if (self.pred)(&mut node.as_mut().element) {

19471947

// `unlink_node` is okay with aliasing `element` references.

19481948

self.list.unlink_node(node);

1949-

return Some(Box::from_raw(node.as_ptr()).element);

1949+

return Some(Box::from_raw_in(node.as_ptr(), &self.list.alloc).element);

19501950

}

19511951

}

19521952

}

Original file line numberDiff line numberDiff line change

@@ -1164,3 +1164,42 @@ fn test_drop_panic() {

11641164
11651165

assert_eq!(unsafe { DROPS }, 8);

11661166

}

1167+
1168+

#[test]

1169+

fn test_allocator() {

1170+

use core::alloc::AllocError;

1171+

use core::alloc::Allocator;

1172+

use core::alloc::Layout;

1173+

use core::cell::Cell;

1174+
1175+

struct A {

1176+

has_allocated: Cell<bool>,

1177+

has_deallocated: Cell<bool>,

1178+

}

1179+
1180+

unsafe impl Allocator for A {

1181+

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {

1182+

assert!(!self.has_allocated.get());

1183+

self.has_allocated.set(true);

1184+
1185+

Global.allocate(layout)

1186+

}

1187+
1188+

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {

1189+

assert!(!self.has_deallocated.get());

1190+

self.has_deallocated.set(true);

1191+
1192+

unsafe { Global.deallocate(ptr, layout) }

1193+

}

1194+

}

1195+
1196+

let alloc = &A { has_allocated: Cell::new(false), has_deallocated: Cell::new(false) };

1197+

{

1198+

let mut list = LinkedList::new_in(alloc);

1199+

list.push_back(5u32);

1200+

list.remove(0);

1201+

}

1202+
1203+

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

1204+

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

1205+

}