]> git.lizzy.rs Git - rust.git/blob - library/alloc/tests/heap.rs
Add comment about the lack of `ExpnData` serialization for proc-macro crates
[rust.git] / library / alloc / tests / heap.rs
1 use std::alloc::{AllocInit, AllocRef, Global, Layout, System};
2
3 /// Issue #45955 and #62251.
4 #[test]
5 fn alloc_system_overaligned_request() {
6     check_overalign_requests(System)
7 }
8
9 #[test]
10 fn std_heap_overaligned_request() {
11     check_overalign_requests(Global)
12 }
13
14 fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
15     for &align in &[4, 8, 16, 32] {
16         // less than and bigger than `MIN_ALIGN`
17         for &size in &[align / 2, align - 1] {
18             // size less than alignment
19             let iterations = 128;
20             unsafe {
21                 let pointers: Vec<_> = (0..iterations)
22                     .map(|_| {
23                         allocator
24                             .alloc(
25                                 Layout::from_size_align(size, align).unwrap(),
26                                 AllocInit::Uninitialized,
27                             )
28                             .unwrap()
29                             .ptr
30                     })
31                     .collect();
32                 for &ptr in &pointers {
33                     assert_eq!(
34                         (ptr.as_ptr() as usize) % align,
35                         0,
36                         "Got a pointer less aligned than requested"
37                     )
38                 }
39
40                 // Clean up
41                 for &ptr in &pointers {
42                     allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
43                 }
44             }
45         }
46     }
47 }