authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-29 09:02:21+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-29 14:17:49+02:00
log547e520359b983d301670b8a3b37df67d4ab1a53
tree3f12605a67f53e4eb9d4afbf75ed46fabe78bad2
parentb3b00ec62f518875a486b4da532f74e304c3aba2

Split DeviceIoControl path into two fn call paths

As discussed in the previous commit, it would be better to avoid function pointers to syscalls and explicitly split the control path into two function calls instead. This commit addresses that for `std.os.windows.DeviceIoControl`.

1 files changed, 36 insertions(+), 13 deletions(-)

lib/std/os/windows.zig+36-13
......@@ -228,20 +228,43 @@ pub fn DeviceIoControl(
228228 out: ?[]u8,
229229) DeviceIoControlError!void {
230230 // Logic from: https://doxygen.reactos.org/d3/d74/deviceio_8c.html
231 const syscall = if ((ioControlCode >> 16) == FILE_DEVICE_FILE_SYSTEM) ntdll.NtFsControlFile else ntdll.NtDeviceIoControlFile;
231 const is_fsctl = (ioControlCode >> 16) == FILE_DEVICE_FILE_SYSTEM;
232
232233 var io: IO_STATUS_BLOCK = undefined;
233 const rc = syscall(
234 h,
235 null,
236 null,
237 null,
238 &io,
239 ioControlCode,
240 if (in) |i| i.ptr else null,
241 if (in) |i| @intCast(ULONG, i.len) else 0,
242 if (out) |o| o.ptr else null,
243 if (out) |o| @intCast(ULONG, o.len) else 0,
244 );
234 const in_ptr = if (in) |i| i.ptr else null;
235 const in_len = if (in) |i| @intCast(ULONG, i.len) else 0;
236 const out_ptr = if (out) |o| o.ptr else null;
237 const out_len = if (out) |o| @intCast(ULONG, o.len) else 0;
238
239 const rc = blk: {
240 if (is_fsctl) {
241 break :blk ntdll.NtFsControlFile(
242 h,
243 null,
244 null,
245 null,
246 &io,
247 ioControlCode,
248 in_ptr,
249 in_len,
250 out_ptr,
251 out_len,
252 );
253 } else {
254 break :blk ntdll.NtDeviceIoControlFile(
255 h,
256 null,
257 null,
258 null,
259 &io,
260 ioControlCode,
261 in_ptr,
262 in_len,
263 out_ptr,
264 out_len,
265 );
266 }
267 };
245268 switch (rc) {
246269 .SUCCESS => {},
247270 .INVALID_PARAMETER => unreachable,