Skip to content

Commit 9d57fb0

Browse files
authored
Merge pull request #456 from atrick/rawptr-alloc
Update for SE-0107: UnsafeRawPointer, rename allocate/deallocate.
2 parents 1c6688c + c8cace0 commit 9d57fb0

19 files changed

+80
-80
lines changed

Foundation/NSArray.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
7272
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
7373
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutablePointer<Void>(ptr))
7474
}
75-
let objects = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: Int(cnt))
75+
let objects = UnsafeMutablePointer<AnyObject?>.allocate(capacity: Int(cnt))
7676
for idx in 0..<cnt {
7777
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject())
7878
}
7979
self.init(objects: UnsafePointer<AnyObject?>(objects), count: Int(cnt))
8080
objects.deinitialize(count: Int(cnt))
81-
objects.deallocateCapacity(Int(cnt))
81+
objects.deallocate(capacity: Int(cnt))
8282
} else if aDecoder.dynamicType == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
8383
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
8484
self.init(array: objects)
@@ -158,11 +158,11 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
158158
// self.init(objects: ptr.baseAddress, count: array.count)
159159
// }
160160
let cnt = array.count
161-
let buffer = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: cnt)
161+
let buffer = UnsafeMutablePointer<AnyObject?>.allocate(capacity: cnt)
162162
buffer.initialize(from: optionalArray)
163163
self.init(objects: buffer, count: cnt)
164164
buffer.deinitialize(count: cnt)
165-
buffer.deallocateCapacity(cnt)
165+
buffer.deallocate(capacity: cnt)
166166
}
167167

168168
public override func isEqual(_ object: AnyObject?) -> Bool {
@@ -376,14 +376,14 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
376376

377377
/*@NSCopying*/ public var sortedArrayHint: Data {
378378
let size = count
379-
let buffer = UnsafeMutablePointer<Int32>(allocatingCapacity: size)
379+
let buffer = UnsafeMutablePointer<Int32>.allocate(capacity: size)
380380
for idx in 0..<count {
381381
let item = object(at: idx) as! NSObject
382382
let hash = item.hash
383383
buffer.advanced(by: idx).pointee = Int32(hash).littleEndian
384384
}
385385
return Data(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<UInt8>.self), count: count * sizeof(Int.self), deallocator: .custom({ _ in
386-
buffer.deallocateCapacity(size)
386+
buffer.deallocate(capacity: size)
387387
buffer.deinitialize(count: size)
388388
}))
389389
}

Foundation/NSCFDictionary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ internal final class _NSCFDictionary : NSMutableDictionary {
6464
let cf = dict._cfObject
6565
count = CFDictionaryGetCount(cf)
6666

67-
let keys = UnsafeMutablePointer<UnsafePointer<Void>?>(allocatingCapacity: count)
67+
let keys = UnsafeMutablePointer<UnsafePointer<Void>?>.allocate(capacity: count)
6868
CFDictionaryGetKeysAndValues(cf, keys, nil)
6969

7070
for idx in 0..<count {
7171
let key = unsafeBitCast(keys.advanced(by: idx).pointee!, to: NSObject.self)
7272
keyArray.append(key)
7373
}
7474
keys.deinitialize()
75-
keys.deallocateCapacity(count)
75+
keys.deallocate(capacity: count)
7676
}
7777
}
7878

Foundation/NSCoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class NSCoder : NSObject {
2222
deinit {
2323
for buffer in _pendingBuffers {
2424
buffer.0.deinitialize()
25-
buffer.0.deallocateCapacity(buffer.1)
25+
buffer.0.deallocate(capacity: buffer.1)
2626
}
2727
}
2828

@@ -159,7 +159,7 @@ public class NSCoder : NSObject {
159159
decodeValue(ofObjCType: "I", at: unsafeBitCast(ptr, to: UnsafeMutablePointer<Void>.self))
160160
}
161161
// we cannot autorelease here so instead the pending buffers will manage the lifespan of the returned data... this is wasteful but good enough...
162-
let result = UnsafeMutablePointer<Void>(allocatingCapacity: Int(length))
162+
let result = UnsafeMutablePointer<Void>.allocate(capacity: Int(length))
163163
decodeValue(ofObjCType: "c", at: result)
164164
lengthp.pointee = Int(length)
165165
_pendingBuffers.append((result, Int(length)))

Foundation/NSConcreteValue.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ internal class NSConcreteValue : NSValue {
9191

9292
self._typeInfo = typeInfo!
9393

94-
self._storage = UnsafeMutablePointer<UInt8>(allocatingCapacity: self._typeInfo.size)
94+
self._storage = UnsafeMutablePointer<UInt8>.allocate(capacity: self._typeInfo.size)
9595
self._storage.initialize(from: unsafeBitCast(value, to: UnsafeMutablePointer<UInt8>.self), count: self._typeInfo.size)
9696
}
9797

9898
deinit {
9999
self._storage.deinitialize(count: self._size)
100-
self._storage.deallocateCapacity(self._size)
100+
self._storage.deallocate(capacity: self._size)
101101
}
102102

103103
override func getValue(_ value: UnsafeMutablePointer<Void>) {

Foundation/NSDictionary.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import CoreFoundation
1212

1313
extension Dictionary : _ObjectTypeBridgeable {
1414
public func _bridgeToObject() -> NSDictionary {
15-
let keyBuffer = UnsafeMutablePointer<NSObject>(allocatingCapacity: count)
16-
let valueBuffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: count)
15+
let keyBuffer = UnsafeMutablePointer<NSObject>.allocate(capacity: count)
16+
let valueBuffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: count)
1717

1818
var idx = 0
1919

@@ -29,8 +29,8 @@ extension Dictionary : _ObjectTypeBridgeable {
2929

3030
keyBuffer.deinitialize(count: count)
3131
valueBuffer.deinitialize(count: count)
32-
keyBuffer.deallocateCapacity(count)
33-
valueBuffer.deallocateCapacity(count)
32+
keyBuffer.deallocate(capacity: count)
33+
valueBuffer.deallocate(capacity: count)
3434

3535
return dict
3636
}
@@ -52,8 +52,8 @@ extension Dictionary : _ObjectTypeBridgeable {
5252
let cf = x._cfObject
5353
let cnt = CFDictionaryGetCount(cf)
5454

55-
let keys = UnsafeMutablePointer<UnsafePointer<Void>?>(allocatingCapacity: cnt)
56-
let values = UnsafeMutablePointer<UnsafePointer<Void>?>(allocatingCapacity: cnt)
55+
let keys = UnsafeMutablePointer<UnsafePointer<Void>?>.allocate(capacity: cnt)
56+
let values = UnsafeMutablePointer<UnsafePointer<Void>?>.allocate(capacity: cnt)
5757

5858
CFDictionaryGetKeysAndValues(cf, keys, values)
5959

@@ -68,8 +68,8 @@ extension Dictionary : _ObjectTypeBridgeable {
6868
}
6969
keys.deinitialize(count: cnt)
7070
values.deinitialize(count: cnt)
71-
keys.deallocateCapacity(cnt)
72-
values.deallocateCapacity(cnt)
71+
keys.deallocate(capacity: cnt)
72+
values.deallocate(capacity: cnt)
7373
}
7474
if !failedConversion {
7575
result = dict
@@ -128,17 +128,17 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
128128
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
129129
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutablePointer<Void>(ptr))
130130
}
131-
let keys = UnsafeMutablePointer<NSObject>(allocatingCapacity: Int(cnt))
132-
let objects = UnsafeMutablePointer<AnyObject>(allocatingCapacity: Int(cnt))
131+
let keys = UnsafeMutablePointer<NSObject>.allocate(capacity: Int(cnt))
132+
let objects = UnsafeMutablePointer<AnyObject>.allocate(capacity: Int(cnt))
133133
for idx in 0..<cnt {
134134
keys.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject()! as! NSObject)
135135
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject()!)
136136
}
137137
self.init(objects: UnsafePointer<AnyObject>(objects), forKeys: UnsafePointer<NSObject>(keys), count: Int(cnt))
138138
keys.deinitialize(count: Int(cnt))
139-
keys.deallocateCapacity(Int(cnt))
139+
keys.deallocate(capacity: Int(cnt))
140140
objects.deinitialize(count: Int(cnt))
141-
objects.deallocateCapacity(Int(cnt))
141+
objects.deallocate(capacity: Int(cnt))
142142

143143
} else if aDecoder.dynamicType == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
144144
let keys = aDecoder._decodeArrayOfObjectsForKey("NS.keys").map() { return $0 as! NSObject }
@@ -226,18 +226,18 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
226226
// }
227227

