region-rs
Cross-platform virtual memory API
This crate provides a cross-platform Rust API for allocating, querying and
manipulating virtual memory. It is a thin abstraction over platform APIs such as
VirtualQuery/VirtualAlloc/VirtualLock on Windows and
mprotect/mmap/mlock (and friends) on Unix-like systems.
Platforms
Continuously tested against:
- Linux (
gnu/musl) - Windows (
gnu/msvc) - macOS
- FreeBSD
- OpenBSD
- NetBSD
Also checked / supported where practical:
- Android (compile-checked; full cross tests currently blocked by toolchain linking)
- Illumos
- GNU/Hurd (compile-checked via nightly
-Zbuild-std=core,alloc; no prebuiltrust-std) - Redox (allocation/protection; region querying is not yet available)
Installation
[]
= "4.0.0"
The default feature set includes std. Disable it to use the crate as
#![no_std] + alloc:
= { = "4.0.0", = false }
Example
use Protection;
let data = ;
// Page size
let pz = size;
// VirtualQuery | '/proc/self/maps'
let q = query?;
let qr = query_range?;
// VirtualAlloc | mmap
let alloc = alloc?;
// VirtualProtect | mprotect
unsafe
// Temporarily change one or more pages' protection
let handle = unsafe ;
// VirtualLock | mlock
let guard = lock?;
Compatibility notes
- This is a major release (
4.0.0): Rust edition is2024, MSRV is1.85, and a few APIs/error types changed forno_stdfriendliness and safer locking semantics. bitflagsis now2.x. PreferProtection::from_bits_retainover the deprecatedfrom_bits_unchecked.windows-sysaccepts>=0.52, <=0.61so dependents can unify versions.