Skip to content

Commit 3f2c5d9

Browse files
committed
improve test
1 parent b920b00 commit 3f2c5d9

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

tests/run-pass/global_allocator.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
1-
use std::alloc::{GlobalAlloc, Layout, System};
1+
#![feature(allocator_api, slice_ptr_get)]
22

3-
#[global_allocator]
4-
static ALLOCATOR: Allocator = Allocator;
5-
6-
struct Allocator;
7-
8-
unsafe impl GlobalAlloc for Allocator {
9-
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
10-
// use specific size to avoid getting triggered by rt
11-
if layout.size() == 123 {
12-
println!("Allocated!")
13-
}
14-
15-
System.alloc(layout)
16-
}
3+
use std::alloc::{Allocator, Global, Layout, System};
174

18-
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
19-
if layout.size() == 123 {
20-
println!("Dellocated!")
21-
}
22-
23-
System.dealloc(ptr, layout)
24-
}
25-
}
5+
#[global_allocator]
6+
static ALLOCATOR: System = System;
267

278
fn main() {
28-
Box::new([0_u8; 123]);
9+
// Only okay because we explicitly set the system allocator as the global one!
10+
let l = Layout::from_size_align(1, 1).unwrap();
11+
let ptr = Global.allocate(l).unwrap().as_non_null_ptr(); // allocating with Global...
12+
unsafe {
13+
System.deallocate(ptr, l);
14+
} // ... and deallocating with System.
2915
}

0 commit comments

Comments
 (0)