authorgravatar for jo.sh@tutanota.comJosh Megnauth <jo.sh@tutanota.com> 2026-03-13 19:37:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-14 22:04:43+01:00
logaaba36ff763a7f016bba1afc4eff758302538362
tree8cd7dfb6338f477fbceb3a1fce122268c9db6af3
parent50cc3b91a5b325b645b0c5c81f3401600b15c92a

libzigc: posix_{fadvise, fallocate}, fallocate

This commit removes `posix_fadvise`, `posix_fallocate`, and `fallocate` from the bundled musl by forwarding to Zig's wrappers. `musl`'s implemenation does not use `__syscall_cp`, so I assume trivially replacing these functions is okay. For `posix_fadvise`, `musl` notes that the arguments are reordered for ARM and other architectures. Zig's wrapper already handles this case. Contributes toward: #30978

6 files changed, 29 insertions(+), 36 deletions(-)

lib/c.zig+1
......@@ -63,6 +63,7 @@ pub fn errno(syscall_return_value: usize) c_int {
6363
6464comptime {
6565 _ = @import("c/ctype.zig");
66 _ = @import("c/fcntl.zig");
6667 _ = @import("c/inttypes.zig");
6768 if (!builtin.target.isMinGW()) {
6869 _ = @import("c/malloc.zig");
lib/c/fcntl.zig created+28
......@@ -0,0 +1,28 @@
1const builtin = @import("builtin");
2
3const std = @import("std");
4const linux = std.os.linux;
5const off_t = linux.off_t;
6
7const symbol = @import("../c.zig").symbol;
8const errno = @import("../c.zig").errno;
9
10comptime {
11 if (builtin.target.isMuslLibC()) {
12 symbol(&fallocateLinux, "fallocate");
13 symbol(&posix_fadviseLinux, "posix_fadvise");
14 symbol(&posix_fallocateLinux, "posix_fallocate");
15 }
16}
17
18fn fallocateLinux(fd: c_int, mode: c_int, offset: off_t, len: off_t) callconv(.c) c_int {
19 return errno(linux.fallocate(fd, mode, offset, len));
20}
21
22fn posix_fadviseLinux(fd: c_int, offset: off_t, len: off_t, advice: c_int) callconv(.c) c_int {
23 return errno(linux.fadvise(fd, offset, len, @intCast(advice)));
24}
25
26fn posix_fallocateLinux(fd: c_int, offset: off_t, len: off_t) callconv(.c) c_int {
27 return errno(linux.fallocate(fd, 0, offset, len));
28}
lib/libc/musl/src/fcntl/posix_fadvise.c deleted-16
......@@ -1,16 +0,0 @@
1#include <fcntl.h>
2#include "syscall.h"
3
4int posix_fadvise(int fd, off_t base, off_t len, int advice)
5{
6#if defined(SYSCALL_FADVISE_6_ARG)
7 /* Some archs, at least arm and powerpc, have the syscall
8 * arguments reordered to avoid needing 7 argument registers
9 * due to 64-bit argument alignment. */
10 return -__syscall(SYS_fadvise, fd, advice,
11 __SYSCALL_LL_E(base), __SYSCALL_LL_E(len));
12#else
13 return -__syscall(SYS_fadvise, fd, __SYSCALL_LL_O(base),
14 __SYSCALL_LL_E(len), advice);
15#endif
16}
lib/libc/musl/src/fcntl/posix_fallocate.c deleted-8
......@@ -1,8 +0,0 @@
1#include <fcntl.h>
2#include "syscall.h"
3
4int posix_fallocate(int fd, off_t base, off_t len)
5{
6 return -__syscall(SYS_fallocate, fd, 0, __SYSCALL_LL_E(base),
7 __SYSCALL_LL_E(len));
8}
lib/libc/musl/src/linux/fallocate.c deleted-9
......@@ -1,9 +0,0 @@
1#define _GNU_SOURCE
2#include <fcntl.h>
3#include "syscall.h"
4
5int fallocate(int fd, int mode, off_t base, off_t len)
6{
7 return syscall(SYS_fallocate, fd, mode, __SYSCALL_LL_E(base),
8 __SYSCALL_LL_E(len));
9}
src/libs/musl.zig-3
......@@ -589,8 +589,6 @@ const src_files = [_][]const u8{
589589 "musl/src/fcntl/fcntl.c",
590590 "musl/src/fcntl/openat.c",
591591 "musl/src/fcntl/open.c",
592 "musl/src/fcntl/posix_fadvise.c",
593 "musl/src/fcntl/posix_fallocate.c",
594592 "musl/src/fenv/aarch64/fenv.s",
595593 "musl/src/fenv/arm/fenv.c",
596594 "musl/src/fenv/arm/fenv-hf.S",
......@@ -708,7 +706,6 @@ const src_files = [_][]const u8{
708706 "musl/src/linux/copy_file_range.c",
709707 "musl/src/linux/epoll.c",
710708 "musl/src/linux/eventfd.c",
711 "musl/src/linux/fallocate.c",
712709 "musl/src/linux/fanotify.c",
713710 "musl/src/linux/getdents.c",
714711 "musl/src/linux/getrandom.c",