mmap(2) bindings for node.js - stop slurping, start mapping.
Easy as pie:
node-waf configure build install
buffer = new mmap.Buffer(n_bytes, protection, flags, fd, offset);
n_bytes | The number of bytes to map into memory. |
protection | Memory protection: either PROT_NONE or a bitwise OR of PROT_READ, PROT_WRITE and PROT_EXEC. |
flags | Flags: either MAP_SHARED or MAP_PRIVATE. |
fd | File descriptor. |
offset | File offset. Must be either zero or a multiple of mmap.PAGESIZE. |
See http://www.opengroup.org/onlinepubs/000095399/functions/mmap.html for more details.
Map a file into memory:
fs = require('fs'), mmap = require('mmap');
fd = fs.openSync('/path/to/file', 'r');
size = fs.fstatSync(fd).size;
buffer = new mmap.Buffer(size, mmap.PROT_READ, mmap.MAP_SHARED, fd, 0);
// calculate faux checksum
var checksum = 0;
for (var i = 0; i < buffer.length; i++) {
checksum ^= buffer[i];
}
The file is automatically unmapped when the buffer object is garbage collected.
- Specifying the memory address is not implemented. I couldn't think of a reason why you would want to do that from JavaScript. Convince me otherwise. :-)