Authors: | Zahary Karadjov, Andreas Rumpf |
---|
This module provides support for memory mapped files (Posix's mmap) on the different operating systems.
Types
MemFile = object mem*: pointer ## a pointer to the memory mapped file. The pointer ## can be used directly to change the contents of the ## file, if it was opened with write access. size*: int ## size of the memory mapped file when defined(windows): fHandle: int mapHandle: int else: handle: cint
- represents a memory mapped file Source
Procs
proc mapMem(m: var MemFile; mode: FileMode = fmRead; mappedSize = - 1; offset = 0): pointer {.raises: [OSError], tags: [].}
- Source
proc unmapMem(f: var MemFile; p: pointer; size: int) {.raises: [OSError], tags: [].}
- unmaps the memory region (p, <p+size) of the mapped file f. All changes are written back to the file system, if f was opened with write access. size must be of exactly the size that was requested via mapMem. Source
proc open(filename: string; mode: FileMode = fmRead; mappedSize = - 1; offset = 0; newFileSize = - 1): MemFile {.raises: [OSError], tags: [].}
-
opens a memory mapped file. If this fails, EOS is raised. newFileSize can only be set if the file does not exist and is opened with write access (e.g., with fmReadWrite). mappedSize and offset can be used to map only a slice of the file. Example:
var mm, mm_full, mm_half: MemFile mm = memfiles.open("/tmp/test.mmap", mode = fmWrite, newFileSize = 1024) # Create a new file mm.close() # Read the whole file, would fail if newFileSize was set mm_full = memfiles.open("/tmp/test.mmap", mode = fmReadWrite, mappedSize = -1) # Read the first 512 bytes mm_half = memfiles.open("/tmp/test.mmap", mode = fmReadWrite, mappedSize = 512)
Source proc close(f: var MemFile) {.raises: [OSError], tags: [].}
- closes the memory mapped file f. All changes are written back to the file system, if f was opened with write access. Source