Skip to content

Commit

Permalink
Merge pull request #11 from bkmartinjr/master
Browse files Browse the repository at this point in the history
Windows fix
  • Loading branch information
ozra authored Jan 16, 2018
2 parents 481fe65 + 18c4691 commit 9d1c9ce
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,21 @@
"bugs": {
"url" : "http://github.com/ozra/mmap-io/issues"
},

"scripts": {
"prepublish": "make ls",
"prepare": "lsc -b -c -o ./ src/mmap-io.ls src/test.ls",
"install": "node-gyp configure && node-gyp rebuild",
"postinstall": "make ls",
"test": "node ./test.js"
},

"devDependencies": {
"errno": "*",
"LiveScript": "1.*.*"
},

"dependencies": {
"bindings": "1.*.*",
"LiveScript": "1.*.*",
"nan": "2.*.*"
},

"main": "mmap-io",
"engines": {
"node": ">=0.10.0"
Expand Down
11 changes: 8 additions & 3 deletions src/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ inline void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t

off_t end = length + offset;
const DWORD dwEndLow = (sizeof(off_t) > sizeof(DWORD)) ? DWORD(end & 0xFFFFFFFFL) : DWORD(end);
const DWORD dwEndHigh = (sizeof(off_t) > sizeof(DWORD)) ? DWORD(end & 0xFFFFFFFFL) : DWORD(0);
const DWORD dwEndHigh = (sizeof(off_t) > sizeof(DWORD)) ? DWORD((end >> 32) & 0xFFFFFFFFL) : DWORD(0);
const DWORD dwOffsetLow = (sizeof(off_t) > sizeof(DWORD)) ? DWORD(offset & 0xFFFFFFFFL) : DWORD(offset);
const DWORD dwOffsetHigh = (sizeof(off_t) > sizeof(DWORD)) ? DWORD(offset & 0xFFFFFFFFL) : DWORD(0);
const DWORD dwOffsetHigh = (sizeof(off_t) > sizeof(DWORD)) ? DWORD((offset >> 32) & 0xFFFFFFFFL) : DWORD(0);

HANDLE h = (fd == -1) ? HANDLE(_get_osfhandle(fd)) : INVALID_HANDLE_VALUE;
HANDLE h = (fd != -1) ? HANDLE(uv_get_osfhandle(fd)) : INVALID_HANDLE_VALUE;
HANDLE fm = CreateFileMapping(h, nullptr, protect, dwEndHigh, dwEndLow, nullptr);
if (fm == nullptr)
return MAP_FAILED;
Expand Down Expand Up @@ -94,3 +94,8 @@ inline int msync(void* addr, size_t length, int flags) {
inline int madvise(void* addr, size_t length, int advice) {
return 0; // Unsupported on Windows
}

inline int mincore(void *addr, size_t length, unsigned char *vec) {
errno = ENOSYS;
return -1;
}
7 changes: 5 additions & 2 deletions src/mmap-io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,13 @@ JS_FN(mmap_incore) {
}

int ret = mincore(data, size, resultData);

if (ret) {
free(resultData);
return Nan::ThrowError((std::string("mincore() failed, ") + std::to_string(errno)).c_str());
if (errno == ENOSYS) {
return Nan::ThrowError("mincore() not implemented");
} else {
return Nan::ThrowError((std::string("mincore() failed, ") + std::to_string(errno)).c_str());
}
}

// Now we want to check all of the pages
Expand Down
17 changes: 12 additions & 5 deletions src/test.ls
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Some lines snatched from Ben Noordhuis test-script of "node-mmap"

fs = require "fs"
os = require "os"
#mmap = require "./build/Release/mmap-io.node"
mmap = require "./mmap-io"
assert = require "assert"
Expand Down Expand Up @@ -46,13 +47,15 @@ try
for ix from (size - 1) to 0 by -1
out += String.from-char-code(buffer[ix])

# not implemented on Win32
incore_stats = mmap.incore(buffer)
assert.equal(incore_stats[0], 0)
assert.equal(incore_stats[1], 2)

#say out, "\n\n"
catch e
assert false, "Shit happened while reading from buffer"
if e.message != 'mincore() not implemented'
assert false, "Shit happened while reading from buffer"

try
say "read out of bounds test"
Expand Down Expand Up @@ -82,10 +85,14 @@ assert.equal(buffer.length, size)

# Snatched from Ben Noordhuis test-script:
# page size is almost certainly >= 4K and this script isn't that large...
fd = fs.open-sync(process.argv[1], 'r')
buffer = mmap.map(size, PROT_READ, MAP_SHARED, fd, PAGESIZE)
say "buflen test 3 = ", buffer.length
assert.equal(buffer.length, size); # ...but this is according to spec
if os.type() != 'Windows_NT'
# XXX: this will always fail on Windows, as it requires that offset be a
# multiple of the dwAllocationGranularity, which is NOT the same as the
# pagesize. In addition, the offset+length can't exceed the file size.
fd = fs.open-sync(process.argv[1], 'r')
buffer = mmap.map(size, PROT_READ, MAP_SHARED, fd, PAGESIZE)
say "buflen test 3 = ", buffer.length
assert.equal(buffer.length, size); # ...but this is according to spec

# non int param should throw exception
fd = fs.open-sync(process.argv[1], 'r')
Expand Down

0 comments on commit 9d1c9ce

Please sign in to comment.