Skip to content

Commit 9cdaaa3

Browse files
authored
Merge pull request #121 from loffgren/machport_fixes
Mach.Port: Tidiness
2 parents 08e9abe + 16c7885 commit 9cdaaa3

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

Sources/System/MachPort.swift

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

1414
public protocol MachPortRight {}
1515

16-
private func machPrecondition(
16+
private func _machPrecondition(
1717
file: StaticString = #file,
1818
line: UInt = #line,
1919
_ body: @autoclosure () -> kern_return_t
@@ -23,11 +23,13 @@ private func machPrecondition(
2323
precondition(kr == expected, file: file, line: line)
2424
}
2525

26+
/*System 1.3.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
27+
@frozen
2628
public enum Mach {
2729
@_moveOnly
2830
public struct Port<RightType: MachPortRight> {
29-
internal var name: mach_port_name_t
30-
internal var context: mach_port_context_t
31+
internal var _name: mach_port_name_t
32+
internal var _context: mach_port_context_t
3133

3234
/// Transfer ownership of an existing unmanaged Mach port right into a
3335
/// `Mach.Port` by name.
@@ -44,17 +46,17 @@ public enum Mach {
4446
/// This initializer makes a syscall to guard the right.
4547
public init(name: mach_port_name_t) {
4648
precondition(name != mach_port_name_t(MACH_PORT_NULL), "Mach.Port cannot be initialized with MACH_PORT_NULL")
47-
self.name = name
49+
self._name = name
4850

4951
if RightType.self == ReceiveRight.self {
5052
precondition(name != 0xFFFFFFFF /* MACH_PORT_DEAD */, "Receive rights cannot be dead names")
5153

5254
let secret = mach_port_context_t(arc4random())
53-
machPrecondition(mach_port_guard(mach_task_self_, name, secret, 0))
54-
self.context = secret
55+
_machPrecondition(mach_port_guard(mach_task_self_, name, secret, 0))
56+
self._context = secret
5557
}
5658
else {
57-
self.context = 0
59+
self._context = 0
5860
}
5961
}
6062

