This section describes functions which offer support for file memory mapping. Once a file is mapped in memory, its elements (bytes, 16-bit words or 32-bit words) can be accessed like a normal array, thanks to virtual memory.
These functions are available only on Unix (or Unix-like) systems, such as Mac OS X.
Map a file in memory.
m = mmap(filename, n) m = mmap(filename, n, type) m = mmap(filename, n, type, perm) m = mmap(filename, n, type, perm, offset)
mmap(filename,n) maps in memory the n first bytes of file whose name is given by string filename. It returns an object which can be used to read bytes with regular array indexing, with the first byte at offset 0. The file is created if necessary.
mmap(filename,n,type) specifies the type of the elements. type is one of the strings in the table below.
| Type | Range | Description |
|---|---|---|
| 'uint8' | 0 - 255 | unsigned byte |
| 'char' | char(0) - char(255) | character |
| 'uint16' | 0 - 65535 | unsigned 16-bit word |
| 'uint32' | 0 - 4294967295 | unsigned 32-bit word |
| 'int8' | -128 - 127 | signed byte |
| 'int16' | -32768 - 32767 | signed 16-bit word |
| 'int32' | -2147483648 - 2147483647 | signed 32-bit word |
By default, multibyte words are encoded with the least significant byte first (little endian). The characters ';b' can be appended to specify that they are encoded with the most significant byte first (big endian) (for symmetry, ';l' is accepted and ignored).
mmap(filename,n,type,perm) specifies permission with string perm, which takes one of the values in the table below.
| Perm | Description |
|---|---|
| 'r' | read-only |
| 'w' | read/write |
mmap(filename,n,type,perm,offset) specified the offset of the part being memory-mapped in the file.
The following functions are overloaded to accept the type of objects returned by mmap: beginning, disp, end, length, subsasgn, and subsref.
Bytes 0-3999 of file 'test' are mapped in memory as 32-bit signed integers. They are multiplied by two.
m = mmap('test', 1000,'int32','w');
m(0:999) = 2 * m(0:999);
unmap(m);
munmap, beginning, disp, end, length, subsasgn, subsref
Unmap a memory-mapped file.
munmap(m)
munmap(m) unmaps a file which has been mapped with mmap. Its argument is the object given by mmap.