| author | |
| committer | |
| log | 31f1cc9a0d80e87efb1f414352ff9d6abf2067ca |
| tree | 71f721104facaeaf79d4c53b365ea855099a1b18 |
| parent | f4a05286d34d2360da5a76cf0c77697775b571eb |
Let's follow the libc/kernel convention and use an i64 for offsets, we
bitcast as needed and avoid the useless conditional casts.4 files changed, 55 insertions(+), 42 deletions(-)
lib/std/c.zig+5-5| ... | ... | @@ -89,13 +89,13 @@ pub extern "c" fn ftruncate(fd: c_int, length: off_t) c_int; |
| 89 | 89 | pub extern "c" fn raise(sig: c_int) c_int; |
| 90 | 90 | pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize; |
| 91 | 91 | pub extern "c" fn readv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint) isize; |
| 92 | pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize; | |
| 93 | pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: u64) isize; | |
| 92 | pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: off_t) isize; | |
| 93 | pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: off_t) isize; | |
| 94 | 94 | pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) isize; |
| 95 | pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: u64) isize; | |
| 95 | pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: off_t) isize; | |
| 96 | 96 | pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize; |
| 97 | pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize; | |
| 98 | pub extern "c" fn mmap(addr: ?*align(page_size) c_void, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: u64) *c_void; | |
| 97 | pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: off_t) isize; | |
| 98 | pub extern "c" fn mmap(addr: ?*align(page_size) c_void, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: off_t) *c_void; | |
| 99 | 99 | pub extern "c" fn munmap(addr: *align(page_size) c_void, len: usize) c_int; |
| 100 | 100 | pub extern "c" fn mprotect(addr: *align(page_size) c_void, len: usize, prot: c_uint) c_int; |
| 101 | 101 | pub extern "c" fn link(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: c_int) c_int; |
lib/std/c/linux.zig+6-6| ... | ... | @@ -67,14 +67,14 @@ pub extern "c" fn fstat64(fd: fd_t, buf: *libc_stat) c_int; |
| 67 | 67 | pub extern "c" fn fstatat64(dirfd: fd_t, path: [*:0]const u8, stat_buf: *libc_stat, flags: u32) c_int; |
| 68 | 68 | pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int; |
| 69 | 69 | pub extern "c" fn getrlimit64(resource: rlimit_resource, rlim: *rlimit) c_int; |
| 70 | pub extern "c" fn lseek64(fd: fd_t, offset: u64, whence: c_int) u64; | |
| 71 | pub extern "c" fn mmap64(addr: ?*align(std.mem.page_size) c_void, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: u64) *c_void; | |
| 70 | pub extern "c" fn lseek64(fd: fd_t, offset: i64, whence: c_int) i64; | |
| 71 | pub extern "c" fn mmap64(addr: ?*align(std.mem.page_size) c_void, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: i64) *c_void; | |
| 72 | 72 | pub extern "c" fn open64(path: [*:0]const u8, oflag: c_uint, ...) c_int; |
| 73 | 73 | pub extern "c" fn openat64(fd: c_int, path: [*:0]const u8, oflag: c_uint, ...) c_int; |
| 74 | pub extern "c" fn pread64(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize; | |
| 75 | pub extern "c" fn preadv64(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: u64) isize; | |
| 76 | pub extern "c" fn pwrite64(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize; | |
| 77 | pub extern "c" fn pwritev64(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: u64) isize; | |
| 74 | pub extern "c" fn pread64(fd: fd_t, buf: [*]u8, nbyte: usize, offset: i64) isize; | |
| 75 | pub extern "c" fn preadv64(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: i64) isize; | |
| 76 | pub extern "c" fn pwrite64(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: i64) isize; | |
| 77 | pub extern "c" fn pwritev64(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: i64) isize; | |
| 78 | 78 | pub extern "c" fn sendfile64(out_fd: fd_t, in_fd: fd_t, offset: ?*i64, count: usize) isize; |
| 79 | 79 | pub extern "c" fn setrlimit64(resource: rlimit_resource, rlim: *const rlimit) c_int; |
| 80 | 80 |
lib/std/os.zig+25-13| ... | ... | @@ -502,8 +502,9 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize { |
| 502 | 502 | else |
| 503 | 503 | system.pread; |
| 504 | 504 | |
| 505 | const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 505 | 506 | while (true) { |
| 506 | const rc = pread_sym(fd, buf.ptr, adjusted_len, offset); | |
| 507 | const rc = pread_sym(fd, buf.ptr, adjusted_len, ioffset); | |
| 507 | 508 | switch (errno(rc)) { |
| 508 | 509 | 0 => return @intCast(usize, rc), |
| 509 | 510 | EINTR => continue, |
| ... | ... | @@ -577,12 +578,8 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { |
| 577 | 578 | else |
| 578 | 579 | system.ftruncate; |
| 579 | 580 | |
| 580 | // XXX Pick a side and avoid this cast madness. | |
| 581 | const casted_length = if (builtin.link_libc) | |
| 582 | @bitCast(off_t, length) | |
| 583 | else | |
| 584 | length; | |
| 585 | switch (errno(ftruncate_sym(fd, casted_length))) { | |
| 581 | const ilen = @bitCast(i64, length); // the OS treats this as unsigned | |
| 582 | switch (errno(ftruncate_sym(fd, ilen))) { | |
| 586 | 583 | 0 => return, |
| 587 | 584 | EINTR => continue, |
| 588 | 585 | EFBIG => return error.FileTooBig, |
| ... | ... | @@ -912,8 +909,9 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { |
| 912 | 909 | else |
| 913 | 910 | system.pwrite; |
| 914 | 911 | |
| 912 | const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 915 | 913 | while (true) { |
| 916 | const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, offset); | |
| 914 | const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, ioffset); | |
| 917 | 915 | switch (errno(rc)) { |
| 918 | 916 | 0 => return @intCast(usize, rc), |
| 919 | 917 | EINTR => continue, |
| ... | ... | @@ -3750,7 +3748,8 @@ pub fn mmap( |
| 3750 | 3748 | else |
| 3751 | 3749 | system.mmap; |
| 3752 | 3750 | |
| 3753 | const rc = mmap_sym(ptr, length, prot, flags, fd, offset); | |
| 3751 | const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 3752 | const rc = mmap_sym(ptr, length, prot, flags, fd, ioffset); | |
| 3754 | 3753 | const err = if (builtin.link_libc) blk: { |
| 3755 | 3754 | if (rc != std.c.MAP_FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length]; |
| 3756 | 3755 | break :blk system._errno().*; |
| ... | ... | @@ -4104,8 +4103,14 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void { |
| 4104 | 4103 | else => |err| return unexpectedErrno(err), |
| 4105 | 4104 | } |
| 4106 | 4105 | } |
| 4107 | const ipos = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 4108 | switch (errno(system.lseek(fd, ipos, SEEK_SET))) { | |
| 4106 | ||
| 4107 | const lseek_sym = if (builtin.os.tag == .linux and builtin.link_libc) | |
| 4108 | system.lseek64 | |
| 4109 | else | |
| 4110 | system.lseek; | |
| 4111 | ||
| 4112 | const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 4113 | switch (errno(lseek_sym(fd, ioffset, SEEK_SET))) { | |
| 4109 | 4114 | 0 => return, |
| 4110 | 4115 | EBADF => unreachable, // always a race condition |
| 4111 | 4116 | EINVAL => return error.Unseekable, |
| ... | ... | @@ -4146,7 +4151,13 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void { |
| 4146 | 4151 | else => |err| return unexpectedErrno(err), |
| 4147 | 4152 | } |
| 4148 | 4153 | } |
| 4149 | switch (errno(system.lseek(fd, offset, SEEK_CUR))) { | |
| 4154 | const lseek_sym = if (builtin.os.tag == .linux and builtin.link_libc) | |
| 4155 | system.lseek64 | |
| 4156 | else | |
| 4157 | system.lseek; | |
| 4158 | ||
| 4159 | const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 4160 | switch (errno(lseek_sym(fd, ioffset, SEEK_CUR))) { | |
| 4150 | 4161 | 0 => return, |
| 4151 | 4162 | EBADF => unreachable, // always a race condition |
| 4152 | 4163 | EINVAL => return error.Unseekable, |
| ... | ... | @@ -4192,7 +4203,8 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void { |
| 4192 | 4203 | else |
| 4193 | 4204 | system.lseek; |
| 4194 | 4205 | |
| 4195 | switch (errno(lseek_sym(fd, offset, SEEK_END))) { | |
| 4206 | const ioffset = @bitCast(i64, offset); // the OS treats this as unsigned | |
| 4207 | switch (errno(lseek_sym(fd, ioffset, SEEK_END))) { | |
| 4196 | 4208 | 0 => return, |
| 4197 | 4209 | EBADF => unreachable, // always a race condition |
| 4198 | 4210 | EINVAL => return error.Unseekable, |
lib/std/os/linux.zig+19-18| ... | ... | @@ -59,15 +59,16 @@ const require_aligned_register_pair = |
| 59 | 59 | std.Target.current.cpu.arch.isThumb(); |
| 60 | 60 | |
| 61 | 61 | // Split a 64bit value into a {LSB,MSB} pair. |
| 62 | fn splitValue64(val: u64) [2]u32 { | |
| 62 | fn splitValue64(val: i64) [2]u32 { | |
| 63 | const u = @bitCast(u64, val); | |
| 63 | 64 | switch (builtin.endian) { |
| 64 | 65 | .Little => return [2]u32{ |
| 65 | @truncate(u32, val), | |
| 66 | @truncate(u32, val >> 32), | |
| 66 | @truncate(u32, u), | |
| 67 | @truncate(u32, u >> 32), | |
| 67 | 68 | }, |
| 68 | 69 | .Big => return [2]u32{ |
| 69 | @truncate(u32, val >> 32), | |
| 70 | @truncate(u32, val), | |
| 70 | @truncate(u32, u >> 32), | |
| 71 | @truncate(u32, u), | |
| 71 | 72 | }, |
| 72 | 73 | } |
| 73 | 74 | } |
| ... | ... | @@ -243,7 +244,7 @@ pub fn umount2(special: [*:0]const u8, flags: u32) usize { |
| 243 | 244 | return syscall2(.umount2, @ptrToInt(special), flags); |
| 244 | 245 | } |
| 245 | 246 | |
| 246 | pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: u64) usize { | |
| 247 | pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: i64) usize { | |
| 247 | 248 | if (@hasField(SYS, "mmap2")) { |
| 248 | 249 | // Make sure the offset is also specified in multiples of page size |
| 249 | 250 | if ((offset & (MMAP2_UNIT - 1)) != 0) |
| ... | ... | @@ -256,7 +257,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of |
| 256 | 257 | prot, |
| 257 | 258 | flags, |
| 258 | 259 | @bitCast(usize, @as(isize, fd)), |
| 259 | @truncate(usize, offset / MMAP2_UNIT), | |
| 260 | @truncate(usize, @bitCast(u64, offset) / MMAP2_UNIT), | |
| 260 | 261 | ); |
| 261 | 262 | } else { |
| 262 | 263 | return syscall6( |
| ... | ... | @@ -266,7 +267,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of |
| 266 | 267 | prot, |
| 267 | 268 | flags, |
| 268 | 269 | @bitCast(usize, @as(isize, fd)), |
| 269 | offset, | |
| 270 | @bitCast(u64, offset), | |
| 270 | 271 | ); |
| 271 | 272 | } |
| 272 | 273 | } |
| ... | ... | @@ -308,7 +309,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize { |
| 308 | 309 | return syscall3(.read, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count); |
| 309 | 310 | } |
| 310 | 311 | |
| 311 | pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { | |
| 312 | pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: i64) usize { | |
| 312 | 313 | const offset_halves = splitValue64(offset); |
| 313 | 314 | return syscall5( |
| 314 | 315 | .preadv, |
| ... | ... | @@ -320,7 +321,7 @@ pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { |
| 320 | 321 | ); |
| 321 | 322 | } |
| 322 | 323 | |
| 323 | pub fn preadv2(fd: i32, iov: [*]const iovec, count: usize, offset: u64, flags: kernel_rwf) usize { | |
| 324 | pub fn preadv2(fd: i32, iov: [*]const iovec, count: usize, offset: i64, flags: kernel_rwf) usize { | |
| 324 | 325 | const offset_halves = splitValue64(offset); |
| 325 | 326 | return syscall6( |
| 326 | 327 | .preadv2, |
| ... | ... | @@ -341,7 +342,7 @@ pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { |
| 341 | 342 | return syscall3(.writev, @bitCast(usize, @as(isize, fd)), @ptrToInt(iov), count); |
| 342 | 343 | } |
| 343 | 344 | |
| 344 | pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { | |
| 345 | pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: i64) usize { | |
| 345 | 346 | const offset_halves = splitValue64(offset); |
| 346 | 347 | return syscall5( |
| 347 | 348 | .pwritev, |
| ... | ... | @@ -353,7 +354,7 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) us |
| 353 | 354 | ); |
| 354 | 355 | } |
| 355 | 356 | |
| 356 | pub fn pwritev2(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64, flags: kernel_rwf) usize { | |
| 357 | pub fn pwritev2(fd: i32, iov: [*]const iovec_const, count: usize, offset: i64, flags: kernel_rwf) usize { | |
| 357 | 358 | const offset_halves = splitValue64(offset); |
| 358 | 359 | return syscall6( |
| 359 | 360 | .pwritev2, |
| ... | ... | @@ -386,7 +387,7 @@ pub fn symlinkat(existing: [*:0]const u8, newfd: i32, newpath: [*:0]const u8) us |
| 386 | 387 | return syscall3(.symlinkat, @ptrToInt(existing), @bitCast(usize, @as(isize, newfd)), @ptrToInt(newpath)); |
| 387 | 388 | } |
| 388 | 389 | |
| 389 | pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: u64) usize { | |
| 390 | pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: i64) usize { | |
| 390 | 391 | if (@hasField(SYS, "pread64") and usize_bits < 64) { |
| 391 | 392 | const offset_halves = splitValue64(offset); |
| 392 | 393 | if (require_aligned_register_pair) { |
| ... | ... | @@ -417,7 +418,7 @@ pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: u64) usize { |
| 417 | 418 | @bitCast(usize, @as(isize, fd)), |
| 418 | 419 | @ptrToInt(buf), |
| 419 | 420 | count, |
| 420 | offset, | |
| 421 | @bitCast(u64, offset), | |
| 421 | 422 | ); |
| 422 | 423 | } |
| 423 | 424 | } |
| ... | ... | @@ -452,7 +453,7 @@ pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { |
| 452 | 453 | return syscall3(.write, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count); |
| 453 | 454 | } |
| 454 | 455 | |
| 455 | pub fn ftruncate(fd: i32, length: u64) usize { | |
| 456 | pub fn ftruncate(fd: i32, length: i64) usize { | |
| 456 | 457 | if (@hasField(SYS, "ftruncate64") and usize_bits < 64) { |
| 457 | 458 | const length_halves = splitValue64(length); |
| 458 | 459 | if (require_aligned_register_pair) { |
| ... | ... | @@ -475,12 +476,12 @@ pub fn ftruncate(fd: i32, length: u64) usize { |
| 475 | 476 | return syscall2( |
| 476 | 477 | .ftruncate, |
| 477 | 478 | @bitCast(usize, @as(isize, fd)), |
| 478 | @truncate(usize, length), | |
| 479 | @bitCast(usize, length), | |
| 479 | 480 | ); |
| 480 | 481 | } |
| 481 | 482 | } |
| 482 | 483 | |
| 483 | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: u64) usize { | |
| 484 | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: i64) usize { | |
| 484 | 485 | if (@hasField(SYS, "pwrite64") and usize_bits < 64) { |
| 485 | 486 | const offset_halves = splitValue64(offset); |
| 486 | 487 | |
| ... | ... | @@ -512,7 +513,7 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: u64) usize { |
| 512 | 513 | @bitCast(usize, @as(isize, fd)), |
| 513 | 514 | @ptrToInt(buf), |
| 514 | 515 | count, |
| 515 | offset, | |
| 516 | @bitCast(u64, offset), | |
| 516 | 517 | ); |
| 517 | 518 | } |
| 518 | 519 | } |