228228
public convenience init(objects: [AnyObject], forKeys keys: [NSObject]) {
229-
let keyBuffer = UnsafeMutablePointer<NSObject>(allocatingCapacity: keys.count)
229+
let keyBuffer = UnsafeMutablePointer<NSObject>.allocate(capacity: keys.count)
230230
keyBuffer.initialize(from: keys)
231231

232-
let valueBuffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: objects.count)
232+
let valueBuffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: objects.count)
233233
valueBuffer.initialize(from: objects)
234234

235235
self.init(objects: valueBuffer, forKeys:keyBuffer, count: keys.count)
236236

237237
keyBuffer.deinitialize(count: keys.count)
238238
valueBuffer.deinitialize(count: objects.count)
239-
keyBuffer.deallocateCapacity(keys.count)
240-
valueBuffer.deallocateCapacity(objects.count)
239+
keyBuffer.deallocate(capacity: keys.count)
240+
valueBuffer.deallocate(capacity: objects.count)
241241
}
242242

243243
public override func isEqual(_ object: AnyObject?) -> Bool {

Foundation/NSFileHandle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public class Pipe: NSObject {
333333

334334
public override init() {
335335
/// the `pipe` system call creates two `fd` in a malloc'ed area
336-
var fds = UnsafeMutablePointer<Int32>(allocatingCapacity: 2)
336+
var fds = UnsafeMutablePointer<Int32>.allocate(capacity: 2)
337337
defer {
338338
free(fds)
339339
}

Foundation/NSFileManager.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,12 @@ public class FileManager: NSObject {
467467
} else if errno == ENOTEMPTY {
468468

469469
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
470-
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 2)
470+
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
471471
ps.initialize(to: UnsafeMutablePointer(fsRep))
472472
ps.advanced(by: 1).initialize(to: nil)
473473
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
474474
ps.deinitialize(count: 2)
475-
ps.deallocateCapacity(2)
475+
ps.deallocate(capacity: 2)
476476

477477
if stream != nil {
478478
defer {
@@ -701,13 +701,13 @@ public class FileManager: NSObject {
701701
if len == kCFNotFound {
702702
fatalError("string could not be converted")
703703
}
704-
let buf = UnsafeMutablePointer<Int8>(allocatingCapacity: len)
704+
let buf = UnsafeMutablePointer<Int8>.allocate(capacity: len)
705705
for i in 0..<len {
706706
buf.advanced(by: i).initialize(to: 0)
707707
}
708708
if !path._nsObject.getFileSystemRepresentation(buf, maxLength: len) {
709709
buf.deinitialize(count: len)
710-
buf.deallocateCapacity(len)
710+
buf.deallocate(capacity: len)
711711
fatalError("string could not be converted")
712712
}
713713
return UnsafePointer(buf)
@@ -913,12 +913,12 @@ extension FileManager {
913913
if let path = _url.path {
914914
if FileManager.default().fileExists(atPath: path) {
915915
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
916-
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 2)
916+
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
917917
ps.initialize(to: UnsafeMutablePointer(fsRep))
918918
ps.advanced(by: 1).initialize(to: nil)
919919
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
920920
ps.deinitialize(count: 2)
921-
ps.deallocateCapacity(2)
921+
ps.deallocate(capacity: 2)
922922
} else {
923923
_rootError = _NSErrorWithErrno(ENOENT, reading: true, url: url)
924924
}

Foundation/NSHost.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public class Host: NSObject {
9292
}
9393
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? sizeof(sockaddr_in6.self) : sizeof(sockaddr_in.self))
9494
let lookupInfo = { (content: inout [String], flags: Int32) in
95-
let hname = UnsafeMutablePointer<Int8>(allocatingCapacity: 1024)
95+
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: 1024)
9696
if (getnameinfo(info.ai_addr, sa_len, hname, 1024, nil, 0, flags) == 0) {
9797
content.append(String(hname))
9898
}
9999
hname.deinitialize()
100-
hname.deallocateCapacity(1024)
100+
hname.deallocate(capacity: 1024)
101101
}
102102
lookupInfo(&_addresses, NI_NUMERICHOST)
103103
lookupInfo(&_names, NI_NAMEREQD)

Foundation/NSJSONSerialization.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,10 @@ private struct JSONReader {
637637
memcpy(buffer.baseAddress!, address, min(count, temp_buffer_size - 1)) // ensure null termination
638638

639639
let startPointer = UnsafePointer<Int8>(buffer.baseAddress!)
640-
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 1)
641-
defer { intEndPointer.deallocateCapacity(1) }
642-
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 1)
643-
defer { doubleEndPointer.deallocateCapacity(1) }
640+
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
641+
defer { intEndPointer.deallocate(capacity: 1) }
642+
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
643+
defer { doubleEndPointer.deallocate(capacity: 1) }
644644

645645
let intResult = strtol(startPointer, intEndPointer, 10)
646646
let intDistance = startPointer.distance(to: intEndPointer[0]!)

Foundation/NSKeyedCoderOldStyleArray.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
4040
deinit {
4141
if self._decoded {
4242
self._addr.deinitialize(count: self._count * self._size)
43-
self._addr.deallocateCapacity(self._count * self._size)
43+
self._addr.deallocate(capacity: self._count * self._size)
4444
}
4545
}
4646

@@ -60,7 +60,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
6060
return nil
6161
}
6262

63-
self._addr = UnsafeMutablePointer<UInt8>(allocatingCapacity: self._count * self._size)
63+
self._addr = UnsafeMutablePointer<UInt8>.allocate(capacity: self._count * self._size)
6464

6565
super.init()
6666

Foundation/NSLock.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public protocol Locking {
2323
}
2424

2525
public class Lock: NSObject, Locking {
26-
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>(allocatingCapacity: 1)
26+
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
2727

2828
public override init() {
2929
pthread_mutex_init(mutex, nil)
@@ -32,7 +32,7 @@ public class Lock: NSObject, Locking {
3232
deinit {
3333
pthread_mutex_destroy(mutex)
3434
mutex.deinitialize()
35-
mutex.deallocateCapacity(1)
35+
mutex.deallocate(capacity: 1)
3636
}
3737

3838
public func lock() {
@@ -136,7 +136,7 @@ public class NSConditionLock : NSObject, Locking {
136136
}
137137

138138
public class RecursiveLock: NSObject, Locking {
139-
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>(allocatingCapacity: 1)
139+
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
140140

141141
public override init() {
142142
super.init()
@@ -150,7 +150,7 @@ public class RecursiveLock: NSObject, Locking {
150150
deinit {
151151
pthread_mutex_destroy(mutex)
152152
mutex.deinitialize()
153-
mutex.deallocateCapacity(1)
153+
mutex.deallocate(capacity: 1)
154154
}
155155

156156
public func lock() {
@@ -169,8 +169,8 @@ public class RecursiveLock: NSObject, Locking {
169169
}
170170

171171
public class Condition: NSObject, Locking {
172-
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>(allocatingCapacity: 1)
173-
internal var cond = UnsafeMutablePointer<pthread_cond_t>(allocatingCapacity: 1)
172+
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
173+
internal var cond = UnsafeMutablePointer<pthread_cond_t>.allocate(capacity: 1)
174174

175175
public override init() {
176176
pthread_mutex_init(mutex, nil)
@@ -182,8 +182,8 @@ public class Condition: NSObject, Locking {
182182
pthread_cond_destroy(cond)
183183
mutex.deinitialize()
184184
cond.deinitialize()
185-
mutex.deallocateCapacity(1)
186-
cond.deallocateCapacity(1)
185+
mutex.deallocate(capacity: 1)
186+
cond.deallocate(capacity: 1)
187187
}
188188

189189
public func lock() {

Foundation/NSOrderedSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,13 @@ extension NSOrderedSet {
285285
}
286286

287287
public convenience init(array: [AnyObject]) {
288-
let buffer = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: array.count)
288+
let buffer = UnsafeMutablePointer<AnyObject?>.allocate(capacity: array.count)
289289
for (idx, element) in array.enumerated() {
290290
buffer.advanced(by: idx).initialize(to: element)
291291
}
292292
self.init(objects: buffer, count: array.count)
293293
buffer.deinitialize(count: array.count)
294-
buffer.deallocateCapacity(array.count)
294+
buffer.deallocate(capacity: array.count)
295295
}
296296

297297
public convenience init(array set: [AnyObject], copyItems flag: Bool) {

0 commit comments

Comments
 (0)