2021-08-23 05:42:13 -05:00
|
|
|
#![feature(core_intrinsics)] // for `volatile_set_memory`
|
|
|
|
|
2017-03-13 19:33:23 -05:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Foo {
|
|
|
|
a: u64,
|
|
|
|
b: u64,
|
|
|
|
c: u64,
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:40:48 -05:00
|
|
|
fn main() {
|
|
|
|
const LENGTH: usize = 10;
|
|
|
|
let mut v: [u64; LENGTH] = [0; LENGTH];
|
|
|
|
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(v[idx], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let p = v.as_mut_ptr();
|
|
|
|
::std::ptr::write_bytes(p, 0xab, LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(v[idx], 0xabababababababab);
|
|
|
|
}
|
2017-03-13 19:33:23 -05:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
let mut w: [Foo; LENGTH] = [Foo { a: 0, b: 0, c: 0 }; LENGTH];
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(w[idx].a, 0);
|
|
|
|
assert_eq!(w[idx].b, 0);
|
|
|
|
assert_eq!(w[idx].c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let p = w.as_mut_ptr();
|
|
|
|
::std::ptr::write_bytes(p, 0xcd, LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(w[idx].a, 0xcdcdcdcdcdcdcdcd);
|
|
|
|
assert_eq!(w[idx].b, 0xcdcdcdcdcdcdcdcd);
|
|
|
|
assert_eq!(w[idx].c, 0xcdcdcdcdcdcdcdcd);
|
|
|
|
}
|
2021-08-23 05:42:13 -05:00
|
|
|
|
|
|
|
// -----
|
|
|
|
// `std::intrinsics::volatile_set_memory` should behave identically
|
|
|
|
|
|
|
|
let mut v: [u64; LENGTH] = [0; LENGTH];
|
|
|
|
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(v[idx], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let p = v.as_mut_ptr();
|
|
|
|
::std::intrinsics::volatile_set_memory(p, 0xab, LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(v[idx], 0xabababababababab);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
let mut w: [Foo; LENGTH] = [Foo { a: 0, b: 0, c: 0 }; LENGTH];
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(w[idx].a, 0);
|
|
|
|
assert_eq!(w[idx].b, 0);
|
|
|
|
assert_eq!(w[idx].c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let p = w.as_mut_ptr();
|
|
|
|
::std::intrinsics::volatile_set_memory(p, 0xcd, LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx in 0..LENGTH {
|
|
|
|
assert_eq!(w[idx].a, 0xcdcdcdcdcdcdcdcd);
|
|
|
|
assert_eq!(w[idx].b, 0xcdcdcdcdcdcdcdcd);
|
|
|
|
assert_eq!(w[idx].c, 0xcdcdcdcdcdcdcdcd);
|
|
|
|
}
|
2017-03-13 18:40:48 -05:00
|
|
|
}
|