Skip to content

Commit f572e4b

Browse files
authored
Merge pull request #122 from lorentey/inlinable-mach-ports
Mach: Avoid non-inlinable generics where possible
2 parents cc96d40 + e708e4a commit f572e4b

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

Sources/System/MachPort.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import Darwin.Mach
1313

1414
public protocol MachPortRight {}
1515

16-
private func _machPrecondition(
16+
@inlinable
17+
internal func _machPrecondition(
1718
file: StaticString = #file,
1819
line: UInt = #line,
1920
_ body: @autoclosure () -> kern_return_t
@@ -28,7 +29,10 @@ private func _machPrecondition(
2829
public enum Mach {
2930
@_moveOnly
3031
public struct Port<RightType: MachPortRight> {
32+
@usableFromInline
3133
internal var _name: mach_port_name_t
34+
35+
@usableFromInline
3236
internal var _context: mach_port_context_t
3337

3438
/// Transfer ownership of an existing unmanaged Mach port right into a
@@ -70,6 +74,7 @@ public enum Mach {
7074
///
7175
/// The body block may optionally return something, which will then be
7276
/// returned to the caller of withBorrowedName.
77+
@inlinable
7378
public func withBorrowedName<ReturnType>(
7479
body: (mach_port_name_t) -> ReturnType
7580
) -> ReturnType {
@@ -101,9 +106,11 @@ public enum Mach {
101106
}
102107

103108
/// The MachPortRight type used to manage a receive right.
109+
@frozen
104110
public struct ReceiveRight: MachPortRight {}
105111

106112
/// The MachPortRight type used to manage a send right.
113+
@frozen
107114
public struct SendRight: MachPortRight {}
108115

109116
/// The MachPortRight type used to manage a send-once right.
@@ -113,6 +120,7 @@ public enum Mach {
113120
///
114121
/// Upon destruction a send-once notification will be sent to the
115122
/// receiving end.
123+
@frozen
116124
public struct SendOnceRight: MachPortRight {}
117125

118126
/// Create a connected pair of rights, one receive, and one send.
@@ -155,6 +163,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
155163
///
156164
/// This initializer will abort if the right could not be created.
157165
/// Callers may assert that a valid right is always returned.
166+
@inlinable
158167
public init() {
159168
var storage: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL)
160169
withUnsafeMutablePointer(to: &storage) { storage in
@@ -176,8 +185,9 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
176185
///
177186
/// After this function completes, the Mach.Port is destroyed and no longer
178187
/// usable.
179-
public __consuming func relinquish() ->
180-
(name: mach_port_name_t, context: mach_port_context_t) {
188+
@inlinable
189+
public __consuming func relinquish(
190+
) -> (name: mach_port_name_t, context: mach_port_context_t) {
181191
return (name: _name, context: _context)
182192
}
183193

@@ -195,6 +205,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
195205
/// This function makes a syscall to remove the guard from
196206
/// Mach.ReceiveRights. Use relinquish() to avoid the syscall and extract
197207
/// the context value along with the port name.
208+
@inlinable
198209
public __consuming func unguardAndRelinquish() -> mach_port_name_t {
199210
_machPrecondition(mach_port_unguard(mach_task_self_, _name, _context))
200211
return _name
@@ -209,10 +220,11 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
209220
///
210221
/// The body block may optionally return something, which will then be
211222
/// returned to the caller of withBorrowedName.
223+
@inlinable
212224
public func withBorrowedName<ReturnType>(
213225
body: (mach_port_name_t, mach_port_context_t) -> ReturnType
214226
) -> ReturnType {
215-
body(_name, _context)
227+
return body(_name, _context)
216228
}
217229

218230
/// Create a send-once right for a given receive right.
@@ -221,6 +233,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
221233
///
222234
/// This function will abort if the right could not be created.
223235
/// Callers may assert that a valid right is always returned.
236+
@inlinable
224237
public func makeSendOnceRight() -> Mach.Port<Mach.SendOnceRight> {
225238
// send once rights do not coalesce
226239
var newRight: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL)
@@ -250,6 +263,7 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
250263
///
251264
/// This function will abort if the right could not be created.
252265
/// Callers may assert that a valid right is always returned.
266+
@inlinable
253267
public func makeSendRight() -> Mach.Port<Mach.SendRight> {
254268
let how = MACH_MSG_TYPE_MAKE_SEND
255269

@@ -262,7 +276,8 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
262276
/// Access the make-send count.
263277
///
264278
/// Each get/set of this property makes a syscall.
265-
public var makeSendCount : mach_port_mscount_t {
279+
@inlinable
280+
public var makeSendCount: mach_port_mscount_t {
266281
get {
267282
var status: mach_port_status = mach_port_status()
268283
var size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<mach_port_status>.size / MemoryLayout<natural_t>.size)
@@ -291,6 +306,7 @@ extension Mach.Port where RightType == Mach.SendRight {
291306
///
292307
/// After this function completes, the Mach.Port is destroyed and no longer
293308
/// usable.
309+
@inlinable
294310
public __consuming func relinquish() -> mach_port_name_t {
295311
return _name
296312
}
@@ -302,6 +318,7 @@ extension Mach.Port where RightType == Mach.SendRight {
302318
/// If the send right being copied has become a dead name, meaning the
303319
/// receiving side has been deallocated, then copySendRight() will throw
304320
/// a Mach.PortRightError.deadName error.
321+
@inlinable
305322
public func copySendRight() throws -> Mach.Port<Mach.SendRight> {
306323
let how = MACH_MSG_TYPE_COPY_SEND
307324

@@ -327,6 +344,7 @@ extension Mach.Port where RightType == Mach.SendOnceRight {
327344
///
328345
/// After this function completes, the Mach.Port is destroyed and no longer
329346
/// usable.
347+
@inlinable
330348
public __consuming func relinquish() -> mach_port_name_t {
331349
return _name
332350
}

0 commit comments

Comments
 (0)