Skip to content

Commit 82d46b4

Browse files
committed
Auto merge of #1296 - RalfJung:rustup, r=RalfJung
rustup for AllocRef changes Cc rust-lang/rust#70362
2 parents 094fec3 + cd132f5 commit 82d46b4

File tree

8 files changed

+31
-60
lines changed

8 files changed

+31
-60
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b793f403bdfbcc0ff3e15ed8177a81d79ba4a29b
1+
127a11a344eb59b5aea1464e98257c262dcba967
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#![feature(allocator_api)]
2-
3-
extern crate alloc;
4-
5-
use alloc::alloc::Global;
6-
use std::alloc::{AllocRef, Layout};
1+
use std::alloc::{alloc, dealloc, realloc, Layout};
72

83
// error-pattern: allocation has size 1 and alignment 1, but gave size 1 and alignment 2
94

105
fn main() {
116
unsafe {
12-
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
13-
Global.dealloc(x, Layout::from_size_align_unchecked(1, 2));
7+
let x = alloc(Layout::from_size_align_unchecked(1, 1));
8+
dealloc(x, Layout::from_size_align_unchecked(1, 2));
149
}
1510
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#![feature(allocator_api)]
2-
3-
extern crate alloc;
4-
5-
use alloc::alloc::Global;
6-
use std::alloc::{AllocRef, Layout};
1+
use std::alloc::{alloc, dealloc, realloc, Layout};
72

83
// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1
94

105
fn main() {
116
unsafe {
12-
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
13-
Global.dealloc(x, Layout::from_size_align_unchecked(2, 1));
7+
let x = alloc(Layout::from_size_align_unchecked(1, 1));
8+
dealloc(x, Layout::from_size_align_unchecked(2, 1));
149
}
1510
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
#![feature(allocator_api)]
2-
3-
extern crate alloc;
4-
5-
use alloc::alloc::Global;
6-
use std::alloc::{AllocRef, Layout};
1+
use std::alloc::{alloc, dealloc, realloc, Layout};
72

83
// error-pattern: dereferenced after this allocation got freed
94

105
fn main() {
116
unsafe {
12-
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
13-
Global.dealloc(x, Layout::from_size_align_unchecked(1, 1));
14-
Global.dealloc(x, Layout::from_size_align_unchecked(1, 1));
7+
let x = alloc(Layout::from_size_align_unchecked(1, 1));
8+
dealloc(x, Layout::from_size_align_unchecked(1, 1));
9+
dealloc(x, Layout::from_size_align_unchecked(1, 1));
1510
}
1611
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#![feature(allocator_api)]
2-
3-
extern crate alloc;
4-
5-
use alloc::alloc::Global;
6-
use std::alloc::{AllocRef, Layout};
1+
use std::alloc::{alloc, dealloc, realloc, Layout};
72

83
// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1
94

105
fn main() {
116
unsafe {
12-
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
13-
Global.realloc(x, Layout::from_size_align_unchecked(2, 1), 1).unwrap();
7+
let x = alloc(Layout::from_size_align_unchecked(1, 1));
8+
realloc(x, Layout::from_size_align_unchecked(2, 1), 1);
149
}
1510
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
#![feature(allocator_api)]
2-
3-
extern crate alloc;
4-
5-
use alloc::alloc::Global;
6-
use std::alloc::{AllocRef, Layout};
1+
use std::alloc::{alloc, dealloc, realloc, Layout};
72

83
fn main() {
94
unsafe {
10-
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
11-
Global.realloc(x, Layout::from_size_align_unchecked(1, 1), 1).unwrap();
12-
let _z = *(x.as_ptr() as *mut u8); //~ ERROR dereferenced after this allocation got freed
5+
let x = alloc(Layout::from_size_align_unchecked(1, 1));
6+
realloc(x, Layout::from_size_align_unchecked(1, 1), 1);
7+
let _z = *x; //~ ERROR dereferenced after this allocation got freed
138
}
149
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
#![feature(allocator_api)]
2-
3-
extern crate alloc;
4-
5-
use alloc::alloc::Global;
6-
use std::alloc::{AllocRef, Layout};
1+
use std::alloc::{alloc, dealloc, realloc, Layout};
72

83
// error-pattern: dereferenced after this allocation got freed
94

105
fn main() {
116
unsafe {
12-
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
13-
Global.dealloc(x, Layout::from_size_align_unchecked(1, 1));
14-
Global.realloc(x, Layout::from_size_align_unchecked(1, 1), 1).unwrap();
7+
let x = alloc(Layout::from_size_align_unchecked(1, 1));
8+
dealloc(x, Layout::from_size_align_unchecked(1, 1));
9+
realloc(x, Layout::from_size_align_unchecked(1, 1), 1);
1510
}
1611
}

tests/run-pass/heap_allocator.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
#![feature(allocator_api)]
22

33
use std::ptr::NonNull;
4-
use std::alloc::{Global, AllocRef, Layout, System};
4+
use std::alloc::{Global, AllocRef, Layout, System, AllocInit, ReallocPlacement};
55
use std::slice;
66

77
fn check_alloc<T: AllocRef>(mut allocator: T) { unsafe {
88
for &align in &[4, 8, 16, 32] {
99
let layout = Layout::from_size_align(20, align).unwrap();
1010

1111
for _ in 0..32 {
12-
let a = allocator.alloc(layout).unwrap().0;
12+
let a = allocator.alloc(layout, AllocInit::Uninitialized).unwrap().ptr;
1313
assert_eq!(a.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
1414
allocator.dealloc(a, layout);
1515
}
1616

17-
let p1 = allocator.alloc_zeroed(layout).unwrap().0;
17+
let p1 = allocator.alloc(layout, AllocInit::Zeroed).unwrap().ptr;
1818
assert_eq!(p1.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
1919

20-
let p2 = allocator.realloc(p1, layout, 40).unwrap().0;
20+
// old size < new size
21+
let p2 = allocator.grow(p1, layout, 40, ReallocPlacement::MayMove, AllocInit::Uninitialized).unwrap().ptr;
2122
let layout = Layout::from_size_align(40, align).unwrap();
2223
assert_eq!(p2.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
2324
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
2425
assert_eq!(&slice, &[0_u8; 20]);
2526

2627
// old size == new size
27-
let p3 = allocator.realloc(p2, layout, 40).unwrap().0;
28+
let p3 = allocator.grow(p2, layout, 40, ReallocPlacement::MayMove, AllocInit::Uninitialized).unwrap().ptr;
2829
assert_eq!(p3.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
2930
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
3031
assert_eq!(&slice, &[0_u8; 20]);
3132

3233
// old size > new size
33-
let p4 = allocator.realloc(p3, layout, 10).unwrap().0;
34+
let p4 = allocator.shrink(p3, layout, 10, ReallocPlacement::MayMove).unwrap().ptr;
3435
let layout = Layout::from_size_align(10, align).unwrap();
3536
assert_eq!(p4.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
3637
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
@@ -46,7 +47,7 @@ fn check_align_requests<T: AllocRef>(mut allocator: T) {
4647
let iterations = 32;
4748
unsafe {
4849
let pointers: Vec<_> = (0..iterations).map(|_| {
49-
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap().0
50+
allocator.alloc(Layout::from_size_align(size, align).unwrap(), AllocInit::Uninitialized).unwrap().ptr
5051
}).collect();
5152
for &ptr in &pointers {
5253
assert_eq!((ptr.as_ptr() as usize) % align, 0,
@@ -67,7 +68,7 @@ fn global_to_box() {
6768
let l = Layout::new::<T>();
6869
// allocate manually with global allocator, then turn into Box and free there
6970
unsafe {
70-
let ptr = Global.alloc(l).unwrap().0.as_ptr() as *mut T;
71+
let ptr = Global.alloc(l, AllocInit::Uninitialized).unwrap().ptr.as_ptr() as *mut T;
7172
let b = Box::from_raw(ptr);
7273
drop(b);
7374
}

0 commit comments

Comments
 (0)