expose mmap size and offset option (#141)

* expose mmap size and offset option

* fix panics
This commit is contained in:
evan
2022-04-14 05:08:56 +03:00
committed by GitHub
parent 95101870e4
commit 95aa76b9fa
2 changed files with 18 additions and 5 deletions

View File

@@ -1619,18 +1619,29 @@ pub fn mmapFile(
const file_flags: u32 = if (@hasDecl(std.os.MAP, "FILE")) std.os.MAP.FILE else 0;
// Conforming applications must specify either MAP_PRIVATE or MAP_SHARED.
var offset: usize = 0;
var flags = file_flags;
var map_size: ?usize = null;
if (args.nextEat()) |opts| {
const sync = opts.get(ctx.ptr(), "sync") orelse JSC.JSValue.jsBoolean(false);
const shared = opts.get(ctx.ptr(), "shared") orelse JSC.JSValue.jsBoolean(true);
flags |= @as(u32, if (sync.toBoolean()) sync_flags else 0);
flags |= @as(u32, if (shared.toBoolean()) std.os.MAP.SHARED else std.os.MAP.PRIVATE);
if (opts.get(ctx.ptr(), "size")) |value| {
map_size = @intCast(usize, value.toInt64());
}
if (opts.get(ctx.ptr(), "offset")) |value| {
offset = @intCast(usize, value.toInt64());
offset = std.mem.alignBackwardAnyAlign(offset, std.mem.page_size);
}
} else {
flags |= std.os.MAP.SHARED;
}
const map = switch (JSC.Node.Syscall.mmapFile(buf_z, flags)) {
const map = switch (JSC.Node.Syscall.mmapFile(buf_z, flags, map_size, offset)) {
.result => |map| map,
.err => |err| {