-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use real align_offset unless we symbolic alignment check is enabled
- Loading branch information
Showing
4 changed files
with
41 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
// This manually makes sure that we have a pointer with the proper alignment. | ||
// Do this a couple times in a loop because it may work "by chance". | ||
/// This manually makes sure that we have a pointer with the proper alignment. | ||
fn manual_alignment() { | ||
let x = &mut [0u8; 3]; | ||
let base_addr = x as *mut _ as usize; | ||
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 }; | ||
let u16_ptr = base_addr_aligned as *mut u16; | ||
unsafe { *u16_ptr = 2; } | ||
} | ||
|
||
/// Test standard library `align_to`. | ||
fn align_to() { | ||
const LEN: usize = 128; | ||
let buf = &[0u8; LEN]; | ||
let (l, m, r) = unsafe { buf.align_to::<i32>() }; | ||
assert!(m.len()*4 >= LEN-4); | ||
assert!(l.len() + r.len() <= 4); | ||
} | ||
|
||
fn main() { | ||
// Do this a couple times in a loop because it may work "by chance". | ||
for _ in 0..10 { | ||
let x = &mut [0u8; 3]; | ||
let base_addr = x as *mut _ as usize; | ||
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 }; | ||
let u16_ptr = base_addr_aligned as *mut u16; | ||
unsafe { *u16_ptr = 2; } | ||
manual_alignment(); | ||
align_to(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/run-pass/align_offset.rs → tests/run-pass/align_offset_symbolic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.