diff --git a/stdlib/private/StdlibCollectionUnittest/CheckSequenceInstance.swift.gyb b/stdlib/private/StdlibCollectionUnittest/CheckSequenceInstance.swift.gyb index b2001374bc8..40fad2795f9 100644 --- a/stdlib/private/StdlibCollectionUnittest/CheckSequenceInstance.swift.gyb +++ b/stdlib/private/StdlibCollectionUnittest/CheckSequenceInstance.swift.gyb @@ -95,7 +95,7 @@ public func checkSequence< "_copyContents failed to use entire buffer") expectEqualSequence(expected, buf, ${trace}, sameValue: sameValue) ptr.deinitialize(count: count) - ptr.deallocate(capacity: count) + ptr.deallocate() } // Test `_copyToContiguousArray()` if we can do so @@ -124,4 +124,3 @@ public func checkSequence< } % end - diff --git a/stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift b/stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift index c1498ff89d6..b3ec50938c7 100644 --- a/stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift +++ b/stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift @@ -18,8 +18,8 @@ public func _opaqueIdentity(_ x: T) -> T { ptr.initialize(to: x) let result = UnsafeMutablePointer(_getPointer(OpaquePointer(ptr))).pointee - ptr.deinitialize() - ptr.deallocate(capacity: 1) + ptr.deinitialize(count: 1) + ptr.deallocate() return result } @@ -79,4 +79,3 @@ public func getFloat80(_ x: Float80) -> Float80 { return _opaqueIdentity(x) } public func getPointer(_ x: OpaquePointer) -> OpaquePointer { return _opaqueIdentity(x) } - diff --git a/stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift b/stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift index 08322da0168..45c38b5f5ac 100644 --- a/stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift +++ b/stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift @@ -43,7 +43,7 @@ public struct _stdlib_ShardedAtomicCounter { public func `deinit`() { self._shardsPtr.deinitialize(count: self._shardsCount) - self._shardsPtr.deallocate(capacity: self._shardsCount) + self._shardsPtr.deallocate() } public func add(_ operand: Int, randomInt: Int) { diff --git a/stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift b/stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift index 35f0ff12572..cea41ab0159 100644 --- a/stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift +++ b/stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift @@ -97,10 +97,10 @@ public func _stdlib_pthread_barrier_destroy( // FIXME: leaking memory. return -1 } - barrier.pointee.cond!.deinitialize() - barrier.pointee.cond!.deallocate(capacity: 1) - barrier.pointee.mutex!.deinitialize() - barrier.pointee.mutex!.deallocate(capacity: 1) + barrier.pointee.cond!.deinitialize(count: 1) + barrier.pointee.cond!.deallocate() + barrier.pointee.mutex!.deinitialize(count: 1) + barrier.pointee.mutex!.deallocate() return 0 } diff --git a/stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift b/stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift index 3a4592813ad..de8908a6ea2 100644 --- a/stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift +++ b/stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift @@ -107,8 +107,8 @@ public func _stdlib_pthread_join( let threadResultPtr = threadResultRawPtr!.assumingMemoryBound( to: Result.self) let threadResult = threadResultPtr.pointee - threadResultPtr.deinitialize() - threadResultPtr.deallocate(capacity: 1) + threadResultPtr.deinitialize(count: 1) + threadResultPtr.deallocate() return (result, threadResult) } else { return (result, nil) diff --git a/stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift b/stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift index 184c746d3a2..7661ed6516a 100644 --- a/stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift +++ b/stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift @@ -361,7 +361,7 @@ public func reflect(any: T) { anyPointer.initialize(to: any) let anyPointerValue = UInt(bitPattern: anyPointer) reflect(instanceAddress: anyPointerValue, kind: .Existential) - anyPointer.deallocate(capacity: MemoryLayout.size) + anyPointer.deallocate() } // Reflect an `Error`, a.k.a. an "error existential". @@ -431,7 +431,7 @@ public func reflect(function: @escaping () -> Void) { reflect(instanceAddress: contextPointer, kind: .Object) - fn.deallocate(capacity: MemoryLayout.size) + fn.deallocate() } /// Reflect a closure context. The given function must be a Swift-native @@ -449,7 +449,7 @@ public func reflect(function: @escaping (Int) -> Void) { reflect(instanceAddress: contextPointer, kind: .Object) - fn.deallocate(capacity: MemoryLayout.size) + fn.deallocate() } /// Reflect a closure context. The given function must be a Swift-native @@ -466,7 +466,7 @@ public func reflect(function: @escaping (Int, String) -> Void) { reflect(instanceAddress: contextPointer, kind: .Object) - fn.deallocate(capacity: MemoryLayout.size) + fn.deallocate() } /// Reflect a closure context. The given function must be a Swift-native @@ -483,7 +483,7 @@ public func reflect(function: @escaping (Int, String, AnyObject?) -> Void) { reflect(instanceAddress: contextPointer, kind: .Object) - fn.deallocate(capacity: MemoryLayout.size) + fn.deallocate() } /// Call this function to indicate to the parent that there are diff --git a/stdlib/public/SDK/Foundation/Data.swift b/stdlib/public/SDK/Foundation/Data.swift index 8083b9e4586..7523a299461 100644 --- a/stdlib/public/SDK/Foundation/Data.swift +++ b/stdlib/public/SDK/Foundation/Data.swift @@ -137,7 +137,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.alignment) defer { buffer.deallocate() } let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count) var enumerated = 0 @@ -168,7 +168,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.alignment) defer { buffer.deallocate() } let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count) var enumerated = 0 diff --git a/stdlib/public/SDK/Foundation/NSDictionary.swift b/stdlib/public/SDK/Foundation/NSDictionary.swift index 7ef65079777..a97969805cf 100644 --- a/stdlib/public/SDK/Foundation/NSDictionary.swift +++ b/stdlib/public/SDK/Foundation/NSDictionary.swift @@ -224,9 +224,9 @@ extension NSDictionary { // Allocate a buffer containing both the keys and values. let buffer = UnsafeMutableRawPointer.allocate( - bytes: totalSize, alignedTo: alignment) + byteCount: totalSize, alignment: alignment) defer { - buffer.deallocate(bytes: totalSize, alignedTo: alignment) + buffer.deallocate() _fixLifetime(otherDictionary) } diff --git a/stdlib/public/SDK/Foundation/NSSet.swift b/stdlib/public/SDK/Foundation/NSSet.swift index b17d7758f47..ddcdcd24487 100644 --- a/stdlib/public/SDK/Foundation/NSSet.swift +++ b/stdlib/public/SDK/Foundation/NSSet.swift @@ -177,9 +177,9 @@ extension NSSet { assert(alignment == MemoryLayout.alignment) let rawBuffer = UnsafeMutableRawPointer.allocate( - bytes: bufferSize, alignedTo: alignment) + byteCount: bufferSize, alignment: alignment) defer { - rawBuffer.deallocate(bytes: bufferSize, alignedTo: alignment) + rawBuffer.deallocate() _fixLifetime(anSet) } let valueBuffer = rawBuffer.bindMemory( diff --git a/stdlib/public/core/HashedCollections.swift.gyb b/stdlib/public/core/HashedCollections.swift.gyb index e71160af66a..9293c50a5fa 100644 --- a/stdlib/public/core/HashedCollections.swift.gyb +++ b/stdlib/public/core/HashedCollections.swift.gyb @@ -3315,7 +3315,7 @@ internal class _TypedNative${Self}Storage<${TypeParameters}> : if !_isPOD(Key.self) { for i in 0 ..< capacity { if initializedEntries[i] { - (keys+i).deinitialize() + (keys+i).deinitialize(count: 1) } } } @@ -3324,7 +3324,7 @@ internal class _TypedNative${Self}Storage<${TypeParameters}> : if !_isPOD(Value.self) { for i in 0 ..< capacity { if initializedEntries[i] { - (values+i).deinitialize() + (values+i).deinitialize(count: 1) } } } @@ -3729,9 +3729,9 @@ internal struct _Native${Self}Buffer<${TypeParameters}> { _sanityCheck(isInitializedEntry(at: i)) defer { _fixLifetime(self) } - (keys + i).deinitialize() + (keys + i).deinitialize(count: 1) %if Self == 'Dictionary': - (values + i).deinitialize() + (values + i).deinitialize(count: 1) %end _storage.initializedEntries[i] = false } diff --git a/stdlib/public/core/KeyPath.swift b/stdlib/public/core/KeyPath.swift index ec368314555..6582ca775b9 100644 --- a/stdlib/public/core/KeyPath.swift +++ b/stdlib/public/core/KeyPath.swift @@ -1526,7 +1526,7 @@ internal struct KeyPathBuffer { src: UnsafeMutableRawPointer(mutating: raw.baseAddress.unsafelyUnwrapped), size: UInt(MemoryLayout.size)) let result = resultBuf.pointee - resultBuf.deallocate(capacity: 1) + resultBuf.deallocate() return result } @_inlineable // FIXME(sil-serialize-all) diff --git a/stdlib/public/core/ManagedBuffer.swift b/stdlib/public/core/ManagedBuffer.swift index 54be014d1be..734245a9e36 100644 --- a/stdlib/public/core/ManagedBuffer.swift +++ b/stdlib/public/core/ManagedBuffer.swift @@ -162,7 +162,7 @@ open class ManagedBuffer { /// Manager(unsafeBufferObject: self).withUnsafeMutablePointers { /// (pointerToHeader, pointerToElements) -> Void in /// pointerToElements.deinitialize(count: self.count) -/// pointerToHeader.deinitialize() +/// pointerToHeader.deinitialize(count: 1) /// } /// } /// diff --git a/stdlib/public/core/Misc.swift b/stdlib/public/core/Misc.swift index f30f9755785..6ece5eb06d9 100644 --- a/stdlib/public/core/Misc.swift +++ b/stdlib/public/core/Misc.swift @@ -69,7 +69,7 @@ internal func _withUninitializedString( let stringPtr = UnsafeMutablePointer.allocate(capacity: 1) let bodyResult = body(stringPtr) let stringResult = stringPtr.move() - stringPtr.deallocate(capacity: 1) + stringPtr.deallocate() return (bodyResult, stringResult) } diff --git a/stdlib/public/core/StringCore.swift b/stdlib/public/core/StringCore.swift index 9fa65705d7c..ddd3d40b2c0 100644 --- a/stdlib/public/core/StringCore.swift +++ b/stdlib/public/core/StringCore.swift @@ -718,8 +718,8 @@ extension _StringCore : RangeReplaceableCollection { let tailStart = rangeStart + (replacedCount &<< elementShift) if growth > 0 { - (tailStart + (growth &<< elementShift)).copyBytes( - from: tailStart, count: tailCount &<< elementShift) + (tailStart + (growth &<< elementShift)).copyMemory( + from: tailStart, byteCount: tailCount &<< elementShift) } if _fastPath(elementWidth == 1) { @@ -738,8 +738,8 @@ extension _StringCore : RangeReplaceableCollection { } if growth < 0 { - (tailStart + (growth &<< elementShift)).copyBytes( - from: tailStart, count: tailCount &<< elementShift) + (tailStart + (growth &<< elementShift)).copyMemory( + from: tailStart, byteCount: tailCount &<< elementShift) } } else { diff --git a/stdlib/public/core/SwiftNativeNSArray.swift b/stdlib/public/core/SwiftNativeNSArray.swift index 4e5cf43a5ca..aa57fcb3f58 100644 --- a/stdlib/public/core/SwiftNativeNSArray.swift +++ b/stdlib/public/core/SwiftNativeNSArray.swift @@ -101,9 +101,9 @@ extension _SwiftNativeNSArrayWithContiguousStorage : _NSArrayCore { // These objects are "returned" at +0, so treat them as pointer values to // avoid retains. Copy bytes via a raw pointer to circumvent reference // counting while correctly aliasing with all other pointer types. - UnsafeMutableRawPointer(aBuffer).copyBytes( + UnsafeMutableRawPointer(aBuffer).copyMemory( from: objects.baseAddress! + range.location, - count: range.length * MemoryLayout.stride) + byteCount: range.length * MemoryLayout.stride) } } diff --git a/stdlib/public/core/ThreadLocalStorage.swift b/stdlib/public/core/ThreadLocalStorage.swift index 03e461701b4..0f4818c195b 100644 --- a/stdlib/public/core/ThreadLocalStorage.swift +++ b/stdlib/public/core/ThreadLocalStorage.swift @@ -121,7 +121,7 @@ internal func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) { let tlsPtr = ptr!.assumingMemoryBound(to: _ThreadLocalStorage.self) __swift_stdlib_ubrk_close(tlsPtr[0].uBreakIterator) tlsPtr.deinitialize(count: 1) - tlsPtr.deallocate(capacity: 1) + tlsPtr.deallocate() #if INTERNAL_CHECKS_ENABLED // Log the fact we've destroyed our storage @@ -167,4 +167,3 @@ internal func _initializeThreadLocalStorage() _sanityCheck(success == 0, "setspecific failed") return tlsPtr } - diff --git a/stdlib/public/core/UnsafeBitMap.swift b/stdlib/public/core/UnsafeBitMap.swift index 8afe7b63c96..469d56a450e 100644 --- a/stdlib/public/core/UnsafeBitMap.swift +++ b/stdlib/public/core/UnsafeBitMap.swift @@ -58,7 +58,7 @@ struct _UnsafeBitMap { @_inlineable // FIXME(sil-serialize-all) public // @testable func initializeToZero() { - values.initialize(to: 0, count: numberOfWords) + values.initialize(repeating: 0, count: numberOfWords) } @_inlineable // FIXME(sil-serialize-all) @@ -82,4 +82,3 @@ struct _UnsafeBitMap { } } } - diff --git a/stdlib/public/core/UnsafeBufferPointer.swift.gyb b/stdlib/public/core/UnsafeBufferPointer.swift.gyb index 0541c78eb9a..880d2aa3599 100644 --- a/stdlib/public/core/UnsafeBufferPointer.swift.gyb +++ b/stdlib/public/core/UnsafeBufferPointer.swift.gyb @@ -303,8 +303,35 @@ public struct Unsafe${Mutable}BufferPointer _position = Unsafe${Mutable}Pointer._max _end = _position } + +% if Mutable: + + /// Creates a mutable typed buffer pointer referencing the same memory as the + /// given immutable buffer pointer. + /// + /// - Parameter other: The immutable buffer pointer to convert. + @_inlineable + public init(mutating other: UnsafeBufferPointer) { + _position = UnsafeMutablePointer(mutating: other._position) + _end = UnsafeMutablePointer(mutating: other._end) + } + +% else: + + /// Creates an immutable typed buffer pointer referencing the same memory as the + /// given mutable buffer pointer. + /// + /// - Parameter other: The mutable buffer pointer to convert. + @_inlineable + public init(_ other: UnsafeMutableBufferPointer) { + _position = UnsafePointer(other._position) + _end = UnsafePointer(other._end) + } + +% end % if not Mutable: + /// Creates a buffer over the same memory as the given buffer slice. /// /// The new buffer represents the same region of memory as `slice`, but is @@ -329,7 +356,8 @@ public struct Unsafe${Mutable}BufferPointer self.init(start: slice.base.baseAddress! + slice.startIndex, count: slice.count) } -% end # !mutable + +% end /// Creates a buffer over the same memory as the given buffer slice. /// @@ -366,6 +394,143 @@ public struct Unsafe${Mutable}BufferPointer public func makeIterator() -> UnsafeBufferPointerIterator { return UnsafeBufferPointerIterator(_position: _position, _end: _end) } + + /// Deallocates the memory block previously allocated at this buffer pointer’s + /// base address. + /// + /// This buffer pointer's `baseAddress` must be `nil` or a pointer to a memory + /// block previously returned by a Swift allocation method. If `baseAddress` is + /// `nil`, this function does nothing. Otherwise, the memory must not be initialized + /// or `Pointee` must be a trivial type. This buffer pointer's `count` must + /// be equal to the originally allocated size of the memory block. + @_inlineable + public func deallocate() { + _position?.deallocate() + } + +% if Mutable: + + /// Allocates uninitialized memory for the specified number of instances of + /// type `Element`. + /// + /// The resulting buffer references a region of memory that is bound to + /// `Element` and is `count * MemoryLayout.stride` bytes in size. + /// + /// The following example allocates a buffer that can store four `Int` + /// instances and then initializes that memory with the elements of a range: + /// + /// let buffer = UnsafeMutableBufferPointer.allocate(capacity: 4) + /// _ = buffer.initialize(from: 1...4) + /// print(buffer[2]) + /// // Prints "3" + /// + /// When you allocate memory, always remember to deallocate once you're + /// finished. + /// + /// buffer.deallocate() + /// + /// - Parameter count: The amount of memory to allocate, counted in instances + /// of `Element`. + @_inlineable + public static func allocate(capacity count: Int) + -> UnsafeMutableBufferPointer { + let size = MemoryLayout.stride * count + let raw = Builtin.allocRaw(size._builtinWordValue, Builtin.alignof(Element.self)) + Builtin.bindMemory(raw, count._builtinWordValue, Element.self) + return UnsafeMutableBufferPointer( + start: UnsafeMutablePointer(raw), count: count) + } + + /// Initializes every element in this buffer's memory to a copy of the given value. + /// + /// The destination memory must be uninitialized or the buffer's `Element` + /// must be a trivial type. After a call to `initialize(repeating:)`, the + /// entire region of memory referenced by this buffer is initialized. + /// + /// - Parameters: + /// - repeatedValue: The instance to initialize this buffer's memory with. + @_inlineable + public func initialize(repeating repeatedValue: Element) { + guard let dstBase = _position else { + return + } + + dstBase.initialize(repeating: repeatedValue, count: _end! - dstBase) + } + + /// Assigns every element in this buffer's memory to a copy of the given value. + /// + /// The buffer’s memory must be initialized or the buffer's `Element` + /// must be a trivial type. + /// + /// - Parameters: + /// - repeatedValue: The instance to assign this buffer's memory to. + /// + /// Warning: All buffer elements must be initialized before calling this. + /// Assigning to part of the buffer must be done using the `assign(repeating:count:)`` + /// method on the buffer’s `baseAddress`. + @_inlineable + public func assign(repeating repeatedValue: Element) { + guard let dstBase = _position else { + return + } + + dstBase.assign(repeating: repeatedValue, count: _end! - dstBase) + } + +% end + + /// Executes the given closure while temporarily binding the memory referenced + /// by this buffer to the given type. + /// + /// Use this method when you have a buffer of memory bound to one type and + /// you need to access that memory as a buffer of another type. Accessing + /// memory as type `T` requires that the memory be bound to that type. A + /// memory location may only be bound to one type at a time, so accessing + /// the same memory as an unrelated type without first rebinding the memory + /// is undefined. + /// + /// The entire region of memory referenced by this buffer must be initialized. + /// + /// Because this buffer's memory is no longer bound to its `Element` type + /// while the `body` closure executes, do not access memory using the + /// original buffer from within `body`. Instead, use the `body` closure's + /// buffer argument to access the values in memory as instances of type + /// `T`. + /// + /// After executing `body`, this method rebinds memory back to the original + /// `Element` type. + /// + /// - Parameters: + /// - type: The type to temporarily bind the memory referenced by this + /// buffer. The type `T` must have the same stride and be layout compatible + /// with the pointer's `Element` type. + /// - body: A closure that takes a ${Mutable.lower()} typed buffer to the + /// same memory as this buffer, only bound to type `T`. The buffer argument + /// contains the same number of complete instances of `T` as the original + /// buffer’s `count`. The closure's buffer argument is valid only for the + /// duration of the closure's execution. If `body` has a return value, that + /// value is also used as the return value for the `withMemoryRebound(to:_:)` + /// method. + /// - Returns: The return value, if any, of the `body` closure parameter. + @_inlineable + public func withMemoryRebound( + to type: T.Type, _ body: (${Self}) throws -> Result + ) rethrows -> Result { + if let base = _position { + _debugPrecondition(MemoryLayout.stride == MemoryLayout.stride) + Builtin.bindMemory(base._rawValue, count._builtinWordValue, T.self) + defer { + Builtin.bindMemory(base._rawValue, count._builtinWordValue, Element.self) + } + + return try body(${Self}( + start: Unsafe${Mutable}Pointer(base._rawValue), count: count)) + } + else { + return try body(${Self}(start: nil, count: 0)) + } + } /// A pointer to the first element of the buffer. /// @@ -382,12 +547,11 @@ public struct Unsafe${Mutable}BufferPointer /// a buffer can have a `count` of zero even with a non-`nil` base address. @_inlineable public var count: Int { - switch(_position, _end) { - case (.some(let pos), .some(let end)): - return (end - pos) - case _: + guard let start = _position, let end = _end else { return 0 } + + return end - start } @_versioned diff --git a/stdlib/public/core/UnsafePointer.swift.gyb b/stdlib/public/core/UnsafePointer.swift.gyb index 61382de9efb..bb7aeb9614b 100644 --- a/stdlib/public/core/UnsafePointer.swift.gyb +++ b/stdlib/public/core/UnsafePointer.swift.gyb @@ -412,8 +412,6 @@ public struct ${Self} /// /// The resulting pointer references a region of memory that is bound to /// `Pointee` and is `count * MemoryLayout.stride` bytes in size. - /// You must eventually deallocate the memory referenced by the returned - /// pointer. /// /// The following example allocates enough new memory to store four `Int` /// instances and then initializes that memory with the elements of a range. @@ -426,7 +424,7 @@ public struct ${Self} /// When you allocate memory, always remember to deallocate once you're /// finished. /// - /// intPointer.deallocate(capacity: 4) + /// intPointer.deallocate() /// /// - Parameter count: The amount of memory to allocate, counted in instances /// of `Pointee`. @@ -439,23 +437,23 @@ public struct ${Self} Builtin.bindMemory(rawPtr, count._builtinWordValue, Pointee.self) return UnsafeMutablePointer(rawPtr) } - - /// Deallocates memory that was allocated for `count` instances of `Pointee`. - /// - /// The memory region that is deallocated is - /// `capacity * MemoryLayout.stride` bytes in size. The memory must - /// not be initialized or `Pointee` must be a trivial type. - /// - /// - Parameter capacity: The amount of memory to deallocate, counted in - /// instances of `Pointee`. - @_inlineable - public func deallocate(capacity: Int) { - let size = MemoryLayout.stride * capacity - Builtin.deallocRaw( - _rawValue, size._builtinWordValue, Builtin.alignof(Pointee.self)) + + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, message: "Swift currently only supports freeing entire heap blocks, use deallocate() instead") + public func deallocate(capacity _: Int) { + self.deallocate() } + % end + /// Deallocates the memory block previously allocated at this pointer. + /// + /// This pointer must be a pointer to the start of a previously allocated memory + /// block. The memory must not be initialized or `Pointee` must be a trivial type. + @_inlineable + public func deallocate() { + Builtin.deallocRaw(_rawValue, (-1)._builtinWordValue, (-1)._builtinWordValue) + } + /// Accesses the instance referenced by this pointer. /// % if mutable: @@ -488,28 +486,48 @@ public struct ${Self} } % if mutable: + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, renamed: "initialize(repeating:count:)") + public func initialize(to newValue: Pointee, count: Int = 1) { + initialize(repeating: newValue, count: count) + } + /// Initializes this pointer's memory with the specified number of /// consecutive copies of the given value. /// /// The destination memory must be uninitialized or the pointer's `Pointee` - /// must be a trivial type. After a call to `initialize(to:count:)`, the + /// must be a trivial type. After a call to `initialize(repeating:count:)`, the /// memory referenced by this pointer is initialized. /// /// - Parameters: - /// - newValue: The instance to initialize this pointer's memory with. + /// - repeatedValue: The instance to initialize this pointer's memory with. /// - count: The number of consecutive copies of `newValue` to initialize. - /// `count` must not be negative. The default is `1`. + /// `count` must not be negative. @_inlineable - public func initialize(to newValue: Pointee, count: Int = 1) { + public func initialize(repeating repeatedValue: Pointee, count: Int) { // FIXME: add tests (since the `count` has been added) _debugPrecondition(count >= 0, - "${Self}.initialize(to:): negative count") + "${Self}.initialize(repeating:count:): negative count") // Must not use `initializeFrom` with a `Collection` as that will introduce // a cycle. for offset in 0.. /// incidental side effects of copying and destroying the value: /// /// let value: T = { - /// defer { p.deinitialize() } + /// defer { p.deinitialize(count: 1) } /// return p.pointee /// }() /// @@ -532,6 +550,26 @@ public struct ${Self} return Builtin.take(_rawValue) } + /// Replaces this pointer's memory with the specified number of + /// consecutive copies of the given value. + /// + /// The region of memory starting at this pointer and covering `count` + /// instances of the pointer's `Pointee` type must be initialized or + /// `Pointee` must be a trivial type. After calling + /// `assign(repeating:count:)`, the region is initialized. + /// + /// - Parameters: + /// - repeatedValue: The instance to assign this pointer's memory to. + /// - count: The number of consecutive copies of `newValue` to assign. + /// `count` must not be negative. + @_inlineable + public func assign(repeating repeatedValue: Pointee, count: Int) { + _debugPrecondition(count >= 0, "${Self}.assign(repeating:count:) with negative count") + for i in 0.. // self[i] = (source + i).move() // } } - + + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, message: "the default argument to deinitialize(count:) has been removed, please specify the count explicitly") + @_inlineable + @discardableResult + public func deinitialize() -> UnsafeMutableRawPointer { + return deinitialize(count: 1) + } + /// Deinitializes the specified number of values starting at this pointer. /// /// The region of memory starting at this pointer and covering `count` @@ -704,12 +749,12 @@ public struct ${Self} /// bound to the `Pointee` type. /// /// - Parameter count: The number of instances to deinitialize. `count` must - /// not be negative. The default value is `1`. + /// not be negative. /// - Returns: A raw pointer to the same address as this pointer. The memory /// referenced by the returned raw pointer is still bound to `Pointee`. @_inlineable @discardableResult - public func deinitialize(count: Int = 1) -> UnsafeMutableRawPointer { + public func deinitialize(count: Int) -> UnsafeMutableRawPointer { _debugPrecondition(count >= 0, "${Self}.deinitialize with negative count") // FIXME: optimization should be implemented, where if the `count` value // is 1, the `Builtin.destroy(Pointee.self, _rawValue)` gets called. diff --git a/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb b/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb index 50f74569185..b890e960ffd 100644 --- a/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb +++ b/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb @@ -36,7 +36,7 @@ /// /// - `load(fromByteOffset:as:)` /// - `storeBytes(of:toByteOffset:as:)` -/// - `copyBytes(from:count:)` +/// - `copyMemory(from:)` % else: /// An `${Self}` instance is a view of the raw bytes in a region of memory. /// Each byte in memory is viewed as a `UInt8` value independent of the type @@ -134,32 +134,44 @@ public struct Unsafe${Mutable}RawBufferPointer } % if mutable: + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, renamed: "allocate(byteCount:alignment:)") + public static func allocate(count: Int) -> UnsafeMutableRawBufferPointer { + return UnsafeMutableRawBufferPointer.allocate( + byteCount: count, alignment: MemoryLayout.alignment) + } + /// Returns a newly allocated buffer with the given size, in bytes. /// /// The memory referenced by the new buffer is allocated, but not /// initialized. /// - /// - Parameter size: The number of bytes to allocate. - /// - Returns: A word-aligned buffer pointer covering a region of memory. + /// - Parameters: + /// - byteCount: The number of bytes to allocate. + /// - alignment: The alignment of the new region of allocated memory, in + /// bytes. + /// - Returns: A buffer pointer to a newly allocated region of memory aligned + /// to `alignment`. @_inlineable - public static func allocate(count size: Int + public static func allocate( + byteCount: Int, alignment: Int ) -> UnsafeMutableRawBufferPointer { - return UnsafeMutableRawBufferPointer( - start: UnsafeMutableRawPointer.allocate( - bytes: size, alignedTo: MemoryLayout.alignment), - count: size) + let base = UnsafeMutableRawPointer.allocate( + byteCount: byteCount, alignment: alignment) + return UnsafeMutableRawBufferPointer(start: base, count: byteCount) } % end # mutable - /// Deallocates the memory viewed by this buffer pointer. + /// Deallocates the memory block previously allocated at this buffer pointer’s + /// base address. /// - /// The memory to be deallocated must not be initialized or must be - /// initialized to a trivial type. For a buffer pointer `p`, all `p.count` - /// bytes referenced by `p` are deallocated. + /// This buffer pointer's `baseAddress` must be `nil` or a pointer to a memory + /// block previously returned by a Swift allocation method. If `baseAddress` is + /// `nil`, this function does nothing. Otherwise, the memory must not be initialized + /// or `Pointee` must be a trivial type. This buffer pointer's byte `count` must + /// be equal to the originally allocated size of the memory block. @_inlineable public func deallocate() { - _position?.deallocate( - bytes: count, alignedTo: MemoryLayout.alignment) + _position?.deallocate() } /// Returns a new instance of the given type, read from the buffer pointer's @@ -233,6 +245,10 @@ public struct Unsafe${Mutable}RawBufferPointer baseAddress!.storeBytes(of: value, toByteOffset: offset, as: T.self) } + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, renamed: "copyMemory(from:)") + public func copyBytes(from source: UnsafeRawBufferPointer) { + copyMemory(from: source) + } /// Copies the bytes from the given buffer to this buffer's memory. /// /// If the `source.count` bytes of memory referenced by this buffer are bound @@ -240,17 +256,17 @@ public struct Unsafe${Mutable}RawBufferPointer /// must be properly aligned for accessing `T`, and `source.count` must be a /// multiple of `MemoryLayout.stride`. /// - /// After calling `copyBytes(from:)`, the first `source.count` bytes of + /// After calling `copyMemory(from:)`, the first `source.count` bytes of /// memory referenced by this buffer are initialized to raw bytes. If the /// memory is bound to type `T`, then it contains values of type `T`. /// /// - Parameter source: A buffer of raw bytes from which to copy. /// `source.count` must be less than or equal to this buffer's `count`. @_inlineable - public func copyBytes(from source: UnsafeRawBufferPointer) { + public func copyMemory(from source: UnsafeRawBufferPointer) { _debugPrecondition(source.count <= self.count, - "${Self}.copyBytes source has too many elements") - baseAddress?.copyBytes(from: source.baseAddress!, count: source.count) + "${Self}.copyMemory source has too many elements") + baseAddress?.copyMemory(from: source.baseAddress!, byteCount: source.count) } /// Copies from a collection of `UInt8` into this buffer's memory. @@ -468,9 +484,9 @@ public struct Unsafe${Mutable}RawBufferPointer _debugPrecondition(bounds.count == newValue.count) if !newValue.isEmpty { - (baseAddress! + bounds.lowerBound).copyBytes( + (baseAddress! + bounds.lowerBound).copyMemory( from: newValue.base.baseAddress! + newValue.startIndex, - count: newValue.count) + byteCount: newValue.count) } } % end # mutable @@ -504,6 +520,42 @@ public struct Unsafe${Mutable}RawBufferPointer } % if mutable: + + /// Initializes the memory referenced by this buffer with the given value, + /// binds the memory to the value's type, and returns a typed buffer of the + /// initialized memory. + /// + /// The memory referenced by this buffer must be uninitialized or + /// initialized to a trivial type, and must be properly aligned for + /// accessing `T`. + /// + /// After calling this method on a raw buffer with non-nil `baseAddress` `b`, + /// the region starting at `b` and continuing up to + /// `b + self.count - self.count % MemoryLayout.stride` is bound to type `T` and + /// initialized. If `T` is a nontrivial type, you must eventually deinitialize + /// or move the values in this region to avoid leaks. If `baseAddress` is + /// `nil`, this function does nothing and returns an empty buffer pointer. + /// + /// - Parameters: + /// - type: The type to bind this buffer’s memory to. + /// - repeatedValue: The instance to copy into memory. + /// - Returns: A typed buffer of the memory referenced by this raw buffer. + /// The typed buffer contains `self.count / MemoryLayout.stride` + /// instances of `T`. + @_inlineable + @discardableResult + public func initializeMemory(as type: T.Type, repeating repeatedValue: T) + -> UnsafeMutableBufferPointer { + guard let base = _position else { + return UnsafeMutableBufferPointer(start: nil, count: 0) + } + + let count = (_end! - base) / MemoryLayout.stride + let typed = base.initializeMemory( + as: type, repeating: repeatedValue, count: count) + return UnsafeMutableBufferPointer(start: typed, count: count) + } + /// Initializes the buffer's memory with the given elements, binding the /// initialized memory to the elements' type. /// @@ -555,7 +607,7 @@ public struct Unsafe${Mutable}RawBufferPointer // underflow is permitted -- e.g. a sequence into // the spare capacity of an Array buffer guard let x = it.next() else { break } - p.initializeMemory(as: S.Element.self, to: x) + p.initializeMemory(as: S.Element.self, repeating: x, count: 1) formIndex(&idx, offsetBy: elementStride) } @@ -565,6 +617,39 @@ public struct Unsafe${Mutable}RawBufferPointer } % end # mutable + /// Binds this buffer’s memory to the specified type and returns a typed buffer + /// of the bound memory. + /// + /// Use the `bindMemory(to:)` method to bind the memory referenced + /// by this buffer to the type `T`. The memory must be uninitialized or + /// initialized to a type that is layout compatible with `T`. If the memory + /// is uninitialized, it is still uninitialized after being bound to `T`. + /// + /// - Warning: A memory location may only be bound to one type at a time. The + /// behavior of accessing memory as a type unrelated to its bound type is + /// undefined. + /// + /// - Parameters: + /// - type: The type `T` to bind the memory to. + /// - Returns: A typed buffer of the newly bound memory. The memory in this + /// region is bound to `T`, but has not been modified in any other way. + /// The typed buffer references `self.count / MemoryLayout.stride` instances of `T`. + @_inlineable // FIXME(sil-serialize-all) + @_transparent + @discardableResult + public func bindMemory( + to type: T.Type + ) -> Unsafe${Mutable}BufferPointer { + guard let base = _position else { + return Unsafe${Mutable}BufferPointer(start: nil, count: 0) + } + + let capacity = count / MemoryLayout.stride + Builtin.bindMemory(base._rawValue, capacity._builtinWordValue, type) + return Unsafe${Mutable}BufferPointer( + start: Unsafe${Mutable}Pointer(base._rawValue), count: capacity) + } + @_versioned internal let _position, _end: Unsafe${Mutable}RawPointer? } diff --git a/stdlib/public/core/UnsafeRawPointer.swift.gyb b/stdlib/public/core/UnsafeRawPointer.swift.gyb index 859e72e546d..d8d3ed03165 100644 --- a/stdlib/public/core/UnsafeRawPointer.swift.gyb +++ b/stdlib/public/core/UnsafeRawPointer.swift.gyb @@ -99,7 +99,7 @@ /// offset by that number of bytes. The following example allocates four bytes /// of memory and stores `0xFF` in all four bytes: /// -/// let bytesPointer = UnsafeMutableRawPointer.allocate(bytes: 4, alignedTo: 1) +/// let bytesPointer = UnsafeMutableRawPointer.allocate(byteCount: 4, alignment: 1) /// bytesPointer.storeBytes(of: 0xFFFF_FFFF, as: UInt32.self) /// /// // Load a value from the memory referenced by 'bytesPointer' @@ -115,7 +115,7 @@ /// /// Always remember to deallocate any memory that you allocate yourself. /// -/// bytesPointer.deallocate(bytes: 4, alignedTo: 1) +/// bytesPointer.deallocate() /// /// Implicit Casting and Bridging /// ============================= @@ -426,6 +426,15 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer { % end # !mutable % if mutable: + + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, renamed: "allocate(byteCount:alignment:)") + @_inlineable + public static func allocate( + bytes size: Int, alignedTo alignment: Int + ) -> UnsafeMutableRawPointer { + return UnsafeMutableRawPointer.allocate(byteCount: size, alignment: alignment) + } + /// Allocates uninitialized memory with the specified size and alignment. /// /// You are in charge of managing the allocated memory. Be sure to deallocate @@ -437,33 +446,32 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer { /// `UnsafeMutablePointer.allocate(capacity:)` static method instead. /// /// - Parameters: - /// - size: The number of bytes to allocate. `size` must not be negative. - /// - alignedTo: The alignment of the new region of allocated memory, in + /// - byteCount: The number of bytes to allocate. `byteCount` must not be negative. + /// - alignment: The alignment of the new region of allocated memory, in /// bytes. /// - Returns: A pointer to a newly allocated region of memory. The memory is /// allocated, but not initialized. @_inlineable public static func allocate( - bytes size: Int, alignedTo: Int + byteCount: Int, alignment: Int ) -> UnsafeMutableRawPointer { return UnsafeMutableRawPointer(Builtin.allocRaw( - size._builtinWordValue, alignedTo._builtinWordValue)) + byteCount._builtinWordValue, alignment._builtinWordValue)) } % end # mutable - /// Deallocates memory referenced by the pointer with the specified size and - /// alignment. + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, message: "Swift currently only supports freeing entire heap blocks, use deallocate() instead") + public func deallocate(bytes _: Int, alignedTo _: Int) { + self.deallocate() + } + + /// Deallocates the previously allocated memory block referenced by this pointer. /// /// The memory to be deallocated must be uninitialized or initialized to a /// trivial type. - /// - /// - Parameters: - /// - size: The number of bytes to deallocate. - /// - alignedTo: The alignment of the region to be deallocated, in bytes. @_inlineable - public func deallocate(bytes size: Int, alignedTo: Int) { - Builtin.deallocRaw( - _rawValue, size._builtinWordValue, alignedTo._builtinWordValue) + public func deallocate() { + Builtin.deallocRaw(_rawValue, (-1)._builtinWordValue, (-1)._builtinWordValue) } /// Binds the memory to the specified type and returns a typed pointer to the @@ -530,6 +538,16 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer { } % if mutable: + + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, renamed: "initializeMemory(as:repeating:count:)") + @discardableResult + public func initializeMemory( + as type: T.Type, at offset: Int = 0, count: Int = 1, to repeatedValue: T + ) -> UnsafeMutablePointer { + return (self + offset * MemoryLayout.stride).initializeMemory( + as: type, repeating: repeatedValue, count: count) + } + /// Initializes the memory referenced by this pointer with the given value, /// binds the memory to the value's type, and returns a typed pointer to the /// initialized memory. @@ -539,49 +557,42 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer { /// accessing `T`. /// /// The following example allocates enough raw memory to hold four instances - /// of `Int8`, and then uses the `initializeMemory(as:at:count:to:)` method + /// of `Int8`, and then uses the `initializeMemory(as:repeating:count:)` method /// to initialize the allocated memory. /// /// let count = 4 /// let bytesPointer = UnsafeMutableRawPointer.allocate( - /// bytes: count * MemoryLayout.stride, - /// alignedTo: MemoryLayout.alignment) + /// byteCount: count * MemoryLayout.stride, + /// alignment: MemoryLayout.alignment) /// let int8Pointer = myBytes.initializeMemory( - /// as: Int8.self, count: count, value: 0) + /// as: Int8.self, repeating: 0, count: count) /// /// // After using 'int8Pointer': - /// int8Pointer.deallocate(count) + /// int8Pointer.deallocate() /// /// After calling this method on a raw pointer `p`, the region starting at - /// `p + index * MemoryLayout.stride` and continuing up to - /// `p + (index + count) * MemoryLayout.stride` is bound to type `T` and - /// initialized. If `T` is a nontrivial type, you must eventually deinitialize - /// or move from the values in this region to avoid leaks. + /// `self` and continuing up to `p + count * MemoryLayout.stride` is bound + /// to type `T` and initialized. If `T` is a nontrivial type, you must + /// eventually deinitialize or move from the values in this region to avoid leaks. /// /// - Parameters: /// - type: The type to bind this memory to. - /// - index: The offset from this pointer to the region of memory to be - /// initialized with `value`, in the stride of type `T`. `index` must - /// not be negative. The default is zero. + /// - repeatedValue: The instance to copy into memory. /// - count: The number of copies of `value` to copy into memory. `count` - /// must not be negative. The default is `1`. - /// - value: The instance to copy into memory. - /// - Returns: A typed pointer to the memory referenced by this raw pointer, - /// offset by `index * MemoryLayout.stride` bytes. + /// must not be negative. + /// - Returns: A typed pointer to the memory referenced by this raw pointer. @_inlineable @discardableResult - public func initializeMemory(as type: T.Type, at index: Int = 0, - count: Int = 1, to value: T + public func initializeMemory( + as type: T.Type, repeating repeatedValue: T, count: Int ) -> UnsafeMutablePointer { - _debugPrecondition(index >= 0, - "UnsafeMutableRawPointer.initializeMemory: negative index") _debugPrecondition(count >= 0, "UnsafeMutableRawPointer.initializeMemory: negative count") Builtin.bindMemory(_rawValue, count._builtinWordValue, type) - var nextPtr = self + index &* MemoryLayout.stride + var nextPtr = self for _ in 0...stride } return UnsafeMutablePointer(_rawValue) @@ -702,8 +713,10 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer { ) -> UnsafeMutablePointer { // TODO: Optimize where `C` is a `ContiguousArrayBuffer`. // Initialize and bind each element of the container. - for (index, element) in source.enumerated() { - self.initializeMemory(as: C.Element.self, at: index, to: element) + var ptr = self + for element in source { + ptr.initializeMemory(as: C.Element.self, repeating: element, count: 1) + ptr += MemoryLayout.stride } return UnsafeMutablePointer(_rawValue) } @@ -852,29 +865,34 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer { } } + @available(swift, deprecated: 4.1, obsoleted: 5.0.0, renamed: "copyMemory(from:byteCount:)") + public func copyBytes(from source: UnsafeRawPointer, count: Int) { + copyMemory(from: source, byteCount: count) + } + /// Copies the specified number of bytes from the given raw pointer's memory /// into this pointer's memory. /// - /// If the `count` bytes of memory referenced by this pointer are bound to a - /// type `T`, then `T` must be a trivial type, this pointer and `source` - /// must be properly aligned for accessing `T`, and `count` must be a + /// If the `byteCount` bytes of memory referenced by this pointer are bound to + /// a type `T`, then `T` must be a trivial type, this pointer and `source` + /// must be properly aligned for accessing `T`, and `byteCount` must be a /// multiple of `MemoryLayout.stride`. /// - /// After calling `copyBytes(from:count:)`, the `count` bytes of memory + /// After calling `copyMemory(from:byteCount:)`, the `byteCount` bytes of memory /// referenced by this pointer are initialized to raw bytes. If the memory /// is bound to type `T`, then it contains values of type `T`. /// /// - Parameters: /// - source: A pointer to the memory to copy bytes from. The memory in the - /// region `source..<(source + count)` must be initialized to a trivial + /// region `source..<(source + byteCount)` must be initialized to a trivial /// type. - /// - count: The number of bytes to copy. `count` must not be negative. + /// - byteCount: The number of bytes to copy. `byteCount` must not be negative. @_inlineable - public func copyBytes(from source: UnsafeRawPointer, count: Int) { + public func copyMemory(from source: UnsafeRawPointer, byteCount: Int) { _debugPrecondition( - count >= 0, "UnsafeMutableRawPointer.copyBytes with negative count") + byteCount >= 0, "UnsafeMutableRawPointer.copyMemory with negative count") - _memmove(dest: self, src: source, size: UInt(count)) + _memmove(dest: self, src: source, size: UInt(byteCount)) } % end # mutable diff --git a/test/Generics/slice_test.swift b/test/Generics/slice_test.swift index 27e1ba23b03..2aa6397fb3a 100644 --- a/test/Generics/slice_test.swift +++ b/test/Generics/slice_test.swift @@ -82,9 +82,7 @@ class Vector { } deinit { - for i in 0.. { self.ptr = ptr } deinit { - ptr.deinitialize() + ptr.deinitialize(count: 1) } } diff --git a/test/Prototypes/CollectionTransformers.swift b/test/Prototypes/CollectionTransformers.swift index 1d2b0a11fce..55f5b5c44f2 100644 --- a/test/Prototypes/CollectionTransformers.swift +++ b/test/Prototypes/CollectionTransformers.swift @@ -231,8 +231,8 @@ struct _ForkJoinMutex { if pthread_mutex_destroy(_mutex) != 0 { fatalError("pthread_mutex_init") } - _mutex.deinitialize() - _mutex.deallocate(capacity: 1) + _mutex.deinitialize(count: 1) + _mutex.deallocate() } func withLock(_ body: () -> Result) -> Result { @@ -261,8 +261,8 @@ struct _ForkJoinCond { if pthread_cond_destroy(_cond) != 0 { fatalError("pthread_cond_destroy") } - _cond.deinitialize() - _cond.deallocate(capacity: 1) + _cond.deinitialize(count: 1) + _cond.deallocate() } func signal() { @@ -1448,4 +1448,3 @@ http://habrahabr.ru/post/255659/ */ runAllTests() - diff --git a/test/Sanitizers/asan.swift b/test/Sanitizers/asan.swift index cafb90e802c..03af1e8a17d 100644 --- a/test/Sanitizers/asan.swift +++ b/test/Sanitizers/asan.swift @@ -28,9 +28,9 @@ public func call_foobar() { var a = UnsafeMutablePointer.allocate(capacity: 1) a.initialize(to: 5) a.deinitialize(count: 1) -a.deallocate(capacity: 1) +a.deallocate() print(a.pointee) a.deinitialize(count: 1) -a.deallocate(capacity: 1) +a.deallocate() // CHECK: AddressSanitizer: heap-use-after-free diff --git a/test/stdlib/Builtins.swift b/test/stdlib/Builtins.swift index da6a4c12827..cbc8919410d 100644 --- a/test/stdlib/Builtins.swift +++ b/test/stdlib/Builtins.swift @@ -163,7 +163,7 @@ func exerciseArrayValueWitnesses(_ value: T) { Builtin.takeArrayFrontToBack(T.self, buf._rawValue, (buf + 1)._rawValue, 4._builtinWordValue) Builtin.destroyArray(T.self, buf._rawValue, 4._builtinWordValue) - buf.deallocate(capacity: 5) + buf.deallocate() } tests.test("array value witnesses") { diff --git a/test/stdlib/KeyPathImplementation.swift b/test/stdlib/KeyPathImplementation.swift index 8151dc144aa..5877cc9a042 100644 --- a/test/stdlib/KeyPathImplementation.swift +++ b/test/stdlib/KeyPathImplementation.swift @@ -243,7 +243,7 @@ struct TestKeyPathBuilder { pushWord(args.count) pushWord(witnesses) - buffer.copyBytes(from: args) + buffer.copyMemory(from: args) buffer = .init(start: buffer.baseAddress! + args.count, count: buffer.count - args.count) @@ -952,7 +952,7 @@ func testCopy(from: UnsafeRawPointer, to: UnsafeMutableRawPointer, count: Int) { numberOfCopyOperations += 1 - to.copyBytes(from: from, count: count) + to.copyMemory(from: from, byteCount: count) } var numberOfEqualsOperations = 0 diff --git a/test/stdlib/ManagedBuffer.swift b/test/stdlib/ManagedBuffer.swift index e52f353384a..24d288c510a 100644 --- a/test/stdlib/ManagedBuffer.swift +++ b/test/stdlib/ManagedBuffer.swift @@ -81,7 +81,7 @@ final class TestManagedBuffer : ManagedBuffer { withUnsafeMutablePointerToElements { (x: UnsafeMutablePointer) -> () in for i in stride(from: 0, to: count, by: 2) { - (x + i).deinitialize() + (x + i).deinitialize(count: 1) } } } @@ -104,7 +104,7 @@ class MyBuffer { Manager(unsafeBufferObject: self).withUnsafeMutablePointers { (pointerToHeader, pointerToElements) -> Void in pointerToElements.deinitialize(count: self.count) - pointerToHeader.deinitialize() + pointerToHeader.deinitialize(count: 1) } } diff --git a/test/stdlib/NSStringAPI.swift b/test/stdlib/NSStringAPI.swift index 4d7d2a7e733..c70068efc80 100644 --- a/test/stdlib/NSStringAPI.swift +++ b/test/stdlib/NSStringAPI.swift @@ -215,7 +215,7 @@ NSStringAPIs.test("init(utf8String:)") { let cstr = UnsafeMutableRawPointer(up) .bindMemory(to: CChar.self, capacity: 100) expectOptionalEqual(s, String(utf8String: cstr)) - up.deallocate(capacity: 100) + up.deallocate() } NSStringAPIs.test("canBeConvertedToEncoding(_:)") { @@ -1837,4 +1837,3 @@ NSStringAPIs.test("copy construction") { } runAllTests() - diff --git a/test/stdlib/Reflection.swift b/test/stdlib/Reflection.swift index b58b2ba9a45..e5fc812e41a 100644 --- a/test/stdlib/Reflection.swift +++ b/test/stdlib/Reflection.swift @@ -184,8 +184,8 @@ dump(randomUnsafeMutablePointerString) var sanePointerString = UnsafeMutablePointer.allocate(capacity: 1) sanePointerString.initialize(to: "Hello panda") dump(sanePointerString.pointee) -sanePointerString.deinitialize() -sanePointerString.deallocate(capacity: 1) +sanePointerString.deinitialize(count: 1) +sanePointerString.deallocate() // Don't crash on types with opaque metadata. rdar://problem/19791252 // CHECK-NEXT: (Opaque Value) @@ -194,4 +194,3 @@ dump(rawPointer) // CHECK-LABEL: and now our song is done print("and now our song is done") - diff --git a/test/stdlib/StringAPI.swift b/test/stdlib/StringAPI.swift index 99c629e66ed..6988b328043 100644 --- a/test/stdlib/StringAPI.swift +++ b/test/stdlib/StringAPI.swift @@ -507,7 +507,7 @@ func getASCIIUTF8() -> (UnsafeMutablePointer, dealloc: () -> ()) { up[0] = 0x61 up[1] = 0x62 up[2] = 0 - return (up, { up.deallocate(capacity: 100) }) + return (up, { up.deallocate() }) } func getNonASCIIUTF8() -> (UnsafeMutablePointer, dealloc: () -> ()) { @@ -517,7 +517,7 @@ func getNonASCIIUTF8() -> (UnsafeMutablePointer, dealloc: () -> ()) { up[2] = 0xd0 up[3] = 0xb1 up[4] = 0 - return (UnsafeMutablePointer(up), { up.deallocate(capacity: 100) }) + return (UnsafeMutablePointer(up), { up.deallocate() }) } func getIllFormedUTF8String1( @@ -529,7 +529,7 @@ func getIllFormedUTF8String1( up[3] = 0x80 up[4] = 0x41 up[5] = 0 - return (UnsafeMutablePointer(up), { up.deallocate(capacity: 100) }) + return (UnsafeMutablePointer(up), { up.deallocate() }) } func getIllFormedUTF8String2( @@ -542,7 +542,7 @@ func getIllFormedUTF8String2( up[3] = 0x81 up[4] = 0x41 up[5] = 0 - return (UnsafeMutablePointer(up), { up.deallocate(capacity: 100) }) + return (UnsafeMutablePointer(up), { up.deallocate() }) } func asCCharArray(_ a: [UInt8]) -> [CChar] { @@ -677,4 +677,3 @@ CStringTests.test("String.utf8CString") { } runAllTests() - diff --git a/test/stdlib/UnsafePointer.swift.gyb b/test/stdlib/UnsafePointer.swift.gyb index c33dbc1619b..568f34fb227 100644 --- a/test/stdlib/UnsafePointer.swift.gyb +++ b/test/stdlib/UnsafePointer.swift.gyb @@ -360,7 +360,7 @@ UnsafeMutablePointerTestSuite.test("initialize:from:/immutable") { var ptr = UnsafeMutablePointer.allocate(capacity: 3) defer { ptr.deinitialize(count: 3) - ptr.deallocate(capacity: 3) + ptr.deallocate() } let source = (0..<3).map(Missile.init) source.withUnsafeBufferPointer { bufferPtr in @@ -375,7 +375,7 @@ UnsafeMutablePointerTestSuite.test("assign/immutable") { var ptr = UnsafeMutablePointer.allocate(capacity: 2) defer { ptr.deinitialize(count: 2) - ptr.deallocate(capacity: 2) + ptr.deallocate() } ptr.initialize(to: Missile(1)) (ptr + 1).initialize(to: Missile(2)) @@ -458,7 +458,7 @@ ${SelfName}TestSuite.test("Comparable") { ${SelfName}TestSuite.test("withMemoryRebound") { let mutablePtrI = UnsafeMutablePointer.allocate(capacity: 4) - defer { mutablePtrI.deallocate(capacity: 4) } + defer { mutablePtrI.deallocate() } let ptrI = ${SelfName}(mutablePtrI) ptrI.withMemoryRebound(to: UInt.self, capacity: 4) { // Make sure the closure argument isa $SelfName diff --git a/test/stdlib/UnsafeRawBufferPointer.swift b/test/stdlib/UnsafeRawBufferPointer.swift index 442f6fcf630..34b03cc67c9 100644 --- a/test/stdlib/UnsafeRawBufferPointer.swift +++ b/test/stdlib/UnsafeRawBufferPointer.swift @@ -33,7 +33,7 @@ UnsafeRawBufferPointerTestSuite.test("initFromValue") { // Mutable view of value2's bytes. withUnsafeMutableBytes(of: &value2) { bytes2 in expectEqual(bytes1.count, bytes2.count) - bytes2.copyBytes(from: bytes1) + bytes2.copyMemory(from: bytes1) } } expectEqual(value2, value1) @@ -51,7 +51,7 @@ UnsafeRawBufferPointerTestSuite.test("nonmutating_subscript_setter") { } expectEqual(value2, value1) - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 4) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 4, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } buffer.copyBytes(from: [0, 1, 2, 3] as [UInt8]) let leftBytes = buffer[0..<2] @@ -90,14 +90,14 @@ UnsafeRawBufferPointerTestSuite.test("initFromArray") { // Mutable view of array2's bytes. array2.withUnsafeMutableBytes { bytes2 in expectEqual(bytes1.count, bytes2.count) - bytes2.copyBytes(from: bytes1) + bytes2.copyMemory(from: bytes1) } } expectEqual(array2, array1) } UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 30) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 30, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let source = stride(from: 5 as Int64, to: 0, by: -1) if _isDebugAssertConfiguration() { @@ -113,7 +113,7 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") { } UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 30) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 30, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let source: [Int64] = [5, 4, 3, 2, 1] if _isDebugAssertConfiguration() { @@ -129,7 +129,7 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") { } UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).exact") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 24) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 24, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let source: [Int64] = [5, 4, 3] var (it,bound) = buffer.initializeMemory(as: Int64.self, from: source) @@ -175,7 +175,7 @@ UnsafeRawBufferPointerTestSuite.test("empty") { for _ in emptyBytes { expectUnreachable() } - let emptyMutableBytes = UnsafeMutableRawBufferPointer.allocate(count: 0) + let emptyMutableBytes = UnsafeMutableRawBufferPointer.allocate(byteCount: 0, alignment: MemoryLayout.alignment) for _ in emptyMutableBytes { expectUnreachable() } @@ -191,7 +191,7 @@ UnsafeRawBufferPointerTestSuite.test("reinterpret") { } let numPairs = 2 let bytes = UnsafeMutableRawBufferPointer.allocate( - count: MemoryLayout.stride * numPairs) + byteCount: MemoryLayout.stride * numPairs, alignment: MemoryLayout.alignment) defer { bytes.deallocate() } for i in 0..<(numPairs * 2) { @@ -221,7 +221,7 @@ UnsafeRawBufferPointerTestSuite.test("reinterpret") { UnsafeRawBufferPointerTestSuite.test("inBounds") { let numInts = 4 let bytes = UnsafeMutableRawBufferPointer.allocate( - count: MemoryLayout.stride * numInts) + byteCount: MemoryLayout.stride * numInts, alignment: MemoryLayout.alignment) defer { bytes.deallocate() } for i in 0...alignment) defer { buffer.deallocate() } let bytes = UnsafeRawBufferPointer(rebasing: buffer[1..<2]) @@ -262,7 +262,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.get.underflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.get.overflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 2) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let bytes = UnsafeRawBufferPointer(rebasing: buffer[0..<1]) @@ -275,7 +275,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.get.overflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.set.underflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 2) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } var bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[1..<2]) @@ -288,7 +288,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.set.underflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.set.overflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 2) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } var bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[0..<1]) @@ -301,7 +301,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.set.overflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.range.get.underflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let bytes = UnsafeRawBufferPointer(rebasing: buffer[1..<3]) @@ -314,7 +314,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.get.underflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.range.get.overflow") { - let buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let bytes = UnsafeRawBufferPointer(rebasing: buffer[0..<2]) @@ -327,7 +327,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.get.overflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.range.set.underflow") { - var buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } var bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[1..<3]) @@ -340,7 +340,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.set.underflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.range.set.overflow") { - var buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } var bytes = UnsafeMutableRawBufferPointer(rebasing: buffer[0..<2]) @@ -354,7 +354,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.set.overflow") { } UnsafeRawBufferPointerTestSuite.test("subscript.range.narrow") { - var buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } if _isDebugAssertConfiguration() { @@ -365,7 +365,7 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.narrow") { } UnsafeRawBufferPointerTestSuite.test("subscript.range.wide") { - var buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } if _isDebugAssertConfiguration() { @@ -375,8 +375,8 @@ UnsafeRawBufferPointerTestSuite.test("subscript.range.wide") { buffer[0..<2] = buffer[0..<3] } -UnsafeRawBufferPointerTestSuite.test("copyBytes.overflow") { - var buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) +UnsafeRawBufferPointerTestSuite.test("copyMemory.overflow") { + var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let bytes = buffer[0..<2] @@ -385,11 +385,12 @@ UnsafeRawBufferPointerTestSuite.test("copyBytes.overflow") { expectCrashLater() } // Performs a valid byte-wise copy but triggers a debug range size check. - UnsafeMutableRawBufferPointer(rebasing: bytes).copyBytes(from: buffer) + UnsafeMutableRawBufferPointer(rebasing: bytes).copyMemory( + from: UnsafeRawBufferPointer(buffer)) } UnsafeRawBufferPointerTestSuite.test("copyBytes.sequence.overflow") { - var buffer = UnsafeMutableRawBufferPointer.allocate(count: 3) + var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 3, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } let bytes = buffer[0..<2] @@ -459,8 +460,8 @@ UnsafeRawBufferPointerTestSuite.test("copy.bytes.overflow") var y: Int32 = 0 withUnsafeBytes(of: &x) { srcBytes in withUnsafeMutableBytes(of: &y) { destBytes in - destBytes.copyBytes( - from: UnsafeMutableRawBufferPointer(mutating: srcBytes)) + destBytes.copyMemory( + from: srcBytes) } } } @@ -475,13 +476,13 @@ UnsafeRawBufferPointerTestSuite.test("copy.sequence.overflow") var y: Int32 = 0 withUnsafeBytes(of: &x) { srcBytes in withUnsafeMutableBytes(of: &y) { destBytes in - destBytes.copyBytes(from: srcBytes) + destBytes.copyMemory(from: srcBytes) } } } UnsafeRawBufferPointerTestSuite.test("copy.overlap") { - let bytes = UnsafeMutableRawBufferPointer.allocate(count: 4) + let bytes = UnsafeMutableRawBufferPointer.allocate(byteCount: 4, alignment: MemoryLayout.alignment) // Right Overlap bytes[0] = 1 bytes[1] = 2 diff --git a/test/stdlib/UnsafeRawPointer.swift b/test/stdlib/UnsafeRawPointer.swift index b0b85a9a376..6f98ce9a0e5 100644 --- a/test/stdlib/UnsafeRawPointer.swift +++ b/test/stdlib/UnsafeRawPointer.swift @@ -18,20 +18,20 @@ UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory") { do { let sizeInBytes = 3 * MemoryLayout.stride var p1 = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + byteCount: sizeInBytes, alignment: MemoryLayout.alignment) defer { - p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + p1.deallocate() } - var ptrM = p1.initializeMemory(as: Missile.self, to: Missile(1)) - p1.initializeMemory(as: Missile.self, at: 1, count: 2, to: Missile(2)) + var ptrM = p1.initializeMemory(as: Missile.self, repeating: Missile(1), count: 1) + (p1 + MemoryLayout.stride).initializeMemory(as: Missile.self, repeating: Missile(2), count: 2) expectEqual(1, ptrM[0].number) expectEqual(2, ptrM[1].number) expectEqual(2, ptrM[2].number) var p2 = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + byteCount: sizeInBytes, alignment: MemoryLayout.alignment) defer { - p2.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + p2.deallocate() } let ptrM2 = p2.moveInitializeMemory(as: Missile.self, from: ptrM, count: 3) defer { @@ -57,9 +57,9 @@ UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory") { UnsafeMutableRawPointerExtraTestSuite.test("bindMemory") { let sizeInBytes = 3 * MemoryLayout.stride var p1 = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + byteCount: sizeInBytes, alignment: MemoryLayout.alignment) defer { - p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + p1.deallocate() } let ptrI = p1.bindMemory(to: Int.self, capacity: 3) let bufI = UnsafeMutableBufferPointer(start: ptrI, count: 3) @@ -75,9 +75,9 @@ UnsafeMutableRawPointerExtraTestSuite.test("bindMemory") { UnsafeMutableRawPointerExtraTestSuite.test("load/store") { let sizeInBytes = 3 * MemoryLayout.stride var p1 = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + byteCount: sizeInBytes, alignment: MemoryLayout.alignment) defer { - p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + p1.deallocate() } let ptrI = p1.initializeMemory(as: Int.self, from: 1...3) defer { @@ -94,14 +94,14 @@ UnsafeMutableRawPointerExtraTestSuite.test("load/store") { expectEqual(6, p1.load(fromByteOffset: 2 * MemoryLayout.stride, as: Int.self)) } -UnsafeMutableRawPointerExtraTestSuite.test("copyBytes") { +UnsafeMutableRawPointerExtraTestSuite.test("copyMemory") { let sizeInBytes = 4 * MemoryLayout.stride var rawPtr = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + byteCount: sizeInBytes, alignment: MemoryLayout.alignment) defer { - rawPtr.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + rawPtr.deallocate() } - let ptrI = rawPtr.initializeMemory(as: Int.self, count: 4, to: 42) + let ptrI = rawPtr.initializeMemory(as: Int.self, repeating: 42, count: 4) defer { ptrI.deinitialize(count: 4) } @@ -109,32 +109,32 @@ UnsafeMutableRawPointerExtraTestSuite.test("copyBytes") { // Right overlap ptrI[0] = 1 ptrI[1] = 2 - (rawPtr + MemoryLayout.stride).copyBytes( - from: roPtr, count: 2 * MemoryLayout.stride) + (rawPtr + MemoryLayout.stride).copyMemory( + from: roPtr, byteCount: 2 * MemoryLayout.stride) expectEqual(1, ptrI[1]) expectEqual(2, ptrI[2]) // Left overlap ptrI[1] = 2 ptrI[2] = 3 - rawPtr.copyBytes( - from: roPtr + MemoryLayout.stride, count: 2 * MemoryLayout.stride) + rawPtr.copyMemory( + from: roPtr + MemoryLayout.stride, byteCount: 2 * MemoryLayout.stride) expectEqual(2, ptrI[0]) expectEqual(3, ptrI[1]) // Disjoint: ptrI[2] = 2 ptrI[3] = 3 - rawPtr.copyBytes( - from: roPtr + 2 * MemoryLayout.stride, count: 2 * MemoryLayout.stride) + rawPtr.copyMemory( + from: roPtr + 2 * MemoryLayout.stride, byteCount: 2 * MemoryLayout.stride) expectEqual(2, ptrI[0]) expectEqual(3, ptrI[1]) // Backwards ptrI[0] = 0 ptrI[1] = 1 - (rawPtr + 2 * MemoryLayout.stride).copyBytes( - from: roPtr, count: 2 * MemoryLayout.stride) + (rawPtr + 2 * MemoryLayout.stride).copyMemory( + from: roPtr, byteCount: 2 * MemoryLayout.stride) expectEqual(0, ptrI[2]) expectEqual(1, ptrI[3]) } diff --git a/validation-test/stdlib/ArrayNew.swift.gyb b/validation-test/stdlib/ArrayNew.swift.gyb index bd7278a4ee3..499f097eb16 100644 --- a/validation-test/stdlib/ArrayNew.swift.gyb +++ b/validation-test/stdlib/ArrayNew.swift.gyb @@ -612,7 +612,7 @@ ArrayTestSuite.test("BridgedToObjC/Verbatim/getObjects") { expectEqual(idValue2, unsafeBitCast(a.object(at: 2) as AnyObject, to: UInt.self)) } - buffer.deallocate(capacity: 3) + buffer.deallocate() _fixLifetime(a) expectAutoreleasedKeysAndValues(unopt: (0, 3)) @@ -886,7 +886,7 @@ ArrayTestSuite.test("BridgedToObjC/Custom/getObjects") { expectEqual(idValue2, unsafeBitCast(a.object(at: 2) as AnyObject, to: UInt.self)) } - buffer.deallocate(capacity: 3) + buffer.deallocate() _fixLifetime(a) expectEqual(3, TestBridgedValueTy.bridgeOperations) @@ -1152,4 +1152,3 @@ ArrayTestSuite.tearDown { #endif // _runtime(_ObjC) runAllTests() - diff --git a/validation-test/stdlib/CoreAudio.swift b/validation-test/stdlib/CoreAudio.swift index 3fe126396f3..71e9dfddadd 100644 --- a/validation-test/stdlib/CoreAudio.swift +++ b/validation-test/stdlib/CoreAudio.swift @@ -202,7 +202,7 @@ CoreAudioTestSuite.test( CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.count") { let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16) let rawPtr = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + byteCount: sizeInBytes, alignment: MemoryLayout.alignment) let ablPtr = rawPtr.bindMemory(to: AudioBufferList.self, capacity: sizeInBytes / MemoryLayout.stride) @@ -218,15 +218,14 @@ CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.count") { ablPtrWrapper.count = 0x7765_4321 expectEqual(0x7765_4321, rawPtr.load(as: UInt32.self)) - rawPtr.deallocate( - bytes: sizeInBytes, alignedTo: MemoryLayout.alignment) + rawPtr.deallocate() } CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.subscript(_: Int)") { let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16) let rawPtr = UnsafeMutableRawPointer.allocate( - bytes: sizeInBytes, alignedTo: 1) + byteCount: sizeInBytes, alignment: 1) let ablPtr = rawPtr.bindMemory( to: AudioBufferList.self, @@ -268,7 +267,7 @@ CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.subscript(_: Int)") expectEqual(audioBuffer.mData, audioBufferPtr.pointee.mData) } - ablPtr.deallocate(capacity: sizeInBytes) + ablPtr.deallocate() } CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.subscript(_: Int)/trap") { @@ -303,4 +302,3 @@ CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer/Collection") { } runAllTests() - diff --git a/validation-test/stdlib/HashingAvalanche.swift b/validation-test/stdlib/HashingAvalanche.swift index b1e5befbfdf..51b89dab453 100644 --- a/validation-test/stdlib/HashingAvalanche.swift +++ b/validation-test/stdlib/HashingAvalanche.swift @@ -41,7 +41,7 @@ func avalancheTest( chiSquaredUniform2(testsInBatch, bitFlips[outputBit], pValue), "inputBit: \(inputBit), outputBit: \(outputBit)") } - bitFlips.deallocate(capacity: bits) + bitFlips.deallocate() } } @@ -56,4 +56,3 @@ HashingTestSuite.test("_mixUInt32/avalanche") { } runAllTests() - diff --git a/validation-test/stdlib/Lazy.swift.gyb b/validation-test/stdlib/Lazy.swift.gyb index 56c716bd776..aeb39a4e8e1 100644 --- a/validation-test/stdlib/Lazy.swift.gyb +++ b/validation-test/stdlib/Lazy.swift.gyb @@ -677,7 +677,7 @@ Base.Iterator.Element == S.Iterator.Element { expectTrue(writtenUpTo == buf.endIndex, "_copyContents failed to use entire buffer") ptr.deinitialize(count: count) - ptr.deallocate(capacity: count) + ptr.deallocate() } } diff --git a/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb b/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb index cedd73614cc..9b4a5a3f289 100644 --- a/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb +++ b/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb @@ -106,7 +106,7 @@ import SwiftShims func allocBytes(count: Int, alignment: Int) -> UnsafeMutableRawPointer { - return UnsafeMutableRawPointer.allocate(bytes: count, alignedTo: alignment) + return UnsafeMutableRawPointer.allocate(byteCount: count, alignment: alignment) } func deallocBytes( @@ -114,7 +114,7 @@ func deallocBytes( byteCount: Int, alignment: Int ) { - pointer.deallocate(bytes: byteCount, alignedTo: alignment) + pointer.deallocate() } func _swift_stdlib_atomicStoreInt( @@ -1054,12 +1054,12 @@ struct _PVArrayNodePointer for i in keyPopulationBitmap.setBitIndices { (_childNodeOrKeyArray + _Self._childNodeOrKeyStride * i) .assumingMemoryBound(to: Key.self) - .deinitialize() + .deinitialize(count: 1) } } if !_isPOD(Value.self) { for i in keyPopulationBitmap.setBitIndices { - (_valueArray + i).deinitialize() + (_valueArray + i).deinitialize(count: 1) } } deallocBytes(self._nodePointer, @@ -1135,7 +1135,7 @@ struct _PVArrayNodePointer precondition(!keyPopulationBitmap[i]) // sanity check (_childNodeOrKeyArray + _Self._childNodeOrKeyStride * i) - .initializeMemory(as: Key.self, to: key) + .initializeMemory(as: Key.self, repeating: key, count: 1) (_valueArray + i).initialize(to: value) keyPopulationBitmap[i] = true @@ -2214,4 +2214,3 @@ PersistentVectorTests.test("removeValue(forKey:)/NoCollisionsInRootNode/1") { } runAllTests() - diff --git a/validation-test/stdlib/StringSlicesConcurrentAppend.swift b/validation-test/stdlib/StringSlicesConcurrentAppend.swift index 89caf778b18..f42513c383b 100644 --- a/validation-test/stdlib/StringSlicesConcurrentAppend.swift +++ b/validation-test/stdlib/StringSlicesConcurrentAppend.swift @@ -109,9 +109,8 @@ StringTestSuite.test("SliceConcurrentAppend") { ret = _stdlib_pthread_barrier_destroy(barrierVar!) expectEqual(0, ret) - barrierVar!.deinitialize() - barrierVar!.deallocate(capacity: 1) + barrierVar!.deinitialize(count: 1) + barrierVar!.deallocate() } runAllTests() - diff --git a/validation-test/stdlib/UnsafeBitMap.swift b/validation-test/stdlib/UnsafeBitMap.swift index 1e5a95d000d..18ac34d71e2 100644 --- a/validation-test/stdlib/UnsafeBitMap.swift +++ b/validation-test/stdlib/UnsafeBitMap.swift @@ -87,7 +87,7 @@ UnsafeBitMapTests.test("initializeToZero()") .forEach(in: sizes) { sizeInBits in let bitMap = make(sizeInBits: sizeInBits) - defer { bitMap.values.deallocate(capacity: bitMap.numberOfWords) } + defer { bitMap.values.deallocate() } bitMap.initializeToZero() for i in 0.. UnsafeMutableRawPointer { - return UnsafeMutableRawPointer.allocate(bytes: count, alignedTo: 1) + return UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1) } func allocateForBuffer(count: Int) -> UnsafeMutablePointer { return UnsafeMutablePointer.allocate(capacity: count) @@ -25,12 +25,12 @@ func allocateForBuffer(count: Int) -> UnsafeMutablePointer { func deallocateForRawBuffer( _ memory: UnsafeMutableRawPointer, count: Int ) { - memory.deallocate(bytes: count, alignedTo: 1) + memory.deallocate() } func deallocateForBuffer( _ memory: UnsafeMutablePointer, count: Int ) { - memory.deallocate(capacity: count) + memory.deallocate() } // Initialize memory with arbitrary equatable, comparable values. @@ -74,7 +74,8 @@ ${SelfName}TestSuite.test("AssociatedTypes") { } ${SelfName}TestSuite.test("rebasing") { - let rawbuffer = UnsafeMutableRawBufferPointer.allocate(count: 4 * MemoryLayout.stride) + let rawbuffer = UnsafeMutableRawBufferPointer.allocate( + byteCount: 4 * MemoryLayout.stride, alignment: MemoryLayout.alignment) defer { rawbuffer.deallocate() } rawbuffer.copyBytes(from: [0, 1, 2, 3]) @@ -136,7 +137,7 @@ func checkWithExpectedBuffer(checkBuffers: (${SelfType}, ${SelfType}) -> Void) { count: elementCount) % if IsRaw: - expectedBuffer.copyBytes(from: buffer) + expectedBuffer.copyMemory(from: UnsafeRawBufferPointer(buffer)) % else: _ = expectedBuffer.initialize(from: buffer) % end @@ -168,7 +169,7 @@ ${SelfName}TestSuite.test("nilBaseAddress") { ${SelfName}TestSuite.test("nonNilButEmpty") { let emptyAllocated = UnsafeMutablePointer.allocate(capacity: 0) - defer { emptyAllocated.deallocate(capacity: 0) } + defer { emptyAllocated.deallocate() } let emptyBuffer = ${SelfType}(start: ${PointerType}(emptyAllocated), count: 0) expectEqual(emptyAllocated, emptyBuffer.baseAddress) @@ -184,9 +185,9 @@ ${SelfName}TestSuite.test("nonNilButEmpty") { % if IsRaw: ${SelfName}TestSuite.test("nonNilNonEmpty") { let count = 4 - let allocated = UnsafeMutableRawPointer.allocate(bytes: count, alignedTo: 1) - defer { allocated.deallocate(bytes: count, alignedTo: 1) } - let uint8Ptr = allocated.initializeMemory(as: UInt8.self, count: count, to: 1) + let allocated = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1) + defer { allocated.deallocate() } + let uint8Ptr = allocated.initializeMemory(as: UInt8.self, repeating: 1, count: count) uint8Ptr[count - 1] = 2 let buffer = ${SelfType}(start: ${PointerType}(allocated), count: count - 1) @@ -213,8 +214,8 @@ ${SelfName}TestSuite.test("nonNilNonEmpty") { ${SelfName}TestSuite.test("nonNilNonEmpty") { let count = 4 let allocated = UnsafeMutablePointer.allocate(capacity: count) - defer { allocated.deallocate(capacity: count) } - allocated.initialize(to: 1.0, count: count) + defer { allocated.deallocate() } + allocated.initialize(repeating: 1.0, count: count) allocated[count - 1] = 2.0 let buffer = ${SelfType}(start: ${PointerType}(allocated), count: count - 1) @@ -247,7 +248,7 @@ ${SelfName}TestSuite.test("badCount") expectCrashLater() let emptyAllocated = UnsafeMutablePointer.allocate(capacity: 0) - defer { emptyAllocated.deallocate(capacity: 0) } + defer { emptyAllocated.deallocate() } let buffer = ${SelfType}(start: ${PointerType}(emptyAllocated), count: -1) _ = buffer @@ -267,9 +268,9 @@ ${SelfName}TestSuite.test("badNilCount") UnsafeMutableRawBufferPointerTestSuite.test("changeElementViaBuffer") { let count = 4 - let allocated = UnsafeMutableRawPointer.allocate(bytes: count, alignedTo: 1) - defer { allocated.deallocate(bytes: count, alignedTo: 1) } - let uint8Ptr = allocated.initializeMemory(as: UInt8.self, count: count, to: 1) + let allocated = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1) + defer { allocated.deallocate() } + let uint8Ptr = allocated.initializeMemory(as: UInt8.self, repeating: 1, count: count) uint8Ptr[count-1] = UInt8.max var buffer = UnsafeMutableRawBufferPointer(start: allocated, count: count - 1) @@ -298,8 +299,8 @@ UnsafeMutableRawBufferPointerTestSuite.test("changeElementViaBuffer") { UnsafeMutableBufferPointerTestSuite.test("changeElementViaBuffer") { let count = 4 let allocated = UnsafeMutablePointer.allocate(capacity: count) - defer { allocated.deallocate(capacity: count) } - allocated.initialize(to: 1.0, count: count) + defer { allocated.deallocate() } + allocated.initialize(repeating: 1.0, count: count) allocated[count-1] = -1.0 var buffer = UnsafeMutableBufferPointer(start: allocated, count: count - 1) @@ -343,7 +344,7 @@ extension SubscriptTest { -> ${SelfType} { for i in Self.start...stride).initializeMemory(as: UInt8.self, repeating: numericCast(i), count: 1) } return ${SelfType}(start: memory, count: Self.elementCount) } @@ -489,8 +490,8 @@ struct SubscriptSetTest : SubscriptTest { ) -> UnsafeMutableRawBufferPointer.SubSequence { for (i, value) in replacementValues.enumerated() { - memory.initializeMemory( - as: UInt8.self, at: i, to: numericCast(value.value)) + (memory + i * MemoryLayout.stride).initializeMemory( + as: UInt8.self, repeating: numericCast(value.value), count: 1) } let buffer = UnsafeMutableRawBufferPointer( start: memory, count: replacementValues.count) @@ -756,6 +757,7 @@ UnsafeMutable${'Raw' if IsRaw else ''}BufferPointerTestSuite.test("subscript/${R defer { deallocateFor${Raw}Buffer( sliceMemory, count: replacementValues.count) } + // This produces a spurious compiler warning, someone take a look lol? var buffer = SubscriptSetTest.create${SelfName}(from: memory) % if IsRaw: