Function region::protect_with_handle
source · [−]pub unsafe fn protect_with_handle<T>(
address: *const T,
size: usize,
protection: Protection
) -> Result<ProtectGuard>
Expand description
Temporarily changes the memory protection of one or more pages.
The address range may overlap one or more pages, and if so, all pages within
the range will be modified. The protection flag for each page will be reset
once the handle is dropped. To conditionally prevent a reset, use
std::mem::forget
.
This function uses query_range
internally and is
therefore less performant than protect
. Use this function only if you
need to reapply the memory protection flags of one or more regions after
operations.
Guard
Remember not to conflate the black hole syntax with the ignored, but
unused, variable syntax. Otherwise the ProtectGuard
instantly resets the
protection flags of all pages.
ⓘ
let _ = protect_with_handle(...); // Pages are instantly reset
let _guard = protect_with_handle(...); // Pages are reset once `_guard` is dropped.
Parameters
- The range is
[address, address + size)
- The address is rounded down to the closest page boundary.
- The size may not be zero.
- The size is rounded up to the closest page boundary, relative to the address.
Errors
- If an interaction with the underlying operating system fails, an error will be returned.
- If size is zero,
Error::InvalidParameter
will be returned.
Safety
See protect.