Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows fix #11

Merged
merged 2 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@
"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",
"test": "node ./test.js"
},

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

"dependencies": {
"bindings": "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