1 //@ignore-target-windows: No libc on Windows
3 #![feature(pointer_is_aligned)]
4 #![feature(strict_provenance)]
9 // A normal allocation.
11 let mut ptr: *mut libc::c_void = ptr::null_mut();
14 assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
15 assert!(!ptr.is_null());
16 assert!(ptr.is_aligned_to(align));
17 ptr.cast::<u8>().write_bytes(1, size);
23 let mut ptr: *mut libc::c_void = ptr::null_mut();
26 assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
27 assert!(!ptr.is_null());
28 assert!(ptr.is_aligned_to(align));
29 ptr.cast::<u8>().write_bytes(1, size);
33 // Size not multiple of align
35 let mut ptr: *mut libc::c_void = ptr::null_mut();
38 assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
39 assert!(!ptr.is_null());
40 assert!(ptr.is_aligned_to(align));
41 ptr.cast::<u8>().write_bytes(1, size);
47 let mut ptr: *mut libc::c_void = ptr::null_mut();
50 assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
51 // We are not required to return null if size == 0, but we currently do.
52 // It's fine to remove this assert if we start returning non-null pointers.
53 assert!(ptr.is_null());
54 assert!(ptr.is_aligned_to(align));
55 // Regardless of what we return, it must be `free`able.
59 // Non-power of 2 align
61 let mut ptr: *mut libc::c_void = ptr::invalid_mut(0x1234567);
64 assert_eq!(libc::posix_memalign(&mut ptr, align, size), libc::EINVAL);
65 // The pointer is not modified on failure, posix_memalign(3) says:
66 // > On Linux (and other systems), posix_memalign() does not modify memptr on failure.
67 // > A requirement standardizing this behavior was added in POSIX.1-2008 TC2.
68 assert_eq!(ptr.addr(), 0x1234567);
71 // Too small align (smaller than ptr)
73 let mut ptr: *mut libc::c_void = ptr::invalid_mut(0x1234567);
74 let align = std::mem::size_of::<usize>() / 2;
76 assert_eq!(libc::posix_memalign(&mut ptr, align, size), libc::EINVAL);
77 // The pointer is not modified on failure, posix_memalign(3) says:
78 // > On Linux (and other systems), posix_memalign() does not modify memptr on failure.
79 // > A requirement standardizing this behavior was added in POSIX.1-2008 TC2.
80 assert_eq!(ptr.addr(), 0x1234567);