Skip to content

Revert "Revert "Merge pull request #1247 from kelvin13/se-0184a"" #1343

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

Closed
Closed
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
4 changes: 2 additions & 2 deletions Foundation/AffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
preconditionFailure("Unkeyed coding is unsupported.")
}

let pointer = UnsafeMutableRawPointer.allocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
let pointer = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<Float>.stride * 6, alignment: 1)
defer {
pointer.deallocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
pointer.deallocate()
}
aDecoder.decodeValue(ofObjCType: "[6f]", at: pointer)

Expand Down
4 changes: 2 additions & 2 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public final class _DataStorage {
}
return try apply(UnsafeRawBufferPointer(start: d.bytes.advanced(by: range.lowerBound - _offset), count: Swift.min(range.count, len)))
} else {
var buffer = UnsafeMutableRawBufferPointer.allocate(count: range.count)
var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: range.count, alignment: MemoryLayout<UInt>.alignment)
defer { buffer.deallocate() }
let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
var enumerated = 0
Expand Down Expand Up @@ -184,7 +184,7 @@ public final class _DataStorage {
}
return try apply(UnsafeRawBufferPointer(start: d.bytes.advanced(by: range.lowerBound - _offset), count: Swift.min(range.count, len)))
} else {
var buffer = UnsafeMutableRawBufferPointer.allocate(count: range.count)
var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: range.count, alignment: MemoryLayout<UInt>.alignment)
defer { buffer.deallocate() }
let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
var enumerated = 0
Expand Down
8 changes: 4 additions & 4 deletions Foundation/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ extension Dictionary : _ObjectTypeBridgeable {

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

return dict

Expand Down Expand Up @@ -75,8 +75,8 @@ extension Dictionary : _ObjectTypeBridgeable {
}
keys.deinitialize(count: cnt)
values.deinitialize(count: cnt)
keys.deallocate(capacity: cnt)
values.deallocate(capacity: cnt)
keys.deallocate()
values.deallocate()
}
if !failedConversion {
result = dict
Expand Down
6 changes: 3 additions & 3 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ open class FileManager : NSObject {
ps.advanced(by: 1).initialize(to: nil)
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
ps.deallocate(capacity: 2)
ps.deallocate()

if stream != nil {
defer {
Expand Down Expand Up @@ -682,7 +682,7 @@ open class FileManager : NSObject {
}
if !path._nsObject.getFileSystemRepresentation(buf, maxLength: len) {
buf.deinitialize(count: len)
buf.deallocate(capacity: len)
buf.deallocate()
fatalError("string could not be converted")
}
return UnsafePointer(buf)
Expand Down Expand Up @@ -1038,7 +1038,7 @@ extension FileManager {
ps.advanced(by: 1).initialize(to: nil)
_stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR, nil)
ps.deinitialize(count: 2)
ps.deallocate(capacity: 2)
ps.deallocate()
} else {
_rootError = _NSErrorWithErrno(ENOENT, reading: true, url: url)
}
Expand Down
10 changes: 3 additions & 7 deletions Foundation/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ open class Host: NSObject {
static internal func currentHostName() -> String {
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
hname.deinitialize()
hname.deallocate(capacity: Int(NI_MAXHOST))
hname.deallocate()
}
let r = gethostname(hname, Int(NI_MAXHOST))
if r < 0 || hname[0] == 0 {
Expand Down Expand Up @@ -80,8 +79,7 @@ open class Host: NSObject {
let address = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
freeifaddrs(ifaddr)
address.deinitialize()
address.deallocate(capacity: Int(NI_MAXHOST))
address.deallocate()
}
while let ifaValue = ifa?.pointee {
if let ifa_addr = ifaValue.ifa_addr, ifaValue.ifa_flags & UInt32(IFF_LOOPBACK) == 0 {
Expand Down Expand Up @@ -138,8 +136,7 @@ open class Host: NSObject {
var res: UnsafeMutablePointer<addrinfo>? = res0
let host = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
host.deinitialize()
host.deallocate(capacity: Int(NI_MAXHOST))
host.deallocate()
}
while res != nil {
let info = res!.pointee
Expand Down Expand Up @@ -186,4 +183,3 @@ open class Host: NSObject {
return nil
}
}

4 changes: 2 additions & 2 deletions Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,9 @@ private struct JSONReader {

let startPointer = buffer.baseAddress!
let intEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
defer { intEndPointer.deallocate(capacity: 1) }
defer { intEndPointer.deallocate() }
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
defer { doubleEndPointer.deallocate(capacity: 1) }
defer { doubleEndPointer.deallocate() }
let intResult = strtol(startPointer, intEndPointer, 10)
let intDistance = startPointer.distance(to: intEndPointer[0]!)
let doubleResult = strtod(startPointer, doubleEndPointer)
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
buffer.initialize(from: optionalArray, count: cnt)
self.init(objects: buffer, count: cnt)
buffer.deinitialize(count: cnt)
buffer.deallocate(capacity: cnt)
buffer.deallocate()
}

open override func isEqual(_ value: Any?) -> Bool {
Expand Down Expand Up @@ -404,7 +404,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
}
}
return Data(bytesNoCopy: buffer, count: size, deallocator: .custom({ (_, _) in
buffer.deallocate(capacity: size)
buffer.deallocate()
}))
}

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCFDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ internal final class _NSCFDictionary : NSMutableDictionary {
let key = unsafeBitCast(keys.advanced(by: idx).pointee!, to: NSObject.self)
keyArray.append(key)
}
keys.deinitialize()
keys.deallocate(capacity: count)
keys.deinitialize(count: 1)
keys.deallocate()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCFSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ internal final class _NSCFSet : NSMutableSet {
let obj = unsafeBitCast(objects.advanced(by: idx).pointee!, to: AnyObject.self)
objArray.append(obj)
}
objects.deinitialize()
objects.deallocate(capacity: count)
objects.deinitialize(count: 1)
objects.deallocate()

return NSGeneratorEnumerator(objArray.makeIterator())

Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ open class NSCoder : NSObject {
deinit {
for buffer in _pendingBuffers {
// Cannot deinitialize a pointer to unknown type.
buffer.0.deallocate(bytes: buffer.1, alignedTo: MemoryLayout<Int>.alignment)
buffer.0.deallocate()
}
}

Expand Down Expand Up @@ -383,7 +383,7 @@ open class NSCoder : NSObject {
decodeValue(ofObjCType: "I", at: unsafeBitCast(ptr, to: UnsafeMutableRawPointer.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 = UnsafeMutableRawPointer.allocate(bytes: Int(length), alignedTo: MemoryLayout<Int>.alignment)
let result = UnsafeMutableRawPointer.allocate(byteCount: Int(length), alignment: MemoryLayout<Int>.alignment)
decodeValue(ofObjCType: "c", at: result)
lengthp.pointee = Int(length)
_pendingBuffers.append((result, Int(length)))
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSConcreteValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ internal class NSConcreteValue : NSValue {

self._typeInfo = typeInfo!

self._storage = UnsafeMutableRawPointer.allocate(bytes: self._typeInfo.size, alignedTo: 1)
self._storage.copyBytes(from: value, count: self._typeInfo.size)
self._storage = UnsafeMutableRawPointer.allocate(byteCount: self._typeInfo.size, alignment: 1)
self._storage.copyMemory(from: value, byteCount: self._typeInfo.size)
}

deinit {
// Cannot deinitialize raw memory.
self._storage.deallocate(bytes: self._size, alignedTo: 1)
self._storage.deallocate()
}

override func getValue(_ value: UnsafeMutableRawPointer) {
value.copyBytes(from: self._storage, count: self._size)
value.copyMemory(from: self._storage, byteCount: self._size)
}

override var objCType : UnsafePointer<Int8> {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,

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

public convenience init(dictionary otherDictionary: [AnyHashable : Any]) {
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSGeometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension CGPoint: NSSpecialValueCoding {
}

func getValue(_ value: UnsafeMutableRawPointer) {
value.initializeMemory(as: CGPoint.self, to: self)
value.initializeMemory(as: CGPoint.self, repeating: self, count: 1)
}

func isEqual(_ aValue: Any) -> Bool {
Expand Down Expand Up @@ -162,7 +162,7 @@ extension CGSize: NSSpecialValueCoding {
}

func getValue(_ value: UnsafeMutableRawPointer) {
value.initializeMemory(as: CGSize.self, to: self)
value.initializeMemory(as: CGSize.self, repeating: self, count: 1)
}

func isEqual(_ aValue: Any) -> Bool {
Expand Down Expand Up @@ -466,7 +466,7 @@ extension CGRect: NSSpecialValueCoding {
}

func getValue(_ value: UnsafeMutableRawPointer) {
value.initializeMemory(as: CGRect.self, to: self)
value.initializeMemory(as: CGRect.self, repeating: self, count: 1)
}

func isEqual(_ aValue: Any) -> Bool {
Expand Down Expand Up @@ -565,7 +565,7 @@ extension NSEdgeInsets: NSSpecialValueCoding {
}

func getValue(_ value: UnsafeMutableRawPointer) {
value.initializeMemory(as: NSEdgeInsets.self, to: self)
value.initializeMemory(as: NSEdgeInsets.self, repeating: self, count: 1)
}

func isEqual(_ aValue: Any) -> Bool {
Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSKeyedCoderOldStyleArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC
deinit {
if self._decoded {
// Cannot deinitialize memory without knowing the element type.
self._addr.deallocate(bytes: self._count * self._size, alignedTo: 1)
self._addr.deallocate()
}
}

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

self._addr = UnsafeMutableRawPointer.allocate(bytes: self._count * self._size, alignedTo: 1)
self._addr = UnsafeMutableRawPointer.allocate(byteCount: self._count * self._size, alignment: 1)

super.init()

Expand Down Expand Up @@ -95,7 +95,7 @@ internal final class _NSKeyedCoderOldStyleArray : NSObject, NSCopying, NSSecureC

func fillObjCType(_ type: _NSSimpleObjCType, count: Int, at addr: UnsafeMutableRawPointer) {
if type == self._type && count <= self._count {
addr.copyBytes(from: self._addr, count: count * self._size)
addr.copyMemory(from: self._addr, byteCount: count * self._size)
}
}

Expand Down
16 changes: 8 additions & 8 deletions Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ open class NSLock: NSObject, NSLocking {

deinit {
pthread_mutex_destroy(mutex)
mutex.deinitialize()
mutex.deallocate(capacity: 1)
mutex.deinitialize(count: 1)
mutex.deallocate()
#if os(OSX) || os(iOS)
deallocateTimedLockData(cond: timeoutCond, mutex: timeoutMutex)
#endif
Expand Down Expand Up @@ -196,8 +196,8 @@ open class NSRecursiveLock: NSObject, NSLocking {

deinit {
pthread_mutex_destroy(mutex)
mutex.deinitialize()
mutex.deallocate(capacity: 1)
mutex.deinitialize(count: 1)
mutex.deallocate()
#if os(OSX) || os(iOS)
deallocateTimedLockData(cond: timeoutCond, mutex: timeoutMutex)
#endif
Expand Down Expand Up @@ -251,10 +251,10 @@ open class NSCondition: NSObject, NSLocking {
deinit {
pthread_mutex_destroy(mutex)
pthread_cond_destroy(cond)
mutex.deinitialize()
cond.deinitialize()
mutex.deallocate(capacity: 1)
cond.deallocate(capacity: 1)
mutex.deinitialize(count: 1)
cond.deinitialize(count: 1)
mutex.deallocate()
cond.deallocate()
}

open func lock() {
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSOrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ extension NSOrderedSet {
}
self.init(objects: buffer, count: array.count)
buffer.deinitialize(count: array.count)
buffer.deallocate(capacity: array.count)
buffer.deallocate()
}

public convenience init(array set: [Any], copyItems flag: Bool) {
Expand Down
3 changes: 1 addition & 2 deletions Foundation/NSRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ extension NSRange: NSSpecialValueCoding {
}

func getValue(_ value: UnsafeMutableRawPointer) {
value.initializeMemory(as: NSRange.self, to: self)
value.initializeMemory(as: NSRange.self, repeating: self, count: 1)
}

func isEqual(_ aValue: Any) -> Bool {
Expand All @@ -426,4 +426,3 @@ extension NSValue {
}
}
#endif

2 changes: 1 addition & 1 deletion Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
}
self.init(objects: buffer, count: array.count)
buffer.deinitialize(count: array.count)
buffer.deallocate(capacity: array.count)
buffer.deallocate()
}

public convenience init(set: Set<AnyHashable>) {
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
let characters = UnsafeMutablePointer<unichar>.allocate(capacity: length)
getCharacters(characters, range: NSMakeRange(0, length))
let result = NSMutableString(characters: characters, length: length)
characters.deinitialize()
characters.deallocate(capacity: length)
characters.deinitialize(count: 1)
characters.deallocate()
return result
}

Expand Down Expand Up @@ -364,8 +364,8 @@ extension NSString {
let buff = UnsafeMutablePointer<unichar>.allocate(capacity: range.length)
getCharacters(buff, range: range)
let result = String(describing: buff)
buff.deinitialize()
buff.deallocate(capacity: range.length)
buff.deinitialize(count: 1)
buff.deallocate()
return result
}
}
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSSwiftRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ extension Array {
let buffer = UnsafeMutablePointer<Element>.allocate(capacity: count)
let res = body(buffer)
buffer.deinitialize(count: count)
buffer.deallocate(capacity: count)
buffer.deallocate()
return res
} else {
return withUnsafeMutableBufferPointer() { (bufferPtr: inout UnsafeMutableBufferPointer<Element>) -> R in
Expand Down
Loading