@@ -68,21 +70,23 @@ public enum Mach {
6870
///
6971
/// The body block may optionally return something, which will then be
7072
/// returned to the caller of withBorrowedName.
71-
public func withBorrowedName<ReturnType>(body: (mach_port_name_t) -> ReturnType) -> ReturnType {
72-
return body(name)
73+
public func withBorrowedName<ReturnType>(
74+
body: (mach_port_name_t) -> ReturnType
75+
) -> ReturnType {
76+
return body(_name)
7377
}
7478

7579
deinit {
76-
if name == 0xFFFFFFFF /* MACH_PORT_DEAD */ {
80+
if _name == 0xFFFFFFFF /* MACH_PORT_DEAD */ {
7781
precondition(RightType.self != ReceiveRight.self, "Receive rights cannot be dead names")
78-
machPrecondition(mach_port_mod_refs(mach_task_self_, name, MACH_PORT_RIGHT_DEAD_NAME, -1))
82+
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_DEAD_NAME, -1))
7983
} else {
8084
if RightType.self == ReceiveRight.self {
81-
machPrecondition(mach_port_destruct(mach_task_self_, name, -1, context))
85+
_machPrecondition(mach_port_destruct(mach_task_self_, _name, -1, _context))
8286
} else if RightType.self == SendRight.self {
83-
machPrecondition(mach_port_mod_refs(mach_task_self_, name, MACH_PORT_RIGHT_SEND, -1))
87+
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND, -1))
8488
} else if RightType.self == SendOnceRight.self {
85-
machPrecondition(mach_port_mod_refs(mach_task_self_, name, MACH_PORT_RIGHT_SEND_ONCE, -1))
89+
_machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND_ONCE, -1))
8690
}
8791
}
8892
}
@@ -115,21 +119,22 @@ public enum Mach {
115119
///
116120
/// This function will abort if the rights could not be created.
117121
/// Callers may assert that valid rights are always returned.
118-
public static func allocatePortRightPair() -> (receive: Mach.Port<Mach.ReceiveRight>, send: Mach.Port<Mach.SendRight>) {
122+
public static func allocatePortRightPair() ->
123+
(receive: Mach.Port<Mach.ReceiveRight>, send: Mach.Port<Mach.SendRight>) {
119124
var name = mach_port_name_t(MACH_PORT_NULL)
120125
let secret = mach_port_context_t(arc4random())
121126
withUnsafeMutablePointer(to: &name) { name in
122127
var options = mach_port_options_t()
123128
options.flags = UInt32(MPO_INSERT_SEND_RIGHT);
124129
withUnsafeMutablePointer(to: &options) { options in
125-
machPrecondition(mach_port_construct(mach_task_self_, options, secret, name))
130+
_machPrecondition(mach_port_construct(mach_task_self_, options, secret, name))
126131
}
127132
}
128133
return (Mach.Port(name: name, context: secret), Mach.Port(name: name))
129134
}
130135
}
131136

132-
public extension Mach.Port where RightType == Mach.ReceiveRight {
137+
extension Mach.Port where RightType == Mach.ReceiveRight {
133138
/// Transfer ownership of an existing, unmanaged, but already guarded,
134139
/// Mach port right into a Mach.Port by name.
135140
///
@@ -140,20 +145,20 @@ public extension Mach.Port where RightType == Mach.ReceiveRight {
140145
///
141146
/// The underlying port right will be automatically deallocated when
142147
/// the Mach.Port object is destroyed.
143-
init(name: mach_port_name_t, context: mach_port_context_t) {
144-
self.name = name
145-
self.context = context
148+
public init(name: mach_port_name_t, context: mach_port_context_t) {
149+
self._name = name
150+
self._context = context
146151
}
147152

148153
/// Allocate a new Mach port with a receive right, creating a
149154
/// Mach.Port<Mach.ReceiveRight> to manage it.
150155
///
151156
/// This initializer will abort if the right could not be created.
152157
/// Callers may assert that a valid right is always returned.
153-
init() {
158+
public init() {
154159
var storage: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL)
155160
withUnsafeMutablePointer(to: &storage) { storage in
156-
machPrecondition(mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, storage))
161+
_machPrecondition(mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, storage))
157162
}
158163

159164
// name-only init will guard ReceiveRights
@@ -171,8 +176,9 @@ public extension Mach.Port where RightType == Mach.ReceiveRight {
171176
///
172177
/// After this function completes, the Mach.Port is destroyed and no longer
173178
/// usable.
174-
__consuming func relinquish() -> (name: mach_port_name_t, context: mach_port_context_t) {
175-
return (name: name, context: context)
179+
public __consuming func relinquish() ->
180+
(name: mach_port_name_t, context: mach_port_context_t) {
181+
return (name: _name, context: _context)
176182
}
177183

178184
/// Remove guard and transfer ownership of the underlying port right to
@@ -189,9 +195,9 @@ public extension Mach.Port where RightType == Mach.ReceiveRight {
189195
/// This function makes a syscall to remove the guard from
190196
/// Mach.ReceiveRights. Use relinquish() to avoid the syscall and extract
191197
/// the context value along with the port name.
192-
__consuming func unguardAndRelinquish() -> mach_port_name_t {
193-
machPrecondition(mach_port_unguard(mach_task_self_, name, context))
194-
return name
198+
public __consuming func unguardAndRelinquish() -> mach_port_name_t {
199+
_machPrecondition(mach_port_unguard(mach_task_self_, _name, _context))
200+
return _name
195201
}
196202

197203
/// Borrow access to the port name in a block that can perform
@@ -203,8 +209,10 @@ public extension Mach.Port where RightType == Mach.ReceiveRight {
203209
///
204210
/// The body block may optionally return something, which will then be
205211
/// returned to the caller of withBorrowedName.
206-
func withBorrowedName<ReturnType>(body: (mach_port_name_t, mach_port_context_t) -> ReturnType) -> ReturnType {
207-
body(name, context)
212+
public func withBorrowedName<ReturnType>(
213+
body: (mach_port_name_t, mach_port_context_t) -> ReturnType
214+
) -> ReturnType {
215+
body(_name, _context)
208216
}
209217

210218
/// Create a send-once right for a given receive right.
@@ -213,16 +221,16 @@ public extension Mach.Port where RightType == Mach.ReceiveRight {
213221
///
214222
/// This function will abort if the right could not be created.
215223
/// Callers may assert that a valid right is always returned.
216-
func makeSendOnceRight() -> Mach.Port<Mach.SendOnceRight> {
224+
public func makeSendOnceRight() -> Mach.Port<Mach.SendOnceRight> {
217225
// send once rights do not coalesce
218226
var newRight: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL)
219227
var newRightType: mach_port_type_t = MACH_PORT_TYPE_NONE
220228

221229
withUnsafeMutablePointer(to: &newRight) { newRight in
222230
withUnsafeMutablePointer(to: &newRightType) { newRightType in
223-
machPrecondition(
231+
_machPrecondition(
224232
mach_port_extract_right(mach_task_self_,
225-
name,
233+
_name,
226234
mach_msg_type_name_t(MACH_MSG_TYPE_MAKE_SEND_ONCE),
227235
newRight,
228236
newRightType)
@@ -242,38 +250,38 @@ public extension Mach.Port where RightType == Mach.ReceiveRight {
242250
///
243251
/// This function will abort if the right could not be created.
244252
/// Callers may assert that a valid right is always returned.
245-
func makeSendRight() -> Mach.Port<Mach.SendRight> {
253+
public func makeSendRight() -> Mach.Port<Mach.SendRight> {
246254
let how = MACH_MSG_TYPE_MAKE_SEND
247255

248256
// name is the same because send and recv rights are coalesced
249-
machPrecondition(mach_port_insert_right(mach_task_self_, name, name, mach_msg_type_name_t(how)))
257+
_machPrecondition(mach_port_insert_right(mach_task_self_, _name, _name, mach_msg_type_name_t(how)))
250258

251-
return Mach.Port(name: name)
259+
return Mach.Port(name: _name)
252260
}
253261

254262
/// Access the make-send count.
255263
///
256264
/// Each get/set of this property makes a syscall.
257-
var makeSendCount : mach_port_mscount_t {
265+
public var makeSendCount : mach_port_mscount_t {
258266
get {
259267
var status: mach_port_status = mach_port_status()
260268
var size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<mach_port_status>.size / MemoryLayout<natural_t>.size)
261269
withUnsafeMutablePointer(to: &size) { size in
262270
withUnsafeMutablePointer(to: &status) { status in
263271
let info = UnsafeMutableRawPointer(status).bindMemory(to: integer_t.self, capacity: 1)
264-
machPrecondition(mach_port_get_attributes(mach_task_self_, name, MACH_PORT_RECEIVE_STATUS, info, size))
272+
_machPrecondition(mach_port_get_attributes(mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, size))
265273
}
266274
}
267275
return status.mps_mscount
268276
}
269277

270278
set {
271-
machPrecondition(mach_port_set_mscount(mach_task_self_, name, newValue))
279+
_machPrecondition(mach_port_set_mscount(mach_task_self_, _name, newValue))
272280
}
273281
}
274282
}
275283

276-
public extension Mach.Port where RightType == Mach.SendRight {
284+
extension Mach.Port where RightType == Mach.SendRight {
277285
/// Transfer ownership of the underlying port right to the caller.
278286
///
279287
/// Returns the Mach port name representing the right.
@@ -283,8 +291,8 @@ public extension Mach.Port where RightType == Mach.SendRight {
283291
///
284292
/// After this function completes, the Mach.Port is destroyed and no longer
285293
/// usable.
286-
__consuming func relinquish() -> mach_port_name_t {
287-
return name
294+
public __consuming func relinquish() -> mach_port_name_t {
295+
return _name
288296
}
289297

290298
/// Create another send right from a given send right.
@@ -294,22 +302,22 @@ public extension Mach.Port where RightType == Mach.SendRight {
294302
/// If the send right being copied has become a dead name, meaning the
295303
/// receiving side has been deallocated, then copySendRight() will throw
296304
/// a Mach.PortRightError.deadName error.
297-
func copySendRight() throws -> Mach.Port<Mach.SendRight> {
305+
public func copySendRight() throws -> Mach.Port<Mach.SendRight> {
298306
let how = MACH_MSG_TYPE_COPY_SEND
299307

300308
// name is the same because send rights are coalesced
301-
let kr = mach_port_insert_right(mach_task_self_, name, name, mach_msg_type_name_t(how))
309+
let kr = mach_port_insert_right(mach_task_self_, _name, _name, mach_msg_type_name_t(how))
302310
if kr == KERN_INVALID_CAPABILITY {
303311
throw Mach.PortRightError.deadName
304312
}
305-
machPrecondition(kr)
313+
_machPrecondition(kr)
306314

307-
return Mach.Port(name: name)
315+
return Mach.Port(name: _name)
308316
}
309317
}
310318

311319

312-
public extension Mach.Port where RightType == Mach.SendOnceRight {
320+
extension Mach.Port where RightType == Mach.SendOnceRight {
313321
/// Transfer ownership of the underlying port right to the caller.
314322
///
315323
/// Returns the Mach port name representing the right.
@@ -319,8 +327,8 @@ public extension Mach.Port where RightType == Mach.SendOnceRight {
319327
///
320328
/// After this function completes, the Mach.Port is destroyed and no longer
321329
/// usable.
322-
__consuming func relinquish() -> mach_port_name_t {
323-
return name
330+
public __consuming func relinquish() -> mach_port_name_t {
331+
return _name
324332
}
325333
}
326334

Utilities/expand-availability.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"System 0.0.2": "macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0",
4949
"System 1.1.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
5050
"System 1.2.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
51+
"System 1.3.0": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
5152
}
5253

5354
parser = argparse.ArgumentParser(description="Expand availability macros.")

0 commit comments

Comments
 (0)