Skip to content

Commit 5a8271c

Browse files
authored
Rename UnsafePointer allocate & deallocate. (#3608)
As proposed in SE-0107: UnsafeRawPointer: Rename 'init(allocatingCapacity:)' to 'UnsafeMutablePointer.allocate(capacity:)' Rename 'deallocateCapacity' to 'deallocate(capacity:)' `allocate` should not be an initializer. It's primary function is to allocate memory, not initialize a pointer.
1 parent f95ffde commit 5a8271c

28 files changed

+130
-118
lines changed

Diff for: benchmark/single-source/PopFront.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public func run_PopFrontArray(_ N: Int) {
3535
@inline(never)
3636
public func run_PopFrontUnsafePointer(_ N: Int) {
3737
var orig = Array(repeating: 1, count: arrayCount)
38-
let a = UnsafeMutablePointer<Int>(allocatingCapacity: arrayCount)
38+
let a = UnsafeMutablePointer<Int>.allocate(capacity: arrayCount)
3939
for _ in 1...100*N {
4040
for _ in 1...reps {
4141
for i in 0..<arrayCount {
@@ -51,6 +51,6 @@ public func run_PopFrontUnsafePointer(_ N: Int) {
5151
CheckResults(result == arrayCount, "IncorrectResults in StringInterpolation: \(result) != \(arrayCount)")
5252
}
5353
}
54-
a.deallocateCapacity(arrayCount)
54+
a.deallocate(capacity: arrayCount)
5555
}
5656

Diff for: stdlib/private/StdlibCollectionUnittest/CheckSequenceInstance.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public func checkSequence<
8686
_ = sequence._preprocessingPass { () -> Void in
8787
var count = 0
8888
for _ in sequence { count += 1 }
89-
let buf = UnsafeMutablePointer<S.Iterator.Element>(allocatingCapacity: count)
89+
let buf = UnsafeMutablePointer<S.Iterator.Element>.allocate(capacity: count)
9090
let end = sequence._copyContents(initializing: buf)
9191
expectTrue(end == buf + count, "_copyContents returned the wrong value")
9292
var j = expected.startIndex
@@ -95,7 +95,7 @@ public func checkSequence<
9595
j = expected.index(after: j)
9696
}
9797
buf.deinitialize(count: end - buf)
98-
buf.deallocateCapacity(count)
98+
buf.deallocate(capacity: count)
9999
}
100100
}
101101

Diff for: stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
func _stdlib_getPointer(_ x: OpaquePointer) -> OpaquePointer
1515

1616
public func _opaqueIdentity<T>(_ x: T) -> T {
17-
let ptr = UnsafeMutablePointer<T>(allocatingCapacity: 1)
17+
let ptr = UnsafeMutablePointer<T>.allocate(capacity: 1)
1818
ptr.initialize(to: x)
1919
let result =
2020
UnsafeMutablePointer<T>(_stdlib_getPointer(OpaquePointer(ptr))).pointee
2121
ptr.deinitialize()
22-
ptr.deallocateCapacity(1)
22+
ptr.deallocate(capacity: 1)
2323
return result
2424
}
2525

Diff for: stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct _stdlib_ShardedAtomicCounter {
3333
public init() {
3434
let hardwareConcurrency = _stdlib_getHardwareConcurrency()
3535
let count = max(8, hardwareConcurrency * hardwareConcurrency)
36-
let shards = UnsafeMutablePointer<Int>(allocatingCapacity: count)
36+
let shards = UnsafeMutablePointer<Int>.allocate(capacity: count)
3737
for i in 0..<count {
3838
(shards + i).initialize(to: 0)
3939
}
@@ -43,7 +43,7 @@ public struct _stdlib_ShardedAtomicCounter {
4343

4444
public func `deinit`() {
4545
self._shardsPtr.deinitialize(count: self._shardsCount)
46-
self._shardsPtr.deallocateCapacity(self._shardsCount)
46+
self._shardsPtr.deallocate(capacity: self._shardsCount)
4747
}
4848

4949
public func add(_ operand: Int, randomInt: Int) {

Diff for: stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public func _stdlib_pthread_barrier_init(
6767
errno = EINVAL
6868
return -1
6969
}
70-
barrier.pointee.mutex = UnsafeMutablePointer(allocatingCapacity: 1)
70+
barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1)
7171
if pthread_mutex_init(barrier.pointee.mutex!, nil) != 0 {
7272
// FIXME: leaking memory.
7373
return -1
7474
}
75-
barrier.pointee.cond = UnsafeMutablePointer(allocatingCapacity: 1)
75+
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
7676
if pthread_cond_init(barrier.pointee.cond!, nil) != 0 {
7777
// FIXME: leaking memory, leaking a mutex.
7878
return -1
@@ -93,9 +93,9 @@ public func _stdlib_pthread_barrier_destroy(
9393
return -1
9494
}
9595
barrier.pointee.cond!.deinitialize()
96-
barrier.pointee.cond!.deallocateCapacity(1)
96+
barrier.pointee.cond!.deallocate(capacity: 1)
9797
barrier.pointee.mutex!.deinitialize()
98-
barrier.pointee.mutex!.deallocateCapacity(1)
98+
barrier.pointee.mutex!.deallocate(capacity: 1)
9999
return 0
100100
}
101101

Diff for: stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal class PthreadBlockContextImpl<Argument, Result>: PthreadBlockContext {
4141
}
4242

4343
override func run() -> UnsafeMutablePointer<Void> {
44-
let result = UnsafeMutablePointer<Result>(allocatingCapacity: 1)
44+
let result = UnsafeMutablePointer<Result>.allocate(capacity: 1)
4545
result.initialize(to: block(arg))
4646
return UnsafeMutablePointer(result)
4747
}
@@ -100,7 +100,7 @@ public func _stdlib_pthread_join<Result>(
100100
if result == 0 {
101101
let threadResult = UnsafeMutablePointer<Result>(threadResultPtr!).pointee
102102
threadResultPtr!.deinitialize()
103-
threadResultPtr!.deallocateCapacity(1)
103+
threadResultPtr!.deallocate(capacity: 1)
104104
return (result, threadResult)
105105
} else {
106106
return (result, nil)

Diff for: stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift

+15-14
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ public func reflect(object: AnyObject) {
356356
/// an Any existential.
357357
public func reflect<T>(any: T) {
358358
let any: Any = any
359-
let anyPointer = UnsafeMutablePointer<Any>(allocatingCapacity: sizeof(Any.self))
359+
let anyPointer = UnsafeMutablePointer<Any>.allocate(capacity: sizeof(Any.self))
360360
anyPointer.initialize(to: any)
361361
let anyPointerValue = unsafeBitCast(anyPointer, to: UInt.self)
362362
reflect(instanceAddress: anyPointerValue, kind: .Existential)
363-
anyPointer.deallocateCapacity(sizeof(Any.self))
363+
anyPointer.deallocate(capacity: sizeof(Any.self))
364364
}
365365

366366
// Reflect an `Error`, a.k.a. an "error existential".
@@ -419,61 +419,62 @@ struct ThickFunctionParts {
419419
/// Reflect a closure context. The given function must be a Swift-native
420420
/// @convention(thick) function value.
421421
public func reflect(function: () -> ()) {
422-
let fn = UnsafeMutablePointer<ThickFunction0>(
423-
allocatingCapacity: sizeof(ThickFunction0.self))
422+
let fn = UnsafeMutablePointer<ThickFunction0>.allocate(
423+
capacity: sizeof(ThickFunction0.self))
424424
fn.initialize(to: ThickFunction0(function: function))
425425

426426
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
427427
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
428428

429429
reflect(instanceAddress: contextPointer, kind: .Object)
430430

431-
fn.deallocateCapacity(sizeof(ThickFunction0.self))
431+
fn.deallocate(capacity: sizeof(ThickFunction0.self))
432432
}
433433

434434
/// Reflect a closure context. The given function must be a Swift-native
435435
/// @convention(thick) function value.
436436
public func reflect(function: (Int) -> ()) {
437-
let fn = UnsafeMutablePointer<ThickFunction1>(
438-
allocatingCapacity: sizeof(ThickFunction1.self))
437+
let fn =
438+
UnsafeMutablePointer<ThickFunction1>.allocate(
439+
capacity: sizeof(ThickFunction1.self))
439440
fn.initialize(to: ThickFunction1(function: function))
440441

441442
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
442443
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
443444

444445
reflect(instanceAddress: contextPointer, kind: .Object)
445446

446-
fn.deallocateCapacity(sizeof(ThickFunction1.self))
447+
fn.deallocate(capacity: sizeof(ThickFunction1.self))
447448
}
448449

449450
/// Reflect a closure context. The given function must be a Swift-native
450451
/// @convention(thick) function value.
451452
public func reflect(function: (Int, String) -> ()) {
452-
let fn = UnsafeMutablePointer<ThickFunction2>(
453-
allocatingCapacity: sizeof(ThickFunction2.self))
453+
let fn = UnsafeMutablePointer<ThickFunction2>.allocate(
454+
capacity: sizeof(ThickFunction2.self))
454455
fn.initialize(to: ThickFunction2(function: function))
455456

456457
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
457458
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
458459

459460
reflect(instanceAddress: contextPointer, kind: .Object)
460461

461-
fn.deallocateCapacity(sizeof(ThickFunction2.self))
462+
fn.deallocate(capacity: sizeof(ThickFunction2.self))
462463
}
463464

464465
/// Reflect a closure context. The given function must be a Swift-native
465466
/// @convention(thick) function value.
466467
public func reflect(function: (Int, String, AnyObject?) -> ()) {
467-
let fn = UnsafeMutablePointer<ThickFunction3>(
468-
allocatingCapacity: sizeof(ThickFunction3.self))
468+
let fn = UnsafeMutablePointer<ThickFunction3>.allocate(
469+
capacity: sizeof(ThickFunction3.self))
469470
fn.initialize(to: ThickFunction3(function: function))
470471

471472
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
472473
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
473474

474475
reflect(instanceAddress: contextPointer, kind: .Object)
475476

476-
fn.deallocateCapacity(sizeof(ThickFunction3.self))
477+
fn.deallocate(capacity: sizeof(ThickFunction3.self))
477478
}
478479

479480
/// Call this function to indicate to the parent that there are

Diff for: stdlib/public/core/Misc.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public func _autorelease(_ x: AnyObject) {
6060
func _withUninitializedString<R>(
6161
_ body: (UnsafeMutablePointer<String>) -> R
6262
) -> (R, String) {
63-
let stringPtr = UnsafeMutablePointer<String>(allocatingCapacity: 1)
63+
let stringPtr = UnsafeMutablePointer<String>.allocate(capacity: 1)
6464
let bodyResult = body(stringPtr)
6565
let stringResult = stringPtr.move()
66-
stringPtr.deallocateCapacity(1)
66+
stringPtr.deallocate(capacity: 1)
6767
return (bodyResult, stringResult)
6868
}
6969

Diff for: stdlib/public/core/UnsafePointer.swift.gyb

+18-7
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ public struct ${Self}<Pointee>
126126
/// instances of `Pointee`.
127127
///
128128
/// - Postcondition: The pointee is allocated, but not initialized.
129-
public init(allocatingCapacity count: Int) {
129+
static public func allocate(capacity count: Int)
130+
-> UnsafeMutablePointer<Pointee> {
130131
let size = strideof(Pointee.self) * count
131-
self._rawValue =
132-
Builtin.allocRaw(size._builtinWordValue, Builtin.alignof(Pointee.self))
132+
return UnsafeMutablePointer(
133+
Builtin.allocRaw(size._builtinWordValue, Builtin.alignof(Pointee.self)))
133134
}
134135
135136
/// Deallocate uninitialized memory allocated for `count` instances
@@ -138,8 +139,8 @@ public struct ${Self}<Pointee>
138139
/// - Precondition: The memory is not initialized.
139140
///
140141
/// - Postcondition: The memory has been deallocated.
141-
public func deallocateCapacity(_ num: Int) {
142-
let size = strideof(Pointee.self) * num
142+
public func deallocate(capacity: Int) {
143+
let size = strideof(Pointee.self) * capacity
143144
Builtin.deallocRaw(
144145
_rawValue, size._builtinWordValue, Builtin.alignof(Pointee.self))
145146
}
@@ -487,15 +488,25 @@ extension ${Self} {
487488
}
488489

489490
% if mutable:
490-
@available(*, unavailable, message: "use the '${Self}(allocatingCapacity:)' initializer")
491+
@available(*, unavailable, renamed: "allocate(capacity:)")
491492
public static func alloc(_ num: Int) -> ${Self} {
492493
Builtin.unreachable()
493494
}
494495

495-
@available(*, unavailable, renamed: "deallocateCapacity")
496+
@available(*, unavailable, message: "use '${Self}.allocate(capacity:)'")
497+
public init(allocatingCapacity: Int) {
498+
Builtin.unreachable()
499+
}
500+
501+
@available(*, unavailable, renamed: "deallocate(capacity:)")
496502
public func dealloc(_ num: Int) {
497503
Builtin.unreachable()
498504
}
505+
506+
@available(*, unavailable, renamed: "deallocate(capacity:)")
507+
public func deallocateCapacity(_ num: Int) {
508+
Builtin.unreachable()
509+
}
499510
% end
500511

501512
@available(*, unavailable, renamed: "pointee")

Diff for: test/1_stdlib/Builtins.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct Large : P {
128128
struct ContainsP { var p: P }
129129

130130
func exerciseArrayValueWitnesses<T>(_ value: T) {
131-
let buf = UnsafeMutablePointer<T>(allocatingCapacity: 5)
131+
let buf = UnsafeMutablePointer<T>.allocate(capacity: 5)
132132

133133
(buf + 0).initialize(to: value)
134134
(buf + 1).initialize(to: value)
@@ -138,7 +138,7 @@ func exerciseArrayValueWitnesses<T>(_ value: T) {
138138
Builtin.takeArrayFrontToBack(T.self, buf._rawValue, (buf + 1)._rawValue, 4._builtinWordValue)
139139
Builtin.destroyArray(T.self, buf._rawValue, 4._builtinWordValue)
140140

141-
buf.deallocateCapacity(5)
141+
buf.deallocate(capacity: 5)
142142
}
143143

144144
tests.test("array value witnesses") {

Diff for: test/1_stdlib/NSStringAPI.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,15 @@ NSStringAPIs.test("init(cString_:encoding:)") {
197197

198198
NSStringAPIs.test("init(utf8String:)") {
199199
var s = "foo あいう"
200-
var up = UnsafeMutablePointer<UInt8>(allocatingCapacity: 100)
200+
var up = UnsafeMutablePointer<UInt8>.allocate(capacity: 100)
201201
var i = 0
202202
for b in s.utf8 {
203203
up[i] = b
204204
i += 1
205205
}
206206
up[i] = 0
207207
expectOptionalEqual(s, String(utf8String: UnsafePointer(up)))
208-
up.deallocateCapacity(100)
208+
up.deallocate(capacity: 100)
209209
}
210210

211211
NSStringAPIs.test("canBeConvertedToEncoding(_:)") {

Diff for: test/1_stdlib/Reflection.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ var randomUnsafeMutablePointerString = UnsafeMutablePointer<String>(
181181
dump(randomUnsafeMutablePointerString)
182182

183183
// CHECK-NEXT: Hello panda
184-
var sanePointerString = UnsafeMutablePointer<String>(allocatingCapacity: 1)
184+
var sanePointerString = UnsafeMutablePointer<String>.allocate(capacity: 1)
185185
sanePointerString.initialize(to: "Hello panda")
186186
dump(sanePointerString.pointee)
187187
sanePointerString.deinitialize()
188-
sanePointerString.deallocateCapacity(1)
188+
sanePointerString.deallocate(capacity: 1)
189189

190190
// Don't crash on types with opaque metadata. rdar://problem/19791252
191191
// CHECK-NEXT: (Opaque Value)

Diff for: test/1_stdlib/Renames.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,17 @@ func _UnsafePointer<T>(x: UnsafeMutablePointer<T>, e: T) {
498498
_ = UnsafeMutablePointer<T>.Memory.self // expected-error {{'Memory' has been renamed to 'Pointee'}} {{31-37=Pointee}} {{none}}
499499
_ = UnsafeMutablePointer<T>() // expected-error {{'init()' is unavailable: use 'nil' literal}} {{none}}
500500
_ = x.memory // expected-error {{'memory' has been renamed to 'pointee'}} {{9-15=pointee}} {{none}}
501-
_ = UnsafeMutablePointer<T>.alloc(1) // expected-error {{'alloc' is unavailable: use the 'UnsafeMutablePointer(allocatingCapacity:)' initializer}} {{none}}
502-
x.dealloc(1) // expected-error {{'dealloc' has been renamed to 'deallocateCapacity'}} {{5-12=deallocateCapacity}} {{none}}
501+
_ = UnsafeMutablePointer<T>.alloc(1) // expected-error {{'alloc' has been renamed to 'allocate(capacity:)'}} {{31-36=allocate}} {{37-37=capacity: }} {{none}}
502+
x.dealloc(1) // expected-error {{'dealloc' has been renamed to 'deallocate(capacity:)'}} {{5-12=deallocate}} {{13-13=capacity: }} {{none}}
503503
x.memory = e // expected-error {{'memory' has been renamed to 'pointee'}} {{5-11=pointee}} {{none}}
504504
x.initialize(e) // expected-error {{'initialize' has been renamed to 'initialize(to:)'}} {{5-15=initialize}} {{16-16=to: }} {{none}}
505505
x.destroy() // expected-error {{'destroy()' has been renamed to 'deinitialize(count:)'}} {{5-12=deinitialize}} {{none}}
506506
x.destroy(1) // expected-error {{'destroy' has been renamed to 'deinitialize(count:)'}} {{5-12=deinitialize}} {{13-13=count: }} {{none}}
507507
x.initialize(with: e) // expected-error {{'initialize(with:count:)' has been renamed to 'initialize(to:count:)'}} {{5-15=initialize}}
508508

509-
let ptr1 = UnsafeMutablePointer<T>(allocatingCapacity: 1)
509+
let ptr1 = UnsafeMutablePointer<T>(allocatingCapacity: 1) // expected-error {{'init(allocatingCapacity:)' is unavailable: use 'UnsafeMutablePointer.allocate(capacity:)'}} {{none}}
510510
ptr1.initialize(with: e, count: 1) // expected-error {{'initialize(with:count:)' has been renamed to 'initialize(to:count:)'}}
511-
let ptr2 = UnsafeMutablePointer<T>(allocatingCapacity: 1)
511+
let ptr2 = UnsafeMutablePointer<T>.allocate(capacity: 1)
512512
ptr2.initializeFrom(ptr1, count: 1) // expected-error {{'initializeFrom(_:count:)' has been renamed to 'initialize(from:count:)'}}
513513
ptr1.assignFrom(ptr2, count: 1) // expected-error {{'assignFrom(_:count:)' has been renamed to 'assign(from:count:)'}}
514514
ptr1.assignFrom(ptr2, count: 1) // expected-error {{'assignFrom(_:count:)' has been renamed to 'assign(from:count:)'}}
@@ -517,8 +517,8 @@ func _UnsafePointer<T>(x: UnsafeMutablePointer<T>, e: T) {
517517
ptr2.moveInitializeFrom(ptr1, count: 1) // expected-error {{'moveInitializeFrom(_:count:)' has been renamed to 'moveInitialize(from:count:)'}}
518518
ptr1.moveInitializeBackwardFrom(ptr1, count: 1) // expected-error {{'moveInitializeBackwardFrom(_:count:)' has been renamed to 'moveInitialize(from:count:)'}}
519519
ptr1.deinitialize(count:1)
520-
ptr1.deallocateCapacity(1)
521-
ptr2.deallocateCapacity(1)
520+
ptr1.deallocateCapacity(1) // expected-error {{'deallocateCapacity' has been renamed to 'deallocate(capacity:)'}} {{8-26=deallocate}} {{27-27=capacity: }} {{none}}
521+
ptr2.deallocate(capacity: 1)
522522
}
523523

524524
func _VarArgs() {

0 commit comments

Comments
 (0)