Skip to content

Update for SE-0107: UnsafeRawPointer, rename allocate/deallocate. #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutablePointer<Void>(ptr))
}
let objects = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: Int(cnt))
let objects = UnsafeMutablePointer<AnyObject?>.allocate(capacity: Int(cnt))
for idx in 0..<cnt {
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject())
}
self.init(objects: UnsafePointer<AnyObject?>(objects), count: Int(cnt))
objects.deinitialize(count: Int(cnt))
objects.deallocateCapacity(Int(cnt))
objects.deallocate(capacity: Int(cnt))
} else if aDecoder.dynamicType == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
self.init(array: objects)
Expand Down Expand Up @@ -158,11 +158,11 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
// self.init(objects: ptr.baseAddress, count: array.count)
// }
let cnt = array.count
let buffer = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: cnt)
let buffer = UnsafeMutablePointer<AnyObject?>.allocate(capacity: cnt)
buffer.initialize(from: optionalArray)
self.init(objects: buffer, count: cnt)
buffer.deinitialize(count: cnt)
buffer.deallocateCapacity(cnt)
buffer.deallocate(capacity: cnt)
}

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

/*@NSCopying*/ public var sortedArrayHint: Data {
let size = count
let buffer = UnsafeMutablePointer<Int32>(allocatingCapacity: size)
let buffer = UnsafeMutablePointer<Int32>.allocate(capacity: size)
for idx in 0..<count {
let item = object(at: idx) as! NSObject
let hash = item.hash
buffer.advanced(by: idx).pointee = Int32(hash).littleEndian
}
return Data(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<UInt8>.self), count: count * sizeof(Int.self), deallocator: .custom({ _ in
buffer.deallocateCapacity(size)
buffer.deallocate(capacity: size)
buffer.deinitialize(count: size)
}))
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCFDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ internal final class _NSCFDictionary : NSMutableDictionary {
let cf = dict._cfObject
count = CFDictionaryGetCount(cf)

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

for idx in 0..<count {
let key = unsafeBitCast(keys.advanced(by: idx).pointee!, to: NSObject.self)
keyArray.append(key)
}
keys.deinitialize()
keys.deallocateCapacity(count)
keys.deallocate(capacity: count)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NSCoder : NSObject {
deinit {
for buffer in _pendingBuffers {
buffer.0.deinitialize()
buffer.0.deallocateCapacity(buffer.1)
buffer.0.deallocate(capacity: buffer.1)
}
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public class NSCoder : NSObject {
decodeValue(ofObjCType: "I", at: unsafeBitCast(ptr, to: UnsafeMutablePointer<Void>.self))
}
// we cannot autorelease here so instead the pending buffers will manage the lifespan of the returned data... this is wasteful but good enough...
let result = UnsafeMutablePointer<Void>(allocatingCapacity: Int(length))
let result = UnsafeMutablePointer<Void>.allocate(capacity: Int(length))
decodeValue(ofObjCType: "c", at: result)
lengthp.pointee = Int(length)
_pendingBuffers.append((result, Int(length)))
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSConcreteValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ internal class NSConcreteValue : NSValue {

self._typeInfo = typeInfo!

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

deinit {
self._storage.deinitialize(count: self._size)
self._storage.deallocateCapacity(self._size)
self._storage.deallocate(capacity: self._size)
}

override func getValue(_ value: UnsafeMutablePointer<Void>) {
Expand Down
32 changes: 16 additions & 16 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import CoreFoundation

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

var idx = 0

Expand All @@ -29,8 +29,8 @@ extension Dictionary : _ObjectTypeBridgeable {

keyBuffer.deinitialize(count: count)
valueBuffer.deinitialize(count: count)
keyBuffer.deallocateCapacity(count)
valueBuffer.deallocateCapacity(count)
keyBuffer.deallocate(capacity: count)
valueBuffer.deallocate(capacity: count)

return dict
}
Expand All @@ -52,8 +52,8 @@ extension Dictionary : _ObjectTypeBridgeable {
let cf = x._cfObject
let cnt = CFDictionaryGetCount(cf)

let keys = UnsafeMutablePointer<UnsafePointer<Void>?>(allocatingCapacity: cnt)
let values = UnsafeMutablePointer<UnsafePointer<Void>?>(allocatingCapacity: cnt)
let keys = UnsafeMutablePointer<UnsafePointer<Void>?>.allocate(capacity: cnt)
let values = UnsafeMutablePointer<UnsafePointer<Void>?>.allocate(capacity: cnt)

CFDictionaryGetKeysAndValues(cf, keys, values)

Expand All @@ -68,8 +68,8 @@ extension Dictionary : _ObjectTypeBridgeable {
}
keys.deinitialize(count: cnt)
values.deinitialize(count: cnt)
keys.deallocateCapacity(cnt)
values.deallocateCapacity(cnt)
keys.deallocate(capacity: cnt)
values.deallocate(capacity: cnt)
}
if !failedConversion {
result = dict
Expand Down Expand Up @@ -128,17 +128,17 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
withUnsafeMutablePointer(&cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutablePointer<Void>(ptr))
}
let keys = UnsafeMutablePointer<NSObject>(allocatingCapacity: Int(cnt))
let objects = UnsafeMutablePointer<AnyObject>(allocatingCapacity: Int(cnt))
let keys = UnsafeMutablePointer<NSObject>.allocate(capacity: Int(cnt))
let objects = UnsafeMutablePointer<AnyObject>.allocate(capacity: Int(cnt))
for idx in 0..<cnt {
keys.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject()! as! NSObject)
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject()!)
}
self.init(objects: UnsafePointer<AnyObject>(objects), forKeys: UnsafePointer<NSObject>(keys), count: Int(cnt))
keys.deinitialize(count: Int(cnt))
keys.deallocateCapacity(Int(cnt))
keys.deallocate(capacity: Int(cnt))
objects.deinitialize(count: Int(cnt))
objects.deallocateCapacity(Int(cnt))
objects.deallocate(capacity: Int(cnt))

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

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

let valueBuffer = UnsafeMutablePointer<AnyObject>(allocatingCapacity: objects.count)
let valueBuffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: objects.count)
valueBuffer.initialize(from: objects)

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

keyBuffer.deinitialize(count: keys.count)
valueBuffer.deinitialize(count: objects.count)
keyBuffer.deallocateCapacity(keys.count)
valueBuffer.deallocateCapacity(objects.count)
keyBuffer.deallocate(capacity: keys.count)
valueBuffer.deallocate(capacity: objects.count)
}

public override func isEqual(_ object: AnyObject?) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public class Pipe: NSObject {

public override init() {
/// the `pipe` system call creates two `fd` in a malloc'ed area
var fds = UnsafeMutablePointer<Int32>(allocatingCapacity: 2)
var fds = UnsafeMutablePointer<Int32>.allocate(capacity: 2)
defer {
free(fds)
}
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,12 @@ public class FileManager: NSObject {
} else if errno == ENOTEMPTY {

let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 2)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(fsRep))
ps.advanced(by: 1).initialize(to: nil)
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
ps.deallocateCapacity(2)
ps.deallocate(capacity: 2)

if stream != nil {
defer {
Expand Down Expand Up @@ -701,13 +701,13 @@ public class FileManager: NSObject {
if len == kCFNotFound {
fatalError("string could not be converted")
}
let buf = UnsafeMutablePointer<Int8>(allocatingCapacity: len)
let buf = UnsafeMutablePointer<Int8>.allocate(capacity: len)
for i in 0..<len {
buf.advanced(by: i).initialize(to: 0)
}
if !path._nsObject.getFileSystemRepresentation(buf, maxLength: len) {
buf.deinitialize(count: len)
buf.deallocateCapacity(len)
buf.deallocate(capacity: len)
fatalError("string could not be converted")
}
return UnsafePointer(buf)
Expand Down Expand Up @@ -913,12 +913,12 @@ extension FileManager {
if let path = _url.path {
if FileManager.default().fileExists(atPath: path) {
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 2)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(fsRep))
ps.advanced(by: 1).initialize(to: nil)
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
ps.deallocateCapacity(2)
ps.deallocate(capacity: 2)
} else {
_rootError = _NSErrorWithErrno(ENOENT, reading: true, url: url)
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public class Host: NSObject {
}
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? sizeof(sockaddr_in6.self) : sizeof(sockaddr_in.self))
let lookupInfo = { (content: inout [String], flags: Int32) in
let hname = UnsafeMutablePointer<Int8>(allocatingCapacity: 1024)
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: 1024)
if (getnameinfo(info.ai_addr, sa_len, hname, 1024, nil, 0, flags) == 0) {
content.append(String(hname))
}
hname.deinitialize()
hname.deallocateCapacity(1024)
hname.deallocate(capacity: 1024)
}
lookupInfo(&_addresses, NI_NUMERICHOST)
lookupInfo(&_names, NI_NAMEREQD)
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@ private struct JSONReader {
memcpy(buffer.baseAddress!, address, min(count, temp_buffer_size - 1)) // ensure null termination

let startPointer = UnsafePointer<Int8>(buffer.baseAddress!)
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 1)
defer { intEndPointer.deallocateCapacity(1) }
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>(allocatingCapacity: 1)
defer { doubleEndPointer.deallocateCapacity(1) }
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
defer { intEndPointer.deallocate(capacity: 1) }
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
defer { doubleEndPointer.deallocate(capacity: 1) }

let intResult = strtol(startPointer, intEndPointer, 10)
let intDistance = startPointer.distance(to: intEndPointer[0]!)
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSKeyedCoderOldStyleArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
deinit {
if self._decoded {
self._addr.deinitialize(count: self._count * self._size)
self._addr.deallocateCapacity(self._count * self._size)
self._addr.deallocate(capacity: self._count * self._size)
}
}

Expand All @@ -60,7 +60,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
return nil
}

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

super.init()

Expand Down
16 changes: 8 additions & 8 deletions Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol Locking {
}

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

public override init() {
pthread_mutex_init(mutex, nil)
Expand All @@ -32,7 +32,7 @@ public class Lock: NSObject, Locking {
deinit {
pthread_mutex_destroy(mutex)
mutex.deinitialize()
mutex.deallocateCapacity(1)
mutex.deallocate(capacity: 1)
}

public func lock() {
Expand Down Expand Up @@ -136,7 +136,7 @@ public class NSConditionLock : NSObject, Locking {
}

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

public override init() {
super.init()
Expand All @@ -150,7 +150,7 @@ public class RecursiveLock: NSObject, Locking {
deinit {
pthread_mutex_destroy(mutex)
mutex.deinitialize()
mutex.deallocateCapacity(1)
mutex.deallocate(capacity: 1)
}

public func lock() {
Expand All @@ -169,8 +169,8 @@ public class RecursiveLock: NSObject, Locking {
}

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

public override init() {
pthread_mutex_init(mutex, nil)
Expand All @@ -182,8 +182,8 @@ public class Condition: NSObject, Locking {
pthread_cond_destroy(cond)
mutex.deinitialize()
cond.deinitialize()
mutex.deallocateCapacity(1)
cond.deallocateCapacity(1)
mutex.deallocate(capacity: 1)
cond.deallocate(capacity: 1)
}

public func lock() {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSOrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ extension NSOrderedSet {
}

public convenience init(array: [AnyObject]) {
let buffer = UnsafeMutablePointer<AnyObject?>(allocatingCapacity: array.count)
let buffer = UnsafeMutablePointer<AnyObject?>.allocate(capacity: array.count)
for (idx, element) in array.enumerated() {
buffer.advanced(by: idx).initialize(to: element)
}
self.init(objects: buffer, count: array.count)
buffer.deinitialize(count: array.count)
buffer.deallocateCapacity(array.count)
buffer.deallocate(capacity: array.count)
}

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