mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 11:29:02 +00:00
expose mmap size and offset option (#141)
* expose mmap size and offset option * fix panics
This commit is contained in:
@@ -486,21 +486,23 @@ fn mmap(
|
||||
return Maybe([]align(mem.page_size) u8){ .result = @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length] };
|
||||
}
|
||||
|
||||
pub fn mmapFile(path: [:0]const u8, flags: u32) Maybe([]align(mem.page_size) u8) {
|
||||
pub fn mmapFile(path: [:0]const u8, flags: u32, wanted_size: ?usize, offset: usize) Maybe([]align(mem.page_size) u8) {
|
||||
const fd = switch (open(path, os.O.RDWR, 0)) {
|
||||
.result => |fd| fd,
|
||||
.err => |err| return .{ .err = err },
|
||||
};
|
||||
|
||||
const size = switch (fstat(fd)) {
|
||||
var size = std.math.sub(usize, @intCast(usize, switch (fstat(fd)) {
|
||||
.result => |result| result.size,
|
||||
.err => |err| {
|
||||
_ = close(fd);
|
||||
return .{ .err = err };
|
||||
},
|
||||
};
|
||||
}), offset) catch 0;
|
||||
|
||||
const map = switch (mmap(null, @intCast(usize, size), os.PROT.READ | os.PROT.WRITE, flags, fd, 0)) {
|
||||
if (wanted_size) |size_| size = @minimum(size, size_);
|
||||
|
||||
const map = switch (mmap(null, size, os.PROT.READ | os.PROT.WRITE, flags, fd, offset)) {
|
||||
.result => |map| map,
|
||||
|
||||
.err => |err| {
|
||||
|
||||
Reference in New Issue
Block a user