Crates.io | lightweight-mmap |
lib.rs | lightweight-mmap |
version | |
source | src |
created_at | 2024-11-02 20:27:13.889133 |
updated_at | 2024-12-04 16:38:52.729631 |
description | Simple memory mapping helpers for Rust, with minimal amount of code generated. |
homepage | |
repository | https://github.com/Sewer56/lightweight-mmap |
max_upload_size | |
id | 1433156 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Simple memory mapping helpers for Rust, with minimal amount of code generated.
This crate provides the facilities for opening a file and mapping it to memory with the minimal amount of code generated.
This crate is for absolute freaks like me who wish to save 3-8KB of code size in their binaries;
with a thin limited wrapper providing complete zero overhead abstraction. Since std
and 3rd party
mmap crates will compile and run a small amount of code which may not be needed for your use case.
If you have more advanced needs than present in this crate, consider using std
for opening
file handles and a library like memmap2-rs and mmap-rs for mapping.
The API surface here is driven by sewer56-archives-nx and any other projects of mine which need mmap in a tiny package.
Send
but not Sync
)std
(default): Enables standard library supportmmap
(default): Enables memory map operations, adding mmap
cache info to handles on some platforms. Without this, library can only be used for opening raw file handles.no-format
: Reduces binary size by skipping core::fmt
formatting machinery as much as possible. Uses itoa and nanokit crates for minimal formatting.trim-file-lengths
: Ensures memory maps cannot exceed file size by trimming mapping length. Adds a small overhead to map open time.To use without standard library:
[dependencies]
lightweight-mmap = { version = "x.y.z", default-features = false }
To minimize binary size:
[dependencies]
lightweight-mmap = { version = "x.y.z", features = ["no-format"] }
Whether you should use no-format
should depend on whether you already use core::fmt
elsewhere in your
library/binary. Check with cargo bloat. If you don't, it's best to use no-format
to reduce binary size.
The crate is tested and supported on:
For other platforms, level of support is unknown.
Open a read-only file handle to an existing file:
let handle = ReadOnlyFileHandle::open("assets/test_file.txt").unwrap();
Open a read-write file handle to an existing file:
let handle = ReadWriteFileHandle::open("assets/test_file.txt").unwrap();
Create a new file with pre-allocated size:
let handle = ReadWriteFileHandle::create_preallocated("assets/test_file.txt", 1024).unwrap();
This will create a new file or overwrite an existing file.
Create a read-only memory mapping:
use lightweight_mmap::{ReadOnlyFileHandle, ReadOnlyMmap};
// Open the file
let handle = ReadOnlyFileHandle::open("data.bin").unwrap();
// Map 1024 bytes starting at offset 4096
let mapping = ReadOnlyMmap::new(&handle, 4096, 1024).unwrap();
// Access the mapped memory
let data = mapping.as_slice();
Create a read-write memory mapping:
use lightweight_mmap::{ReadWriteFileHandle, ReadWriteMmap};
// Open the file
let handle = ReadWriteFileHandle::open("data.bin").unwrap();
// Map 1024 bytes starting at offset 4096
let mapping = ReadWriteMmap::new(&handle, 4096, 1024).unwrap();
// Access and modify the mapped memory
unsafe {
let data = core::slice::from_raw_parts_mut(mapping.data(), mapping.len());
data[0] = 42;
}
Note: Memory mappings cannot outlive their file handles (compiler should ensure this), and the mapped memory should be accessed carefully to avoid data races.
Provide hints to the operating system about how memory mapped regions will be accessed:
use lightweight_mmap::{ReadOnlyFileHandle, ReadOnlyMmap, MemoryAdvice};
// Open and map the file
let handle = ReadOnlyFileHandle::open("data.bin").unwrap();
let mapping = ReadOnlyMmap::new(&handle, 0, 1024).unwrap();
// Indicate we'll access this memory soon
mapping.advise(MemoryAdvice::WILL_NEED);
// Indicate sequential access pattern
mapping.advise(MemoryAdvice::SEQUENTIAL);
// Combine multiple hints
mapping.advise(MemoryAdvice::WILL_NEED | MemoryAdvice::SEQUENTIAL);
Available advice flags:
WILL_NEED
: Indicates that the application expects to access the memory soonSEQUENTIAL
: Indicates that memory access will be sequential from lower to higher addressesRANDOM
: Indicates that memory access will be random (non-sequential)Note: These are hints and may be ignored by the operating system.
Not all hints are supported on all platforms. On Windows, only WILL_NEED
has an effect.
For information on how to work with this codebase, see README-DEV.MD.
Licensed under MIT.
Learn more about Reloaded's general choice of licensing for projects..