servo/ports/servoshell/platform/macos/count_threads.c
webbeef d252a631d2
Fix a leak in MacOS thread count function (#34862)
Signed-off-by: webbeef <me@webbeef.org>
2025-01-07 03:28:30 +00:00

20 lines
705 B
C

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <mach/mach.h>
int macos_count_running_threads() {
task_t task = current_task();
thread_act_array_t threads;
mach_msg_type_number_t tcnt;
const kern_return_t status = task_threads(task, &threads, &tcnt);
if (status == KERN_SUCCESS) {
// Free data structures attached to the thread list.
for (uint32_t t = 0; t < tcnt; t++) {
mach_port_deallocate(task, threads[t]);
}
vm_deallocate(task, (vm_address_t)threads, sizeof(thread_t) * tcnt);
}
return tcnt;
}