Skip to content

Commit e4d06ae

Browse files
committed
simplify deinit
1 parent d259ef4 commit e4d06ae

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Sources/System/MachPort.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ public enum Mach {
8181
}
8282

8383
deinit {
84-
if _name == 0xFFFFFFFF /* MACH_PORT_DEAD */ {
85-
precondition(RightType.self != ReceiveRight.self, "Receive rights cannot be dead names")
86-
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_DEAD_NAME, -1))
84+
if RightType.self == ReceiveRight.self {
85+
precondition(_name != 0xffffffff, "Receive rights cannot be dead names")
86+
_machPrecondition(
87+
mach_port_destruct(mach_task_self_, _name, 0, _context)
88+
)
8789
} else {
88-
if RightType.self == ReceiveRight.self {
89-
_machPrecondition(mach_port_destruct(mach_task_self_, _name, 0, _context))
90-
} else if RightType.self == SendRight.self {
91-
_machPrecondition(mach_port_deallocate(mach_task_self_, _name))
92-
} else if RightType.self == SendOnceRight.self {
93-
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND_ONCE, -1))
94-
}
90+
assert(
91+
RightType.self == SendRight.self ||
92+
RightType.self == SendOnceRight.self
93+
)
94+
_machPrecondition(mach_port_deallocate(mach_task_self_, _name))
9595
}
9696
}
9797
}

Tests/SystemTests/MachPortTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ final class MachPortTests: XCTestCase {
210210
XCTAssertEqual(context, $1)
211211
}
212212
}
213+
214+
func testDeinitDeadSendRight() throws {
215+
let send = Mach.Port<Mach.SendRight>(name: 0xffffffff)
216+
send.withBorrowedName {
217+
XCTAssertEqual($0, .max)
218+
}
219+
_ = consume send
220+
221+
let send1 = Mach.Port<Mach.SendOnceRight>(name: 0xffffffff)
222+
send1.withBorrowedName {
223+
XCTAssertEqual($0, .max)
224+
}
225+
_ = consume send1
226+
}
213227
}
214228

215229
#endif

0 commit comments

Comments
 (0)