memfiles
nimony/lib/std/memfiles.nim
This module provides support for memory mapped files:idx: (Posix's mmap:idx:) on the different operating systems.
type MemFile = object mem: pointer size: int64 handle: int32 flags: int32
proc open(filename: string; mode: FileMode; mappedSize: int64; offset: int64; newFileSize: int64; allowRemap: bool; mapFlags: int32): MemFileopens a memory mapped file. If this fails,
OSErroris raised.newFileSizecan only be set if the file does not exist and is opened with write access (e.g., with fmReadWrite).mappedSizeandoffsetcan be used to map only a slice of the file.offsetmust be multiples of the PAGE SIZE of your OS (usually 4K or 8K but is unique to your OS)allowRemaponly needs to be true if you want to callmapMemon the resulting MemFile; else file handles are not kept open.mapFlagsallows callers to override default choices for memory mapping flags with a bitwise mask of a variety of likely platform-specific flags which may be ignored or even causeopento fail if misspecified.Example:
`nim var mm, mmfull, mmhalf: MemFilemm = 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)
`proc close(f: var MemFile)closes the memory mapped file
f. All changes are written back to the file system, iffwas opened with write access.