]>
Commit | Line | Data |
---|---|---|
a1dfa0c6 | 1 | use std::alloc::{Global, Alloc, Layout, System}; |
ff7c6d11 | 2 | |
9fa01778 | 3 | /// Issue #45955. |
ff7c6d11 XL |
4 | #[test] |
5 | fn alloc_system_overaligned_request() { | |
6 | check_overalign_requests(System) | |
7 | } | |
8 | ||
9 | #[test] | |
10 | fn std_heap_overaligned_request() { | |
83c7162d | 11 | check_overalign_requests(Global) |
ff7c6d11 XL |
12 | } |
13 | ||
14 | fn check_overalign_requests<T: Alloc>(mut allocator: T) { | |
15 | let size = 8; | |
16 | let align = 16; // greater than size | |
17 | let iterations = 100; | |
18 | unsafe { | |
19 | let pointers: Vec<_> = (0..iterations).map(|_| { | |
20 | allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap() | |
21 | }).collect(); | |
22 | for &ptr in &pointers { | |
83c7162d XL |
23 | assert_eq!((ptr.as_ptr() as usize) % align, 0, |
24 | "Got a pointer less aligned than requested") | |
ff7c6d11 XL |
25 | } |
26 | ||
27 | // Clean up | |
28 | for &ptr in &pointers { | |
29 | allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap()) | |
30 | } | |
31 | } | |
32 | } |