Skip to content

Commit f5336a8

Browse files
committed
darwin: add ability to dealloc obtained thread list for a task
1 parent f505cb9 commit f5336a8

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

lib/std/os/darwin.zig

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,24 @@ const mach_task = if (builtin.target.isDarwin()) struct {
439439
}
440440
}
441441

442-
pub fn getThreads(task: MachTask) MachError![]ThreadId {
442+
const ThreadList = struct {
443+
buf: []ThreadId,
444+
445+
pub fn deinit(list: ThreadList) void {
446+
const self_task = machTaskForSelf();
447+
_ = std.c.vm_deallocate(
448+
self_task.port,
449+
@ptrToInt(list.buf.ptr),
450+
@intCast(std.c.vm_size_t, list.buf.len * @sizeOf(std.c.mach_port_t)),
451+
);
452+
}
453+
};
454+
455+
pub fn getThreads(task: MachTask) MachError!ThreadList {
443456
var thread_list: std.c.mach_port_array_t = undefined;
444457
var thread_count: std.c.mach_msg_type_number_t = undefined;
445458
switch (std.c.getKernError(std.c.task_threads(task.port, &thread_list, &thread_count))) {
446-
.SUCCESS => return @ptrCast([*]ThreadId, thread_list)[0..thread_count],
459+
.SUCCESS => return ThreadList{ .buf = @ptrCast([*]ThreadId, thread_list)[0..thread_count] },
447460
else => |err| {
448461
log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)});
449462
return error.Unexpected;

0 commit comments

Comments
 (0)