Skip to content

Commit 7ae4ee8

Browse files
committed
Implement read_volatile and write_volatile
1 parent 8e2a577 commit 7ae4ee8

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/libcore/ptr.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,54 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
161161
intrinsics::move_val_init(&mut *dst, src)
162162
}
163163

164+
/// Performs a volatile read of the value from `src` without moving it. This
165+
/// leaves the memory in `src` unchanged.
166+
///
167+
/// Volatile operations are intended to act on I/O memory, and are guaranteed
168+
/// to not be elided or reordered by the compiler across other volatile
169+
/// operations. See the LLVM documentation on [[volatile]].
170+
///
171+
/// [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
172+
///
173+
/// # Safety
174+
///
175+
/// Beyond accepting a raw pointer, this is unsafe because it semantically
176+
/// moves the value out of `src` without preventing further usage of `src`.
177+
/// If `T` is not `Copy`, then care must be taken to ensure that the value at
178+
/// `src` is not used before the data is overwritten again (e.g. with `write`,
179+
/// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
180+
/// because it will attempt to drop the value previously at `*src`.
181+
#[inline]
182+
#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
183+
pub unsafe fn read_volatile<T>(src: *const T) -> T {
184+
intrinsics::volatile_load(src)
185+
}
186+
187+
/// Performs a volatile write of a memory location with the given value without
188+
/// reading or dropping the old value.
189+
///
190+
/// Volatile operations are intended to act on I/O memory, and are guaranteed
191+
/// to not be elided or reordered by the compiler across other volatile
192+
/// operations. See the LLVM documentation on [[volatile]].
193+
///
194+
/// [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
195+
///
196+
/// # Safety
197+
///
198+
/// This operation is marked unsafe because it accepts a raw pointer.
199+
///
200+
/// It does not drop the contents of `dst`. This is safe, but it could leak
201+
/// allocations or resources, so care must be taken not to overwrite an object
202+
/// that should be dropped.
203+
///
204+
/// This is appropriate for initializing uninitialized memory, or overwriting
205+
/// memory that has previously been `read` from.
206+
#[inline]
207+
#[unstable(feature = "volatile", reason = "recently added", issue = "31756")]
208+
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
209+
intrinsics::volatile_store(dst, src);
210+
}
211+
164212
#[lang = "const_ptr"]
165213
impl<T: ?Sized> *const T {
166214
/// Returns true if the pointer is null.

0 commit comments

Comments
 (0)