mmap-go is a portable mmap package for the Go programming language. It has been tested on Linux (386, amd64), OS X, and Windows (386). It should also work on other Unix-like platforms, but hasn't been tested with them. I'm interested to hear about the results.
I haven't been able to add more features without adding significant complexity, so mmap-go doesn't support mprotect, mincore, and maybe a few other things. If you're running on a Unix-like platform and need some of these features, I suggest Gustavo Niemeyer's gommap.
This fork adds changes by @barakmich and maintains the BSD license and original credit to @edsrzf. It's a fantastic minimal mmap.
Package mmap allows mapping files into memory. It tries to provide a simple, reasonably portable interface, but doesn't go out of its way to abstract away every little platform detail. This specifically means:
- forked processes may or may not inherit mappings
- a file's timestamp may or may not be updated by writes through mappings
- specifying a size larger than the file's actual size can increase the file's size
- If the mapped file is being modified by another process while your program's running, don't expect consistent results between platforms
- type MMap
const ( // RDONLY maps the memory read-only. // Attempts to write to the MMap object will result in undefined behavior. RDONLY = 0 // RDWR maps the memory as read-write. Writes to the MMap object will update the // underlying file. RDWR = 1 << iota // COPY maps the memory as copy-on-write. Writes to the MMap object will affect // memory, but the underlying file will remain unchanged. COPY // If EXEC is set, the mapped memory is marked as executable. EXEC )
This section is empty.
This section is empty.
type MMap byte
MMap represents a file mapped into memory.
Map maps an entire file into memory. If ANON is set in flags, f is ignored.
MapRegion maps part of a file into memory. The offset parameter must be a multiple of the system's page size. If length < 0, the entire file will be mapped. If ANON is set in flags, f is ignored.
Flush synchronizes the mapping's contents to the file's contents on disk.
func (MMap) FlushAsync ¶
FlushAsync synchronizes the mapping's contents to the file's contents on disk, asynchronously.
Lock keeps the mapped region in physical memory, ensuring that it will not be swapped out.
Truncate runs f.Truncate(size) on the file backing the MMap, and then edits the slice using unsafe to appropriately reflect this.
Unlock reverses the effect of Lock, allowing the mapped region to potentially be swapped out. If m is already unlocked, aan error will result.
Unmap deletes the memory mapped region, flushes any remaining changes, and sets m to nil. Trying to read or write any remaining references to m after Unmap is called will result in undefined behavior. Unmap should only be called on the slice value that was originally returned from a call to Map. Calling Unmap on a derived slice may cause errors.