Skip to content

Commit 65693b8

Browse files
committed
improve memory rebinding for mach_port_get_attributes call
1 parent f5edd72 commit 65693b8

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Sources/System/MachPort.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,19 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
263263
public var makeSendCount: mach_port_mscount_t {
264264
get {
265265
var status: mach_port_status = mach_port_status()
266-
var size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<mach_port_status>.size / MemoryLayout<natural_t>.size)
267-
withUnsafeMutablePointer(to: &size) { size in
268-
withUnsafeMutablePointer(to: &status) { status in
269-
let info = UnsafeMutableRawPointer(status).bindMemory(to: integer_t.self, capacity: 1)
270-
_machPrecondition(mach_port_get_attributes(mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, size))
266+
var size = mach_msg_type_number_t(
267+
MemoryLayout<mach_port_status>.size / MemoryLayout<natural_t>.size
268+
)
269+
270+
withUnsafeMutablePointer(to: &status) {
271+
let status = UnsafeMutableBufferPointer(start: $0, count: 1)
272+
status.withMemoryRebound(to: integer_t.self) {
273+
let info = $0.baseAddress
274+
_machPrecondition(
275+
mach_port_get_attributes(
276+
mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, &size
277+
)
278+
)
271279
}
272280
}
273281
return status.mps_mscount

0 commit comments

Comments
 (0)