Skip to content

Commit 1f70e25

Browse files
gribozavrMax Moiseev
authored and
Max Moiseev
committed
UnsafeMutablePointer.alloc(_:) => UnsafeMutablePointer(allocatingCapacity:)
1 parent 4e8291f commit 1f70e25

20 files changed

+42
-42
lines changed

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

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

1616
public func _opaqueIdentity<T>(x: T) -> T {
17-
let ptr = UnsafeMutablePointer<T>.alloc(1)
17+
let ptr = UnsafeMutablePointer<T>(allocatingCapacity: 1)
1818
ptr.initialize(x)
1919
let result =
2020
UnsafeMutablePointer<T>(_stdlib_getPointer(OpaquePointer(ptr))).memory

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ public func checkSequence<
19301930
sequence._preprocessingPass { (sequence)->Void in
19311931
var count = 0
19321932
for _ in sequence { ++count }
1933-
let buf = UnsafeMutablePointer<S.Iterator.Element>.alloc(count)
1933+
let buf = UnsafeMutablePointer<S.Iterator.Element>(allocatingCapacity: count)
19341934
let end = sequence._initializeTo(buf)
19351935
expectTrue(end == buf + count, "_initializeTo returned the wrong value")
19361936
var j = expected.startIndex

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

+1-1
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>.alloc(count)
36+
let shards = UnsafeMutablePointer<Int>(allocatingCapacity: count)
3737
for var i = 0; i != count; i++ {
3838
(shards + i).initialize(0)
3939
}

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

+2-2
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.memory.mutex = UnsafeMutablePointer.alloc(1)
70+
barrier.memory.mutex = UnsafeMutablePointer(allocatingCapacity: 1)
7171
if pthread_mutex_init(barrier.memory.mutex, nil) != 0 {
7272
// FIXME: leaking memory.
7373
return -1
7474
}
75-
barrier.memory.cond = UnsafeMutablePointer.alloc(1)
75+
barrier.memory.cond = UnsafeMutablePointer(allocatingCapacity: 1)
7676
if pthread_cond_init(barrier.memory.cond, nil) != 0 {
7777
// FIXME: leaking memory, leaking a mutex.
7878
return -1

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

+1-1
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>.alloc(1)
44+
let result = UnsafeMutablePointer<Result>(allocatingCapacity: 1)
4545
result.initialize(block(arg))
4646
return UnsafeMutablePointer(result)
4747
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public func _autorelease(x: AnyObject) {
6363
func _withUninitializedString<R>(
6464
body: (UnsafeMutablePointer<String>) -> R
6565
) -> (R, String) {
66-
let stringPtr = UnsafeMutablePointer<String>.alloc(1)
66+
let stringPtr = UnsafeMutablePointer<String>(allocatingCapacity: 1)
6767
let bodyResult = body(stringPtr)
6868
let stringResult = stringPtr.move()
6969
stringPtr.dealloc(1)

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ public struct ${Self}<Pointee>
102102
///
103103
/// - Postcondition: The pointee is allocated, but not initialized.
104104
@warn_unused_result
105-
public static func alloc(num: Int) -> ${Self} {
106-
let size = strideof(Pointee.self) * num
107-
return ${Self}(
108-
Builtin.allocRaw(size._builtinWordValue, Builtin.alignof(Pointee.self)))
105+
public init(allocatingCapacity count: Int) {
106+
let size = strideof(Pointee.self) * count
107+
self._rawValue =
108+
Builtin.allocRaw(size._builtinWordValue, Builtin.alignof(Pointee.self))
109109
}
110110
111111
/// Deallocate uninitialized memory allocated for `count` instances

Diff for: test/1_stdlib/Builtins.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct Large : P {
137137
struct ContainsP { var p: P }
138138

139139
func exerciseArrayValueWitnesses<T>(value: T) {
140-
let buf = UnsafeMutablePointer<T>.alloc(5)
140+
let buf = UnsafeMutablePointer<T>(allocatingCapacity: 5)
141141

142142
(buf + 0).initialize(value)
143143
(buf + 1).initialize(value)

Diff for: test/1_stdlib/Lazy.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func expectSequencePassthrough<
403403
}
404404

405405
SequenceLog._initializeTo.expectIncrement(baseType) { ()->Void in
406-
let buf = UnsafeMutablePointer<S.Iterator.Element>.alloc(count)
406+
let buf = UnsafeMutablePointer<S.Iterator.Element>(allocatingCapacity: count)
407407

408408
let end = s._initializeTo(buf)
409409
expectTrue(end <= buf + count)

Diff for: test/1_stdlib/NSStringAPI.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ NSStringAPIs.test("init(withCString_:encoding:)") {
198198

199199
NSStringAPIs.test("init(UTF8String:)") {
200200
var s = "foo あいう"
201-
var up = UnsafeMutablePointer<UInt8>.alloc(100)
201+
var up = UnsafeMutablePointer<UInt8>(allocatingCapacity: 100)
202202
var i = 0
203203
for b in s.utf8 {
204204
up[i] = b
@@ -2144,15 +2144,15 @@ func getNullCString() -> UnsafeMutablePointer<CChar> {
21442144
}
21452145

21462146
func getASCIICString() -> (UnsafeMutablePointer<CChar>, dealloc: ()->()) {
2147-
let up = UnsafeMutablePointer<CChar>.alloc(100)
2147+
let up = UnsafeMutablePointer<CChar>(allocatingCapacity: 100)
21482148
up[0] = 0x61
21492149
up[1] = 0x62
21502150
up[2] = 0
21512151
return (up, { up.dealloc(100) })
21522152
}
21532153

21542154
func getNonASCIICString() -> (UnsafeMutablePointer<CChar>, dealloc: ()->()) {
2155-
let up = UnsafeMutablePointer<UInt8>.alloc(100)
2155+
let up = UnsafeMutablePointer<UInt8>(allocatingCapacity: 100)
21562156
up[0] = 0xd0
21572157
up[1] = 0xb0
21582158
up[2] = 0xd0
@@ -2163,7 +2163,7 @@ func getNonASCIICString() -> (UnsafeMutablePointer<CChar>, dealloc: ()->()) {
21632163

21642164
func getIllFormedUTF8String1(
21652165
) -> (UnsafeMutablePointer<CChar>, dealloc: ()->()) {
2166-
let up = UnsafeMutablePointer<UInt8>.alloc(100)
2166+
let up = UnsafeMutablePointer<UInt8>(allocatingCapacity: 100)
21672167
up[0] = 0x41
21682168
up[1] = 0xed
21692169
up[2] = 0xa0
@@ -2175,7 +2175,7 @@ func getIllFormedUTF8String1(
21752175

21762176
func getIllFormedUTF8String2(
21772177
) -> (UnsafeMutablePointer<CChar>, dealloc: ()->()) {
2178-
let up = UnsafeMutablePointer<UInt8>.alloc(100)
2178+
let up = UnsafeMutablePointer<UInt8>(allocatingCapacity: 100)
21792179
up[0] = 0x41
21802180
up[1] = 0xed
21812181
up[2] = 0xa0

Diff for: test/1_stdlib/Reflection.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ var randomUnsafeMutablePointerString = UnsafeMutablePointer<String>(
204204
print(_reflect(randomUnsafeMutablePointerString).summary)
205205

206206
// CHECK-NEXT: Hello panda
207-
var sanePointerString = UnsafeMutablePointer<String>.alloc(1)
207+
var sanePointerString = UnsafeMutablePointer<String>(allocatingCapacity: 1)
208208
sanePointerString.initialize("Hello panda")
209209
print(_reflect(sanePointerString.memory).summary)
210210
sanePointerString.destroy()

Diff for: test/1_stdlib/UnsafePointer.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func checkPointerCorrectness(check: Check,
139139
_ f: (UnsafeMutablePointer<Missile>) ->
140140
(UnsafeMutablePointer<Missile>, count: Int) -> Void,
141141
_ withMissiles: Bool = false) {
142-
let ptr = UnsafeMutablePointer<Missile>.alloc(4)
142+
let ptr = UnsafeMutablePointer<Missile>(allocatingCapacity: 4)
143143
switch check {
144144
case .RightOverlap:
145145
ptr.initialize(Missile(1))
@@ -170,7 +170,7 @@ func checkPointerCorrectness(check: Check,
170170
expectEqual(2, ptr[0].number)
171171
expectEqual(3, ptr[1].number)
172172
// backwards
173-
let ptr2 = UnsafeMutablePointer<Missile>.alloc(4)
173+
let ptr2 = UnsafeMutablePointer<Missile>(allocatingCapacity: 4)
174174
ptr2.initialize(Missile(0))
175175
(ptr2 + 1).initialize(Missile(1))
176176
if withMissiles {

Diff for: test/Interpreter/SDK/libc.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ print("\(UINT32_MAX)")
2121
// CHECK: the magic word is ///* magic *///
2222
let sourceFile = open(sourcePath, O_RDONLY)
2323
assert(sourceFile >= 0)
24-
var bytes = UnsafeMutablePointer<CChar>.alloc(12)
24+
var bytes = UnsafeMutablePointer<CChar>(allocatingCapacity: 12)
2525
var readed = read(sourceFile, bytes, 11)
2626
close(sourceFile)
2727
assert(readed == 11)

Diff for: test/Prototypes/CollectionTransformers.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ struct _ForkJoinMutex {
218218
var _mutex: UnsafeMutablePointer<pthread_mutex_t>
219219

220220
init() {
221-
_mutex = UnsafeMutablePointer.alloc(1)
221+
_mutex = UnsafeMutablePointer(allocatingCapacity: 1)
222222
if pthread_mutex_init(_mutex, nil) != 0 {
223223
fatalError("pthread_mutex_init")
224224
}
@@ -248,7 +248,7 @@ struct _ForkJoinCond {
248248
var _cond: UnsafeMutablePointer<pthread_cond_t> = nil
249249

250250
init() {
251-
_cond = UnsafeMutablePointer.alloc(1)
251+
_cond = UnsafeMutablePointer(allocatingCapacity: 1)
252252
if pthread_cond_init(_cond, nil) != 0 {
253253
fatalError("pthread_cond_init")
254254
}

Diff for: test/SILGen/addressors.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func test1() -> Int32 {
7676
return A()[0]
7777
}
7878

79-
let uninitAddr = UnsafeMutablePointer<Int32>.alloc(1)
79+
let uninitAddr = UnsafeMutablePointer<Int32>(allocatingCapacity: 1)
8080
var global: Int32 {
8181
unsafeAddress {
8282
return UnsafePointer(uninitAddr)
@@ -251,7 +251,7 @@ func test_e(e: E) {
251251
}
252252

253253
class F {
254-
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer.alloc(100)
254+
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer(allocatingCapacity: 100)
255255

256256
final var value: Int32 {
257257
addressWithNativeOwner {
@@ -302,7 +302,7 @@ func test_f1(f: F) {
302302
// CHECK: strong_release [[SELF]] : $F
303303

304304
class G {
305-
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer.alloc(100)
305+
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer(allocatingCapacity: 100)
306306

307307
var value: Int32 {
308308
addressWithNativeOwner {
@@ -375,7 +375,7 @@ class G {
375375
// CHECK: dealloc_value_buffer $Builtin.NativeObject in [[STORAGE]] : $*Builtin.UnsafeValueBuffer
376376

377377
class H {
378-
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer.alloc(100)
378+
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer(allocatingCapacity: 100)
379379

380380
final var value: Int32 {
381381
addressWithPinnedNativeOwner {
@@ -426,7 +426,7 @@ func test_h1(f: H) {
426426
// CHECK: strong_release [[SELF]] : $H
427427

428428
class I {
429-
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer.alloc(100)
429+
var data: UnsafeMutablePointer<Int32> = UnsafeMutablePointer(allocatingCapacity: 100)
430430

431431
var value: Int32 {
432432
addressWithPinnedNativeOwner {
@@ -505,7 +505,7 @@ struct RecMiddle {
505505
var inner: RecInner
506506
}
507507
class RecOuter {
508-
var data: UnsafeMutablePointer<RecMiddle> = UnsafeMutablePointer.alloc(100)
508+
var data: UnsafeMutablePointer<RecMiddle> = UnsafeMutablePointer(allocatingCapacity: 100)
509509
final var middle: RecMiddle {
510510
addressWithPinnedNativeOwner {
511511
return (UnsafePointer(data), Builtin.tryPin(Builtin.castToNativeObject(self)))

Diff for: validation-test/stdlib/ArrayNew.swift.gyb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ for indexRange in [
10031003
.code {
10041004
let a = getBridgedNSArrayOfRefTypeVerbatimBridged(
10051005
numElements: 0, capacity: 16)
1006-
let buffer = UnsafeMutablePointer<AnyObject>.alloc(16)
1006+
let buffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: 16)
10071007
a.getObjects(
10081008
AutoreleasingUnsafeMutablePointer(buffer), range: NSRange(0..<0))
10091009
expectCrashLater()
@@ -1018,7 +1018,7 @@ for indexRange in [ 0..<4, -2..<(-1), -1..<2, 2..<4, 4..<5 ] as [Range<Int>] {
10181018
.code {
10191019
let a = getBridgedNSArrayOfRefTypeVerbatimBridged(
10201020
numElements: 3, capacity: 16)
1021-
let buffer = UnsafeMutablePointer<AnyObject>.alloc(16)
1021+
let buffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: 16)
10221022
a.getObjects(
10231023
AutoreleasingUnsafeMutablePointer(buffer), range: NSRange(0..<3))
10241024
expectCrashLater()
@@ -1029,7 +1029,7 @@ for indexRange in [ 0..<4, -2..<(-1), -1..<2, 2..<4, 4..<5 ] as [Range<Int>] {
10291029

10301030
ArrayTestSuite.test("BridgedToObjC/Verbatim/getObjects") {
10311031
let a = getBridgedNSArrayOfRefTypeVerbatimBridged(numElements: 3)
1032-
let buffer = UnsafeMutablePointer<AnyObject>.alloc(16)
1032+
let buffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: 16)
10331033
a.getObjects(
10341034
AutoreleasingUnsafeMutablePointer(buffer), range: NSRange(0..<3))
10351035

@@ -1275,7 +1275,7 @@ for indexRange in [
12751275
.code {
12761276
let a = getBridgedNSArrayOfValueTypeCustomBridged(
12771277
numElements: 0, capacity: 16)
1278-
let buffer = UnsafeMutablePointer<AnyObject>.alloc(16)
1278+
let buffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: 16)
12791279
a.getObjects(
12801280
AutoreleasingUnsafeMutablePointer(buffer), range: NSRange(0..<0))
12811281
expectCrashLater()
@@ -1290,7 +1290,7 @@ for indexRange in [ 0..<4, -2..<(-1), -1..<2, 2..<4, 4..<5 ] as [Range<Int>] {
12901290
.code {
12911291
let a = getBridgedNSArrayOfValueTypeCustomBridged(
12921292
numElements: 3, capacity: 16)
1293-
let buffer = UnsafeMutablePointer<AnyObject>.alloc(16)
1293+
let buffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: 16)
12941294
a.getObjects(
12951295
AutoreleasingUnsafeMutablePointer(buffer), range: NSRange(0..<3))
12961296
expectCrashLater()
@@ -1301,7 +1301,7 @@ for indexRange in [ 0..<4, -2..<(-1), -1..<2, 2..<4, 4..<5 ] as [Range<Int>] {
13011301

13021302
ArrayTestSuite.test("BridgedToObjC/Custom/getObjects") {
13031303
let a = getBridgedNSArrayOfValueTypeCustomBridged(numElements: 3)
1304-
let buffer = UnsafeMutablePointer<AnyObject>.alloc(16)
1304+
let buffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: 16)
13051305
a.getObjects(
13061306
AutoreleasingUnsafeMutablePointer(buffer), range: NSRange(0..<3))
13071307

Diff for: validation-test/stdlib/CoreAudio.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ CoreAudioTestSuite.test(
191191
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.count") {
192192
let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16)
193193
let ablPtr = UnsafeMutablePointer<AudioBufferList>(
194-
UnsafeMutablePointer<UInt8>.alloc(sizeInBytes))
194+
UnsafeMutablePointer<UInt8>(allocatingCapacity: sizeInBytes))
195195

196196
// It is important that 'ablPtrWrapper' is a 'let'. We are verifying that
197197
// the 'count' property has a nonmutating setter.
@@ -211,7 +211,7 @@ CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.count") {
211211
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.subscript(_: Int)") {
212212
let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16)
213213
let ablPtr = UnsafeMutablePointer<AudioBufferList>(
214-
UnsafeMutablePointer<UInt8>.alloc(sizeInBytes))
214+
UnsafeMutablePointer<UInt8>(allocatingCapacity: sizeInBytes))
215215

216216
// It is important that 'ablPtrWrapper' is a 'let'. We are verifying that
217217
// the subscript has a nonmutating setter.

Diff for: validation-test/stdlib/Dictionary.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -3821,9 +3821,9 @@ DictionaryTestSuite.test("dropsBridgedCache") {
38213821
DictionaryTestSuite.test("getObjects:andKeys:") {
38223822
let d = ([1: "one", 2: "two"] as Dictionary<Int, String>) as NSDictionary
38233823
var keys = UnsafeMutableBufferPointer(
3824-
start: UnsafeMutablePointer<NSNumber>.alloc(2), count: 2)
3824+
start: UnsafeMutablePointer<NSNumber>(allocatingCapacity: 2), count: 2)
38253825
var values = UnsafeMutableBufferPointer(
3826-
start: UnsafeMutablePointer<NSString>.alloc(2), count: 2)
3826+
start: UnsafeMutablePointer<NSString>(allocatingCapacity: 2), count: 2)
38273827
var kp = AutoreleasingUnsafeMutablePointer<AnyObject?>(keys.baseAddress)
38283828
var vp = AutoreleasingUnsafeMutablePointer<AnyObject?>(values.baseAddress)
38293829
var null: AutoreleasingUnsafeMutablePointer<AnyObject?> = nil

Diff for: validation-test/stdlib/HashingAvalanche.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func avalancheTest(bits: Int, _ hashUnderTest: (UInt64) -> UInt64, _ pValue: Dou
2222

2323
for inputBit in 0..<bits {
2424
// Using an array here makes the test too slow.
25-
var bitFlips = UnsafeMutablePointer<Int>.alloc(bits)
25+
var bitFlips = UnsafeMutablePointer<Int>(allocatingCapacity: bits)
2626
for i in 0..<bits {
2727
bitFlips[i] = 0
2828
}

Diff for: validation-test/stdlib/StringSlicesConcurrentAppend.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func sliceConcurrentAppendThread(tid: ThreadID) {
9494
}
9595

9696
StringTestSuite.test("SliceConcurrentAppend") {
97-
barrierVar = UnsafeMutablePointer.alloc(1)
97+
barrierVar = UnsafeMutablePointer(allocatingCapacity: 1)
9898
barrierVar.initialize(_stdlib_pthread_barrier_t())
9999
var ret = _stdlib_pthread_barrier_init(barrierVar, nil, 2)
100100
expectEqual(0, ret)

0 commit comments

Comments
 (0)