io.js/node.js mmap bindings revisited.
npm install @luminati-io/mmap-io
I needed shared memory mapping and came across @bnoordhuis module node-mmap, only to find that it didn't work with later versions of io.js, node.js and compatibles. So out of need I threw this together along with the functionality I found was missing in the node-mmap: advice and sync.
Strong temptations to re-order arguments to something more sane was kept at bay, and I kept it as mmap(2) and node-mmap for compatibility. Notable difference is the additional optional argument to pass a usage advise in the mapping stage. I've given advise and sync more practical arguments, out of a node.js perspective, compared to their C/C++ counterparts.
The flag constants have crooked names from C/C++ retained in order to make it straight forward for the user to search the net, and relate to man-pages.
This is my first node.js addon and after hours wasted reading up on V8 API I luckily stumbled upon Native Abstractions for Node. Makes life so much easier. Hot tip!
_mmap-io_ is written in C++11 and ~~LiveScript~~ — _although I love LS, it's more prudent to use TypeScript for a library, so I've rewritten that code._
It should be noted that mem-mapping is by nature potentially blocking, and _should not be used in concurrent serving/processing applications_, but rather has it's niche where multiple processes are working on the same giant sets of data (thereby sparing physical memory, and load times if the kernel does it's job for read ahead), preferably multiple readers and single or none concurrent writer, to not spoil the gains by shitloads of spin-locks, mutexes or such. _And your noble specific use case of course._
yarn in "package.json" — which may have failed builds for those not having it installed (and then not building "es-release"), and completely missing the point of getting rid of Makenpm run build) (_should only ever be needed if you clone from git and contribute_)offs_t changed to size_t because of bitwidth goofyness. Thanks to @bmarkivForceSet to DefineOwnPropertyincore fix for Mac OS. Thanks to @rustyconoverincore added. Thanks to @rustyconoverhuman_errors, ls only, build```
npm install mmap-io
``
git clone https://github.com/ozra/mmap-io.git
cd mmap-io
npm build
Note: All code in examples are in LiveScript
`livescriptFollowing code is plastic fruit; not t[ae]sted...
mmap = require "mmap-io"
fs = require "fs"
some-file = "./foo.bar"
fd = fs.open-sync some-file, "r"
fd-w = fs.open-sync some-file, "r+"
denotes optional argument denotes default value for argumentsize = fs.fstat-sync(fd).size
rx-prot = mmap.PROT_READ .|. mmap.PROT_EXECUTE
priv = mmap.MAP_SHARED
mmap.sync w-buffer
mmap.sync w-buffer, true
mmap.sync w-buffer, 0, size
mmap.sync w-buffer, 0, size, true
mmap.sync w-buffer, 0, size, true, false
$3
- Checkout man pages mmap(2), madvise(2), msync(2), mincore(2) for more detailed intell.
- The mappings are automatically unmapped when the buffer is garbage collected.
- Write-mappings need the fd to be opened with "r+", or you'll get a permission error (13).
- If you make a read-only mapping and then ignorantly set a value in the buffer, all hell previously unknown to a JS'er breaks loose (segmentation fault). It is possible to write some devilous code to intercept the SIGSEGV and throw an exception, but let's not do that!
-
Offset, and in some cases length needs to be a multiple of mmap-io.PAGESIZE (which commonly is 4096)
- Huge pages are only supported for anonymous / private mappings (well, in Linux), so I didn't throw in flags for that since I found no use.
- As Ben Noordhuis previously has stated: Supplying hint for a fixed virtual memory adress is kinda moot point in JS, so not supported.
- If you miss a feature - contribute! Or request it in an issue.
- If documentation isn't clear, make an issue.
Contribution Guidelines
- Please create a new branch like fix-{ISSUE_NUMBER}-human-readable-descr-here or feature-this-and-that for your work
- PR using that branch
- Have a look at surrounding code for style and follow that
- 4 spaces indent
- 12 spaces initial indent for function bodies in the C++ code
- Yeah it was a "feel it out" of the moment, I'll leave it there.
- _Please_, when writing an issue or making a PR: tag my handle @ozra, so that
GitHub adds a notification to the _"participating" list_ (less risk I miss it)
npm test
``