Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

mmap(2) bindings for node.js

tag: 0.0.4
README.md

node-mmap

mmap(2) bindings for node.js - stop slurping, start mapping.

Installing

Through npm:

npm install mmap

Or compile it from source with this one-liner:

node-waf configure build install

Usage

buffer = mmap.map(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 the man page for more details.

Examples

Map a file into memory:

fs = require('fs'), mmap = require('mmap');
fd = fs.openSync('/path/to/file', 'r');
size = fs.fstatSync(fd).size;
buffer = mmap.map(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.

Bugs and limitations

  • 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. :-)

  • Reading from and writing to memory-mapped files is a potentially blocking operation. Do not use this module in performance critical code. Better yet, don't use this module at all.

Something went wrong with that request. Please try again.