diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-02-07 17:27:30 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-02-07 17:27:30 -0500 |
| commit | aa043a633904b6685a201000a0ab2a62fc3bdb91 (patch) | |
| tree | 24520ed49575e88a6f6ed9592c1166efd1c074dd /std | |
| parent | 96c9a9bdb3f7b10aa1ce4833bf6adb0af1c82dc9 (diff) | |
| parent | 79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5 (diff) | |
| download | zig-aa043a633904b6685a201000a0ab2a62fc3bdb91.tar.gz zig-aa043a633904b6685a201000a0ab2a62fc3bdb91.zip | |
Merge remote-tracking branch 'origin/master' into llvm6
Diffstat (limited to 'std')
| -rw-r--r-- | std/c/linux.zig | 2 | ||||
| -rw-r--r-- | std/fmt/index.zig | 16 | ||||
| -rw-r--r-- | std/index.zig | 1 | ||||
| -rw-r--r-- | std/io.zig | 2 | ||||
| -rw-r--r-- | std/os/index.zig | 55 | ||||
| -rw-r--r-- | std/os/linux/errno.zig (renamed from std/os/linux_errno.zig) | 0 | ||||
| -rw-r--r-- | std/os/linux/i386.zig (renamed from std/os/linux_i386.zig) | 3 | ||||
| -rw-r--r-- | std/os/linux/index.zig (renamed from std/os/linux.zig) | 12 | ||||
| -rw-r--r-- | std/os/linux/test.zig (renamed from std/os/linux_test.zig) | 2 | ||||
| -rw-r--r-- | std/os/linux/x86_64.zig (renamed from std/os/linux_x86_64.zig) | 3 |
10 files changed, 67 insertions, 29 deletions
diff --git a/std/c/linux.zig b/std/c/linux.zig index 6378955795..b2ac05eba5 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -1,4 +1,4 @@ -pub use @import("../os/linux_errno.zig"); +pub use @import("../os/linux/errno.zig"); pub extern "c" fn getrandom(buf_ptr: &u8, buf_len: usize, flags: c_uint) c_int; extern "c" fn __errno_location() &c_int; diff --git a/std/fmt/index.zig b/std/fmt/index.zig index b7ae018bdc..a32a6e0295 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) { return output(context, (*value)[0..]); } else { - @compileError("Unable to format type '" ++ @typeName(T) ++ "'"); + return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)); } }, else => if (@canImplicitCast([]const u8, value)) { @@ -546,6 +546,12 @@ test "parse unsigned comptime" { } } +// Dummy field because of https://github.com/zig-lang/zig/issues/557. +// At top level because of https://github.com/zig-lang/zig/issues/675. +const Struct = struct { + unused: u8, +}; + test "fmt.format" { { var buf1: [32]u8 = undefined; @@ -577,6 +583,14 @@ test "fmt.format" { const result = try bufPrint(buf1[0..], "u3: {}\n", value); assert(mem.eql(u8, result, "u3: 5\n")); } + { + var buf1: [32]u8 = undefined; + const value = Struct { + .unused = 42, + }; + const result = try bufPrint(buf1[0..], "pointer: {}\n", &value); + assert(mem.startsWith(u8, result, "pointer: Struct@")); + } // TODO get these tests passing in release modes // https://github.com/zig-lang/zig/issues/564 diff --git a/std/index.zig b/std/index.zig index 3986063068..b5a80cba23 100644 --- a/std/index.zig +++ b/std/index.zig @@ -6,6 +6,7 @@ pub const Buffer = @import("buffer.zig").Buffer; pub const BufferOutStream = @import("buffer.zig").BufferOutStream; pub const HashMap = @import("hash_map.zig").HashMap; pub const LinkedList = @import("linked_list.zig").LinkedList; +pub const IntrusiveLinkedList = @import("linked_list.zig").IntrusiveLinkedList; pub const base64 = @import("base64.zig"); pub const build = @import("build.zig"); diff --git a/std/io.zig b/std/io.zig index 41f32622f8..2fe57e4dfe 100644 --- a/std/io.zig +++ b/std/io.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const builtin = @import("builtin"); const Os = builtin.Os; const system = switch(builtin.os) { - Os.linux => @import("os/linux.zig"), + Os.linux => @import("os/linux/index.zig"), Os.macosx, Os.ios => @import("os/darwin.zig"), Os.windows => @import("os/windows/index.zig"), else => @compileError("Unsupported OS"), diff --git a/std/os/index.zig b/std/os/index.zig index 4451faf103..a543f27be4 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -6,7 +6,7 @@ const os = this; pub const windows = @import("windows/index.zig"); pub const darwin = @import("darwin.zig"); -pub const linux = @import("linux.zig"); +pub const linux = @import("linux/index.zig"); pub const zen = @import("zen.zig"); pub const posix = switch(builtin.os) { Os.linux => linux, @@ -82,18 +82,24 @@ pub fn getRandomBytes(buf: []u8) %void { // See #397 const err = posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)); if (err > 0) { - return switch (err) { + switch (err) { posix.EINVAL => unreachable, posix.EFAULT => unreachable, posix.EINTR => continue, - else => unexpectedErrorPosix(err), - }; + posix.ENOSYS => { + const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0); + defer close(fd); + + try posixRead(fd, buf); + return; + }, + else => return unexpectedErrorPosix(err), + } } return; }, Os.macosx, Os.ios => { - const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, - 0, null); + const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0); defer close(fd); try posixRead(fd, buf); @@ -183,10 +189,15 @@ pub fn close(handle: FileHandle) void { /// Calls POSIX read, and keeps trying if it gets interrupted. pub fn posixRead(fd: i32, buf: []u8) %void { + // Linux can return EINVAL when read amount is > 0x7ffff000 + // See https://github.com/zig-lang/zig/pull/743#issuecomment-363158274 + const max_buf_len = 0x7ffff000; + var index: usize = 0; while (index < buf.len) { - const amt_written = posix.read(fd, &buf[index], buf.len - index); - const err = posix.getErrno(amt_written); + const want_to_read = math.min(buf.len - index, usize(max_buf_len)); + const rc = posix.read(fd, &buf[index], want_to_read); + const err = posix.getErrno(rc); if (err > 0) { return switch (err) { posix.EINTR => continue, @@ -199,7 +210,7 @@ pub fn posixRead(fd: i32, buf: []u8) %void { else => unexpectedErrorPosix(err), }; } - index += amt_written; + index += rc; } } @@ -214,9 +225,15 @@ error BrokenPipe; /// Calls POSIX write, and keeps trying if it gets interrupted. pub fn posixWrite(fd: i32, bytes: []const u8) %void { - while (true) { - const write_ret = posix.write(fd, bytes.ptr, bytes.len); - const write_err = posix.getErrno(write_ret); + // Linux can return EINVAL when write amount is > 0x7ffff000 + // See https://github.com/zig-lang/zig/pull/743#issuecomment-363165856 + const max_bytes_len = 0x7ffff000; + + var index: usize = 0; + while (index < bytes.len) { + const amt_to_write = math.min(bytes.len - index, usize(max_bytes_len)); + const rc = posix.write(fd, &bytes[index], amt_to_write); + const write_err = posix.getErrno(rc); if (write_err > 0) { return switch (write_err) { posix.EINTR => continue, @@ -233,7 +250,7 @@ pub fn posixWrite(fd: i32, bytes: []const u8) %void { else => unexpectedErrorPosix(write_err), }; } - return; + index += rc; } } @@ -262,8 +279,12 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al mem.copy(u8, path0, file_path); path0[file_path.len] = 0; + return posixOpenC(path0.ptr, flags, perm); +} + +pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) %i32 { while (true) { - const result = posix.open(path0.ptr, flags, perm); + const result = posix.open(file_path, flags, perm); const err = posix.getErrno(result); if (err > 0) { return switch (err) { @@ -1495,10 +1516,10 @@ test "std.os" { _ = @import("darwin_errno.zig"); _ = @import("darwin.zig"); _ = @import("get_user_id.zig"); - _ = @import("linux_errno.zig"); + _ = @import("linux/errno.zig"); //_ = @import("linux_i386.zig"); - _ = @import("linux_x86_64.zig"); - _ = @import("linux.zig"); + _ = @import("linux/x86_64.zig"); + _ = @import("linux/index.zig"); _ = @import("path.zig"); _ = @import("windows/index.zig"); } diff --git a/std/os/linux_errno.zig b/std/os/linux/errno.zig index 39f4e37a10..39f4e37a10 100644 --- a/std/os/linux_errno.zig +++ b/std/os/linux/errno.zig diff --git a/std/os/linux_i386.zig b/std/os/linux/i386.zig index 353461562b..691377e24e 100644 --- a/std/os/linux_i386.zig +++ b/std/os/linux/i386.zig @@ -1,4 +1,5 @@ -const linux = @import("linux.zig"); +const std = @import("../../index.zig"); +const linux = std.os.linux; const socklen_t = linux.socklen_t; const iovec = linux.iovec; diff --git a/std/os/linux.zig b/std/os/linux/index.zig index 04cdafb0ac..43d175b74d 100644 --- a/std/os/linux.zig +++ b/std/os/linux/index.zig @@ -1,12 +1,12 @@ -const std = @import("../index.zig"); +const std = @import("../../index.zig"); const assert = std.debug.assert; const builtin = @import("builtin"); const arch = switch (builtin.arch) { - builtin.Arch.x86_64 => @import("linux_x86_64.zig"), - builtin.Arch.i386 => @import("linux_i386.zig"), + builtin.Arch.x86_64 => @import("x86_64.zig"), + builtin.Arch.i386 => @import("i386.zig"), else => @compileError("unsupported arch"), }; -pub use @import("linux_errno.zig"); +pub use @import("errno.zig"); pub const PATH_MAX = 4096; @@ -787,10 +787,10 @@ pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_va return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)); } -test "import linux_test" { +test "import linux test" { // TODO lazy analysis should prevent this test from being compiled on windows, but // it is still compiled on windows if (builtin.os == builtin.Os.linux) { - _ = @import("linux_test.zig"); + _ = @import("test.zig"); } } diff --git a/std/os/linux_test.zig b/std/os/linux/test.zig index 265d0a17f9..c7dbeab67f 100644 --- a/std/os/linux_test.zig +++ b/std/os/linux/test.zig @@ -1,4 +1,4 @@ -const std = @import("std"); +const std = @import("../../index.zig"); const linux = std.os.linux; const assert = std.debug.assert; diff --git a/std/os/linux_x86_64.zig b/std/os/linux/x86_64.zig index 3706633745..3a76ca4f87 100644 --- a/std/os/linux_x86_64.zig +++ b/std/os/linux/x86_64.zig @@ -1,4 +1,5 @@ -const linux = @import("linux.zig"); +const std = @import("../../index.zig"); +const linux = std.os.linux; const socklen_t = linux.socklen_t; const iovec = linux.iovec; |
