import Foundation import Darwin @main struct MachPortChecks { static func hostReferences() -> mach_port_urefs_t { let host = mach_host_self() defer { mach_port_deallocate(mach_task_self_, host) } var references: mach_port_urefs_t = 0 precondition(mach_port_get_refs(mach_task_self_, host, mach_port_right_t(MACH_PORT_RIGHT_SEND), &references) == KERN_SUCCESS) return references } static func main() { var cpu = CPUMonitor() _ = cpu.sample() _ = MemoryMonitor.sample() let initial = hostReferences() for _ in 0..<1_000 { _ = cpu.sample() } precondition(hostReferences() == initial, "CPU sampling leaked host send-right references") for _ in 0..<1_000 { _ = MemoryMonitor.sample() } precondition(hostReferences() == initial, "Memory sampling leaked host send-right references") print("PASS: 1,000 CPU + 1,000 memory samples keep host send-right references at \(initial)") } }