Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jdonnerstag/vlang-mmap
Provide memory mapping functionality to v-lang
https://github.com/jdonnerstag/vlang-mmap
Last synced: 3 months ago
JSON representation
Provide memory mapping functionality to v-lang
- Host: GitHub
- URL: https://github.com/jdonnerstag/vlang-mmap
- Owner: jdonnerstag
- License: mit
- Created: 2021-05-17T16:33:17.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-21T06:45:53.000Z (over 3 years ago)
- Last Synced: 2024-04-23T16:38:36.304Z (7 months ago)
- Language: V
- Size: 13.7 KB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-v - mmap - Provide native V-lang support for memory-mapping on Linux and Windows. (Libraries / Operating system)
README
# Memory Mapping support for [V-lang](https://vlang.io)
Inspired by [mman-win32](https://github.com/alitrack/mman-win32), this module
provides [V-lang](https://vlang.io) with easy to use memory mapping support.## Key Features
- Supports Linux and Windows
- Consistent public API for Linux and Windows
- V-lang like error handling## API
```v
pub fn mmap(args MmapOptions) ?voidptr
pub fn munmap(addr voidptr, len u64) ?
pub fn mprotect(addr voidptr, len u64, prot int) ?
pub fn msync(addr voidptr, len u64, flags int) ?
pub fn mlock(addr voidptr, len u64) ?
pub fn munlock(addr voidptr, len u64) ?
```Additionally
```v
// Cast the memory mapped region to a byte array
pub fn to_byte_array(addr voidptr, len u64) []byte {
```and
```v
// Open a file (binary, read-only) and map its content into memory
pub fn mmap_file(file string) ?MmapInfo {// Release the memory mapped region and close the file
pub fn (mut this MmapInfo) close() {
```## Example
```v
import mmapfn main() {
big_file_path := '../../MY_BIG_FILE.20180119104659.A901'
mut minfo := mmap.mmap_file(big_file_path)?
defer { minfo.close() }b := minfo.bytes
eprintln('src: $minfo.src')
eprintln('b.len after: $b.len')
eprintln('b starts with:')
eprintln(b[0..64].hex())defer { eprintln('finished') }
}/*
home: $ ./v run ./mmap_example.v
src: 0000000000B30000
b.len before: 0
b.len after: 2096582968
b starts with:
20202020203130303031324b64697077696d6c79676f716a6d712038205720202020202020202020202020202020202020202020202020202020202020202020
finished
*/
```