From 65693b8b808d013cab03055963989cbe97a145ba Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Mon, 14 Aug 2023 18:00:14 -0700 Subject: [PATCH] improve memory rebinding for `mach_port_get_attributes` call --- Sources/System/MachPort.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sources/System/MachPort.swift b/Sources/System/MachPort.swift index 0f58243..5852146 100644 --- a/Sources/System/MachPort.swift +++ b/Sources/System/MachPort.swift @@ -263,11 +263,19 @@ extension Mach.Port where RightType == Mach.ReceiveRight { public var makeSendCount: mach_port_mscount_t { get { var status: mach_port_status = mach_port_status() - var size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout.size / MemoryLayout.size) - withUnsafeMutablePointer(to: &size) { size in - withUnsafeMutablePointer(to: &status) { status in - let info = UnsafeMutableRawPointer(status).bindMemory(to: integer_t.self, capacity: 1) - _machPrecondition(mach_port_get_attributes(mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, size)) + var size = mach_msg_type_number_t( + MemoryLayout.size / MemoryLayout.size + ) + + withUnsafeMutablePointer(to: &status) { + let status = UnsafeMutableBufferPointer(start: $0, count: 1) + status.withMemoryRebound(to: integer_t.self) { + let info = $0.baseAddress + _machPrecondition( + mach_port_get_attributes( + mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, &size + ) + ) } } return status.mps_mscount