Commit 57de61084e

Robin Voetter <robin@voetter.nl>
2019-08-29 00:55:22
Make mmap use SYS_mmap2 if it exists
1 parent 223b773
Changed files (2)
std
os
std/os/bits/linux/arm-eabi.zig
@@ -397,6 +397,8 @@ pub const SYS_usr32 = 0x0f0004;
 pub const SYS_set_tls = 0x0f0005;
 pub const SYS_get_tls = 0x0f0006;
 
+pub const MMAP2_UNIT = 4096;
+
 pub const O_CREAT = 0o100;
 pub const O_EXCL = 0o200;
 pub const O_NOCTTY = 0o400;
std/os/linux.zig
@@ -190,7 +190,11 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
 }
 
 pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
-    return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
+    if (@hasDecl(@This(), "SYS_mmap2")) {
+        return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, @divTrunc(offset, MMAP2_UNIT)));
+    } else {
+        return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
+    }
 }
 
 pub fn mprotect(address: [*]const u8, length: usize, protection: usize) usize {