22 releases (9 stable)

3.0.2 Mar 25, 2024
3.0.0 Aug 5, 2021
2.2.0 May 28, 2020
2.1.2 Jun 20, 2019
0.0.3 Nov 30, 2016

#13 in Memory management

Download history 62470/week @ 2024-07-17 48010/week @ 2024-07-24 57041/week @ 2024-07-31 62615/week @ 2024-08-07 61696/week @ 2024-08-14 54595/week @ 2024-08-21 50510/week @ 2024-08-28 43443/week @ 2024-09-04 45137/week @ 2024-09-11 45786/week @ 2024-09-18 39618/week @ 2024-09-25 49040/week @ 2024-10-02 49148/week @ 2024-10-09 46003/week @ 2024-10-16 43826/week @ 2024-10-23 49969/week @ 2024-10-30

200,503 downloads per month
Used in 489 crates (51 directly)

MIT license

81KB
1.5K SLoC

region-rs

Cross-platform virtual memory API

GitHub CI Status crates.io version Documentation License

This crate provides a cross-platform Rust API for allocating, querying and manipulating virtual memory. It is a thin abstraction, with the underlying interaction implemented using platform specific APIs (e.g VirtualQuery, VirtualAlloc, VirtualLock, mprotect, mmap, mlock).

Platforms

This library is continuously tested against these targets:

  • Linux
    • aarch64-linux-android
    • armv7-unknown-linux-gnueabihf
    • i686-unknown-linux-gnu
    • mips-unknown-linux-gnu
    • x86_64-unknown-linux-gnu
    • x86_64-unknown-linux-musl
  • Windows
    • i686-pc-windows-gnu
    • i686-pc-windows-msvc
    • x86_64-pc-windows-gnu
    • x86_64-pc-windows-msvc
  • macOS
    • x86_64-apple-darwin
  • NetBSD
    • x86_64-unknown-netbsd
  • FreeBSD
    • x86_64-unknown-freebsd
  • OpenBSD
    • x86_64-unknown-openbsd

... and continuously checked against these targets:

  • Illumos
    • x86_64-unknown-illumos

Beyond the aformentioned target triplets, the library is also expected to work against a multitude of omitted architectures.

Installation

Add this to your Cargo.toml:

[dependencies]
region = "3.0.2"

Example

  • Cross-platform equivalents:
let data = [0xDE, 0xAD, 0xBE, 0xEF];

// Page size
let pz = region::page::size();

// VirtualQuery | '/proc/self/maps'
let q  = region::query(data.as_ptr())?;
let qr = region::query_range(data.as_ptr(), data.len())?;

// VirtualAlloc | mmap
let alloc = region::alloc(100, Protection::READ_WRITE)?;

// VirtualProtect | mprotect
region::protect(data.as_ptr(), data.len(), Protection::READ_WRITE_EXECUTE)?;

// ... you can also temporarily change one or more pages' protection
let handle = region::protect_with_handle(data.as_ptr(), data.len(), Protection::READ_WRITE_EXECUTE)?;

// VirtualLock | mlock
let guard = region::lock(data.as_ptr(), data.len())?;

Dependencies

~0–7.5MB
~60K SLoC