]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass-dep/malloc.rs
Rollup merge of #102199 - GuillaumeGomez:improve-rustdoc-gui-tests, r=notriddle
[rust.git] / src / tools / miri / tests / pass-dep / malloc.rs
1 //@ignore-target-windows: No libc on Windows
2
3 use core::{ptr, slice};
4
5 fn main() {
6     // Test that small allocations sometimes *are* not very aligned.
7     let saw_unaligned = (0..64).any(|_| unsafe {
8         let p = libc::malloc(3);
9         libc::free(p);
10         (p as usize) % 4 != 0 // find any that this is *not* 4-aligned
11     });
12     assert!(saw_unaligned);
13
14     unsafe {
15         // Use calloc for initialized memory
16         let p1 = libc::calloc(20, 1);
17
18         // old size < new size
19         let p2 = libc::realloc(p1, 40);
20         let slice = slice::from_raw_parts(p2 as *const u8, 20);
21         assert_eq!(&slice, &[0_u8; 20]);
22
23         // old size == new size
24         let p3 = libc::realloc(p2, 40);
25         let slice = slice::from_raw_parts(p3 as *const u8, 20);
26         assert_eq!(&slice, &[0_u8; 20]);
27
28         // old size > new size
29         let p4 = libc::realloc(p3, 10);
30         let slice = slice::from_raw_parts(p4 as *const u8, 10);
31         assert_eq!(&slice, &[0_u8; 10]);
32
33         libc::free(p4);
34     }
35
36     unsafe {
37         let p1 = libc::malloc(20);
38
39         let p2 = libc::realloc(p1, 0);
40         assert!(p2.is_null());
41     }
42
43     unsafe {
44         let p1 = libc::realloc(ptr::null_mut(), 20);
45         assert!(!p1.is_null());
46
47         libc::free(p1);
48     }
49 }