mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 02:24:33 +01:00
std.Io: make all the close functions batched
This commit is contained in:
parent
6f46570958
commit
7f5bb118d4
6 changed files with 29 additions and 23 deletions
|
|
@ -208,7 +208,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
|
|||
|
||||
const open_dir_cache = try arena.alloc(Io.Dir, write_file.directories.items.len);
|
||||
var open_dirs_count: usize = 0;
|
||||
defer closeDirs(open_dir_cache[0..open_dirs_count]);
|
||||
defer Io.Dir.closeMany(io, open_dir_cache[0..open_dirs_count]);
|
||||
|
||||
for (write_file.directories.items, open_dir_cache) |dir, *open_dir_cache_elem| {
|
||||
man.hash.addBytes(dir.sub_path);
|
||||
|
|
@ -341,9 +341,3 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
|
|||
|
||||
try step.writeManifest(&man);
|
||||
}
|
||||
|
||||
fn closeDirs(io: Io, dirs: []Io.Dir) void {
|
||||
var group: Io.Group = .init;
|
||||
defer group.wait();
|
||||
for (dirs) |d| group.async(Io.Dir.close, .{ d, io });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -671,7 +671,7 @@ pub const VTable = struct {
|
|||
dirCreateFile: *const fn (?*anyopaque, Dir, []const u8, File.CreateFlags) File.OpenError!File,
|
||||
dirOpenFile: *const fn (?*anyopaque, Dir, []const u8, File.OpenFlags) File.OpenError!File,
|
||||
dirOpenDir: *const fn (?*anyopaque, Dir, []const u8, Dir.OpenOptions) Dir.OpenError!Dir,
|
||||
dirClose: *const fn (?*anyopaque, Dir) void,
|
||||
dirClose: *const fn (?*anyopaque, []const Dir) void,
|
||||
dirRead: *const fn (?*anyopaque, *Dir.Reader, []Dir.Entry) Dir.Reader.Error!usize,
|
||||
dirRealPath: *const fn (?*anyopaque, Dir, path_name: []const u8, out_buffer: []u8) Dir.RealPathError!usize,
|
||||
dirDeleteFile: *const fn (?*anyopaque, Dir, []const u8) Dir.DeleteFileError!void,
|
||||
|
|
@ -686,7 +686,7 @@ pub const VTable = struct {
|
|||
|
||||
fileStat: *const fn (?*anyopaque, File) File.StatError!File.Stat,
|
||||
fileLength: *const fn (?*anyopaque, File) File.LengthError!u64,
|
||||
fileClose: *const fn (?*anyopaque, File) void,
|
||||
fileClose: *const fn (?*anyopaque, []const File) void,
|
||||
fileWriteStreaming: *const fn (?*anyopaque, File, header: []const u8, data: []const []const u8, splat: usize) File.Writer.Error!usize,
|
||||
fileWritePositional: *const fn (?*anyopaque, File, header: []const u8, data: []const []const u8, splat: usize, offset: u64) File.WritePositionalError!usize,
|
||||
fileWriteFileStreaming: *const fn (?*anyopaque, File, header: []const u8, *Io.File.Reader, Io.Limit) File.Writer.WriteFileError!usize,
|
||||
|
|
@ -729,7 +729,7 @@ pub const VTable = struct {
|
|||
netRead: *const fn (?*anyopaque, src: net.Socket.Handle, data: [][]u8) net.Stream.Reader.Error!usize,
|
||||
netWrite: *const fn (?*anyopaque, dest: net.Socket.Handle, header: []const u8, data: []const []const u8, splat: usize) net.Stream.Writer.Error!usize,
|
||||
netWriteFile: *const fn (?*anyopaque, net.Socket.Handle, header: []const u8, *Io.File.Reader, Io.Limit) net.Stream.Writer.WriteFileError!usize,
|
||||
netClose: *const fn (?*anyopaque, handle: net.Socket.Handle) void,
|
||||
netClose: *const fn (?*anyopaque, handle: []const net.Socket.Handle) void,
|
||||
netInterfaceNameResolve: *const fn (?*anyopaque, *const net.Interface.Name) net.Interface.Name.ResolveError!net.Interface,
|
||||
netInterfaceName: *const fn (?*anyopaque, net.Interface) net.Interface.NameError!net.Interface.Name,
|
||||
netLookup: *const fn (?*anyopaque, net.HostName, *Queue(net.HostName.LookupResult), net.HostName.LookupOptions) net.HostName.LookupError!void,
|
||||
|
|
|
|||
|
|
@ -446,7 +446,11 @@ pub fn openDirAbsolute(io: Io, absolute_path: []const u8, options: OpenOptions)
|
|||
}
|
||||
|
||||
pub fn close(dir: Dir, io: Io) void {
|
||||
return io.vtable.dirClose(io.userdata, dir);
|
||||
return io.vtable.dirClose(io.userdata, (&dir)[0..1]);
|
||||
}
|
||||
|
||||
pub fn closeMany(io: Io, dirs: []const Dir) void {
|
||||
return io.vtable.dirClose(io.userdata, dirs);
|
||||
}
|
||||
|
||||
/// Opens a file for reading or writing, without attempting to create a new file.
|
||||
|
|
|
|||
|
|
@ -252,7 +252,11 @@ pub const OpenError = error{
|
|||
} || Io.Dir.PathNameError || Io.Cancelable || Io.UnexpectedError;
|
||||
|
||||
pub fn close(file: File, io: Io) void {
|
||||
return io.vtable.fileClose(io.userdata, file);
|
||||
return io.vtable.fileClose(io.userdata, (&file)[0..1]);
|
||||
}
|
||||
|
||||
pub fn closeMany(io: Io, files: []const File) void {
|
||||
return io.vtable.fileClose(io.userdata, files);
|
||||
}
|
||||
|
||||
pub const SyncError = error{
|
||||
|
|
|
|||
|
|
@ -3243,10 +3243,10 @@ const MakeOpenDirAccessMaskWOptions = struct {
|
|||
create_disposition: u32,
|
||||
};
|
||||
|
||||
fn dirClose(userdata: ?*anyopaque, dir: Dir) void {
|
||||
fn dirClose(userdata: ?*anyopaque, dirs: []const Dir) void {
|
||||
const t: *Threaded = @ptrCast(@alignCast(userdata));
|
||||
_ = t;
|
||||
posix.close(dir.handle);
|
||||
for (dirs) |dir| posix.close(dir.handle);
|
||||
}
|
||||
|
||||
const dirRealPath = switch (native_os) {
|
||||
|
|
@ -5515,10 +5515,10 @@ fn dirOpenDirWasi(
|
|||
}
|
||||
}
|
||||
|
||||
fn fileClose(userdata: ?*anyopaque, file: File) void {
|
||||
fn fileClose(userdata: ?*anyopaque, files: []const File) void {
|
||||
const t: *Threaded = @ptrCast(@alignCast(userdata));
|
||||
_ = t;
|
||||
posix.close(file.handle);
|
||||
for (files) |file| posix.close(file.handle);
|
||||
}
|
||||
|
||||
const fileReadStreaming = switch (native_os) {
|
||||
|
|
@ -9084,18 +9084,18 @@ fn addBuf(v: []posix.iovec_const, i: *iovlen_t, bytes: []const u8) void {
|
|||
i.* += 1;
|
||||
}
|
||||
|
||||
fn netClose(userdata: ?*anyopaque, handle: net.Socket.Handle) void {
|
||||
fn netClose(userdata: ?*anyopaque, handles: []const net.Socket.Handle) void {
|
||||
const t: *Threaded = @ptrCast(@alignCast(userdata));
|
||||
_ = t;
|
||||
switch (native_os) {
|
||||
.windows => closeSocketWindows(handle),
|
||||
else => posix.close(handle),
|
||||
.windows => for (handles) |handle| closeSocketWindows(handle),
|
||||
else => for (handles) |handle| posix.close(handle),
|
||||
}
|
||||
}
|
||||
|
||||
fn netCloseUnavailable(userdata: ?*anyopaque, handle: net.Socket.Handle) void {
|
||||
fn netCloseUnavailable(userdata: ?*anyopaque, handles: []const net.Socket.Handle) void {
|
||||
_ = userdata;
|
||||
_ = handle;
|
||||
_ = handles;
|
||||
unreachable; // How you gonna close something that was impossible to open?
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1043,7 +1043,11 @@ pub const Socket = struct {
|
|||
|
||||
/// Leaves `address` in a valid state.
|
||||
pub fn close(s: *const Socket, io: Io) void {
|
||||
io.vtable.netClose(io.userdata, s.handle);
|
||||
io.vtable.netClose(io.userdata, (&s.handle)[0..1]);
|
||||
}
|
||||
|
||||
pub fn closeMany(io: Io, sockets: []const Socket) void {
|
||||
io.vtable.netClose(io.userdata, sockets);
|
||||
}
|
||||
|
||||
pub const SendError = error{
|
||||
|
|
@ -1184,7 +1188,7 @@ pub const Stream = struct {
|
|||
const max_iovecs_len = 8;
|
||||
|
||||
pub fn close(s: *const Stream, io: Io) void {
|
||||
io.vtable.netClose(io.userdata, s.socket.handle);
|
||||
io.vtable.netClose(io.userdata, (&s.socket.handle)[0..1]);
|
||||
}
|
||||
|
||||
pub const Reader = struct {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue