mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
implement SE 184: add allocation methods to Unsafe buffer pointers, drop all parameters from deallocation, adjust namings, and add repeated-value assignment methods
This commit is contained in:
committed by
Andrew Trick
parent
c7d33e70d3
commit
c85880899d
@@ -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
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@ public func _opaqueIdentity<T>(_ x: T) -> T {
|
||||
ptr.initialize(to: x)
|
||||
let result =
|
||||
UnsafeMutablePointer<T>(_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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -107,8 +107,8 @@ public func _stdlib_pthread_join<Result>(
|
||||
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)
|
||||
|
||||
@@ -361,7 +361,7 @@ public func reflect<T>(any: T) {
|
||||
anyPointer.initialize(to: any)
|
||||
let anyPointerValue = UInt(bitPattern: anyPointer)
|
||||
reflect(instanceAddress: anyPointerValue, kind: .Existential)
|
||||
anyPointer.deallocate(capacity: MemoryLayout<Any>.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<ThickFunction0>.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<ThickFunction1>.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<ThickFunction2>.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<ThickFunction3>.size)
|
||||
fn.deallocate()
|
||||
}
|
||||
|
||||
/// Call this function to indicate to the parent that there are
|
||||
|
||||
@@ -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<UInt>.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<UInt>.alignment)
|
||||
defer { buffer.deallocate() }
|
||||
let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
|
||||
var enumerated = 0
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -177,9 +177,9 @@ extension NSSet {
|
||||
assert(alignment == MemoryLayout<AnyObject>.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(
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1526,7 +1526,7 @@ internal struct KeyPathBuffer {
|
||||
src: UnsafeMutableRawPointer(mutating: raw.baseAddress.unsafelyUnwrapped),
|
||||
size: UInt(MemoryLayout<T>.size))
|
||||
let result = resultBuf.pointee
|
||||
resultBuf.deallocate(capacity: 1)
|
||||
resultBuf.deallocate()
|
||||
return result
|
||||
}
|
||||
@_inlineable // FIXME(sil-serialize-all)
|
||||
|
||||
@@ -162,7 +162,7 @@ open class ManagedBuffer<Header, Element> {
|
||||
/// Manager(unsafeBufferObject: self).withUnsafeMutablePointers {
|
||||
/// (pointerToHeader, pointerToElements) -> Void in
|
||||
/// pointerToElements.deinitialize(count: self.count)
|
||||
/// pointerToHeader.deinitialize()
|
||||
/// pointerToHeader.deinitialize(count: 1)
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
|
||||
@@ -69,7 +69,7 @@ internal func _withUninitializedString<R>(
|
||||
let stringPtr = UnsafeMutablePointer<String>.allocate(capacity: 1)
|
||||
let bodyResult = body(stringPtr)
|
||||
let stringResult = stringPtr.move()
|
||||
stringPtr.deallocate(capacity: 1)
|
||||
stringPtr.deallocate()
|
||||
return (bodyResult, stringResult)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<AnyObject>.stride)
|
||||
byteCount: range.length * MemoryLayout<AnyObject>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -303,8 +303,35 @@ public struct Unsafe${Mutable}BufferPointer<Element>
|
||||
_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<Element>) {
|
||||
_position = UnsafeMutablePointer<Element>(mutating: other._position)
|
||||
_end = UnsafeMutablePointer<Element>(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<Element>) {
|
||||
_position = UnsafePointer<Element>(other._position)
|
||||
_end = UnsafePointer<Element>(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<Element>
|
||||
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<Element>
|
||||
public func makeIterator() -> UnsafeBufferPointerIterator<Element> {
|
||||
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<Element>.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<Int>.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<Element> {
|
||||
let size = MemoryLayout<Element>.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<T, Result>(
|
||||
to type: T.Type, _ body: (${Self}<T>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
if let base = _position {
|
||||
_debugPrecondition(MemoryLayout<Element>.stride == MemoryLayout<T>.stride)
|
||||
Builtin.bindMemory(base._rawValue, count._builtinWordValue, T.self)
|
||||
defer {
|
||||
Builtin.bindMemory(base._rawValue, count._builtinWordValue, Element.self)
|
||||
}
|
||||
|
||||
return try body(${Self}<T>(
|
||||
start: Unsafe${Mutable}Pointer<T>(base._rawValue), count: count))
|
||||
}
|
||||
else {
|
||||
return try body(${Self}<T>(start: nil, count: 0))
|
||||
}
|
||||
}
|
||||
|
||||
/// A pointer to the first element of the buffer.
|
||||
///
|
||||
@@ -382,12 +547,11 @@ public struct Unsafe${Mutable}BufferPointer<Element>
|
||||
/// 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
|
||||
|
||||
@@ -412,8 +412,6 @@ public struct ${Self}<Pointee>
|
||||
///
|
||||
/// The resulting pointer references a region of memory that is bound to
|
||||
/// `Pointee` and is `count * MemoryLayout<Pointee>.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}<Pointee>
|
||||
/// 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}<Pointee>
|
||||
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<Pointee>.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<Pointee>.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}<Pointee>
|
||||
}
|
||||
|
||||
% 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..<count {
|
||||
Builtin.initialize(newValue, (self + offset)._rawValue)
|
||||
Builtin.initialize(repeatedValue, (self + offset)._rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes this pointer's memory with a single instance 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:)`, the
|
||||
/// memory referenced by this pointer is initialized. Calling this method is
|
||||
/// roughly equivalent to calling `initialize(repeating:count:)` with a
|
||||
/// `count` of 1.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: The instance to initialize this pointer's pointee to.
|
||||
@_inlineable
|
||||
public func initialize(to value: Pointee) {
|
||||
Builtin.initialize(value, self._rawValue)
|
||||
}
|
||||
|
||||
/// Retrieves and returns the referenced instance, returning the pointer's
|
||||
/// memory to an uninitialized state.
|
||||
@@ -519,7 +537,7 @@ public struct ${Self}<Pointee>
|
||||
/// 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}<Pointee>
|
||||
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..<count {
|
||||
self[i] = repeatedValue
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces this pointer's initialized memory with the specified number of
|
||||
/// instances from the given pointer's memory.
|
||||
///
|
||||
@@ -695,7 +733,14 @@ public struct ${Self}<Pointee>
|
||||
// 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}<Pointee>
|
||||
/// 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.
|
||||
|
||||
@@ -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<UInt>.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<UInt>.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<UInt>.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<T>.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<T>.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<T>.stride`
|
||||
/// instances of `T`.
|
||||
@_inlineable
|
||||
@discardableResult
|
||||
public func initializeMemory<T>(as type: T.Type, repeating repeatedValue: T)
|
||||
-> UnsafeMutableBufferPointer<T> {
|
||||
guard let base = _position else {
|
||||
return UnsafeMutableBufferPointer<T>(start: nil, count: 0)
|
||||
}
|
||||
|
||||
let count = (_end! - base) / MemoryLayout<T>.stride
|
||||
let typed = base.initializeMemory(
|
||||
as: type, repeating: repeatedValue, count: count)
|
||||
return UnsafeMutableBufferPointer<T>(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<T>.stride` instances of `T`.
|
||||
@_inlineable // FIXME(sil-serialize-all)
|
||||
@_transparent
|
||||
@discardableResult
|
||||
public func bindMemory<T>(
|
||||
to type: T.Type
|
||||
) -> Unsafe${Mutable}BufferPointer<T> {
|
||||
guard let base = _position else {
|
||||
return Unsafe${Mutable}BufferPointer<T>(start: nil, count: 0)
|
||||
}
|
||||
|
||||
let capacity = count / MemoryLayout<T>.stride
|
||||
Builtin.bindMemory(base._rawValue, capacity._builtinWordValue, type)
|
||||
return Unsafe${Mutable}BufferPointer<T>(
|
||||
start: Unsafe${Mutable}Pointer<T>(base._rawValue), count: capacity)
|
||||
}
|
||||
|
||||
@_versioned
|
||||
internal let _position, _end: Unsafe${Mutable}RawPointer?
|
||||
}
|
||||
|
||||
@@ -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<T>(
|
||||
as type: T.Type, at offset: Int = 0, count: Int = 1, to repeatedValue: T
|
||||
) -> UnsafeMutablePointer<T> {
|
||||
return (self + offset * MemoryLayout<T>.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<Int8>.stride,
|
||||
/// alignedTo: MemoryLayout<Int8>.alignment)
|
||||
/// byteCount: count * MemoryLayout<Int8>.stride,
|
||||
/// alignment: MemoryLayout<Int8>.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<T>.stride` and continuing up to
|
||||
/// `p + (index + count) * MemoryLayout<T>.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<T>.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<T>.stride` bytes.
|
||||
/// must not be negative.
|
||||
/// - Returns: A typed pointer to the memory referenced by this raw pointer.
|
||||
@_inlineable
|
||||
@discardableResult
|
||||
public func initializeMemory<T>(as type: T.Type, at index: Int = 0,
|
||||
count: Int = 1, to value: T
|
||||
public func initializeMemory<T>(
|
||||
as type: T.Type, repeating repeatedValue: T, count: Int
|
||||
) -> UnsafeMutablePointer<T> {
|
||||
_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<T>.stride
|
||||
var nextPtr = self
|
||||
for _ in 0..<count {
|
||||
Builtin.initialize(value, nextPtr._rawValue)
|
||||
Builtin.initialize(repeatedValue, nextPtr._rawValue)
|
||||
nextPtr += MemoryLayout<T>.stride
|
||||
}
|
||||
return UnsafeMutablePointer(_rawValue)
|
||||
@@ -702,8 +713,10 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
|
||||
) -> UnsafeMutablePointer<C.Element> {
|
||||
// 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<C.Element>.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<T>.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
|
||||
|
||||
|
||||
@@ -82,9 +82,7 @@ class Vector<T> {
|
||||
}
|
||||
|
||||
deinit {
|
||||
for i in 0..<length {
|
||||
(base + i).deinitialize()
|
||||
}
|
||||
base.deinitialize(count: length)
|
||||
c_free(base)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ class B<T> {
|
||||
self.ptr = ptr
|
||||
}
|
||||
deinit {
|
||||
ptr.deinitialize()
|
||||
ptr.deinitialize(count: 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Result>(_ 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()
|
||||
|
||||
|
||||
@@ -28,9 +28,9 @@ public func call_foobar() {
|
||||
var a = UnsafeMutablePointer<Int>.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
|
||||
|
||||
@@ -163,7 +163,7 @@ func exerciseArrayValueWitnesses<T>(_ 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") {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -81,7 +81,7 @@ final class TestManagedBuffer<T> : ManagedBuffer<CountAndCapacity, T> {
|
||||
withUnsafeMutablePointerToElements {
|
||||
(x: UnsafeMutablePointer<T>) -> () 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<T> {
|
||||
Manager(unsafeBufferObject: self).withUnsafeMutablePointers {
|
||||
(pointerToHeader, pointerToElements) -> Void in
|
||||
pointerToElements.deinitialize(count: self.count)
|
||||
pointerToHeader.deinitialize()
|
||||
pointerToHeader.deinitialize(count: 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -184,8 +184,8 @@ dump(randomUnsafeMutablePointerString)
|
||||
var sanePointerString = UnsafeMutablePointer<String>.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")
|
||||
|
||||
|
||||
@@ -507,7 +507,7 @@ func getASCIIUTF8() -> (UnsafeMutablePointer<UInt8>, dealloc: () -> ()) {
|
||||
up[0] = 0x61
|
||||
up[1] = 0x62
|
||||
up[2] = 0
|
||||
return (up, { up.deallocate(capacity: 100) })
|
||||
return (up, { up.deallocate() })
|
||||
}
|
||||
|
||||
func getNonASCIIUTF8() -> (UnsafeMutablePointer<UInt8>, dealloc: () -> ()) {
|
||||
@@ -517,7 +517,7 @@ func getNonASCIIUTF8() -> (UnsafeMutablePointer<UInt8>, 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()
|
||||
|
||||
|
||||
@@ -360,7 +360,7 @@ UnsafeMutablePointerTestSuite.test("initialize:from:/immutable") {
|
||||
var ptr = UnsafeMutablePointer<Missile>.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<Missile>.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<Int>.allocate(capacity: 4)
|
||||
defer { mutablePtrI.deallocate(capacity: 4) }
|
||||
defer { mutablePtrI.deallocate() }
|
||||
let ptrI = ${SelfName}<Int>(mutablePtrI)
|
||||
ptrI.withMemoryRebound(to: UInt.self, capacity: 4) {
|
||||
// Make sure the closure argument isa $SelfName
|
||||
|
||||
@@ -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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.alignment)
|
||||
for _ in emptyMutableBytes {
|
||||
expectUnreachable()
|
||||
}
|
||||
@@ -191,7 +191,7 @@ UnsafeRawBufferPointerTestSuite.test("reinterpret") {
|
||||
}
|
||||
let numPairs = 2
|
||||
let bytes = UnsafeMutableRawBufferPointer.allocate(
|
||||
count: MemoryLayout<Pair>.stride * numPairs)
|
||||
byteCount: MemoryLayout<Pair>.stride * numPairs, alignment: MemoryLayout<UInt>.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<Int>.stride * numInts)
|
||||
byteCount: MemoryLayout<Int>.stride * numInts, alignment: MemoryLayout<UInt>.alignment)
|
||||
defer { bytes.deallocate() }
|
||||
|
||||
for i in 0..<numInts {
|
||||
@@ -249,7 +249,7 @@ UnsafeRawBufferPointerTestSuite.test("inBounds") {
|
||||
}
|
||||
|
||||
UnsafeRawBufferPointerTestSuite.test("subscript.get.underflow") {
|
||||
let buffer = UnsafeMutableRawBufferPointer.allocate(count: 2)
|
||||
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.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<UInt>.alignment)
|
||||
// Right Overlap
|
||||
bytes[0] = 1
|
||||
bytes[1] = 2
|
||||
|
||||
@@ -18,20 +18,20 @@ UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory") {
|
||||
do {
|
||||
let sizeInBytes = 3 * MemoryLayout<Missile>.stride
|
||||
var p1 = UnsafeMutableRawPointer.allocate(
|
||||
bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.alignment)
|
||||
byteCount: sizeInBytes, alignment: MemoryLayout<Missile>.alignment)
|
||||
defer {
|
||||
p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.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<Missile>.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<Missile>.alignment)
|
||||
byteCount: sizeInBytes, alignment: MemoryLayout<Missile>.alignment)
|
||||
defer {
|
||||
p2.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Missile>.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<Int>.stride
|
||||
var p1 = UnsafeMutableRawPointer.allocate(
|
||||
bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
|
||||
byteCount: sizeInBytes, alignment: MemoryLayout<Int>.alignment)
|
||||
defer {
|
||||
p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.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<Int>.stride
|
||||
var p1 = UnsafeMutableRawPointer.allocate(
|
||||
bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
|
||||
byteCount: sizeInBytes, alignment: MemoryLayout<Int>.alignment)
|
||||
defer {
|
||||
p1.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.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<Int>.stride, as: Int.self))
|
||||
}
|
||||
|
||||
UnsafeMutableRawPointerExtraTestSuite.test("copyBytes") {
|
||||
UnsafeMutableRawPointerExtraTestSuite.test("copyMemory") {
|
||||
let sizeInBytes = 4 * MemoryLayout<Int>.stride
|
||||
var rawPtr = UnsafeMutableRawPointer.allocate(
|
||||
bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.alignment)
|
||||
byteCount: sizeInBytes, alignment: MemoryLayout<Int>.alignment)
|
||||
defer {
|
||||
rawPtr.deallocate(bytes: sizeInBytes, alignedTo: MemoryLayout<Int>.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<Int>.stride).copyBytes(
|
||||
from: roPtr, count: 2 * MemoryLayout<Int>.stride)
|
||||
(rawPtr + MemoryLayout<Int>.stride).copyMemory(
|
||||
from: roPtr, byteCount: 2 * MemoryLayout<Int>.stride)
|
||||
expectEqual(1, ptrI[1])
|
||||
expectEqual(2, ptrI[2])
|
||||
|
||||
// Left overlap
|
||||
ptrI[1] = 2
|
||||
ptrI[2] = 3
|
||||
rawPtr.copyBytes(
|
||||
from: roPtr + MemoryLayout<Int>.stride, count: 2 * MemoryLayout<Int>.stride)
|
||||
rawPtr.copyMemory(
|
||||
from: roPtr + MemoryLayout<Int>.stride, byteCount: 2 * MemoryLayout<Int>.stride)
|
||||
expectEqual(2, ptrI[0])
|
||||
expectEqual(3, ptrI[1])
|
||||
|
||||
// Disjoint:
|
||||
ptrI[2] = 2
|
||||
ptrI[3] = 3
|
||||
rawPtr.copyBytes(
|
||||
from: roPtr + 2 * MemoryLayout<Int>.stride, count: 2 * MemoryLayout<Int>.stride)
|
||||
rawPtr.copyMemory(
|
||||
from: roPtr + 2 * MemoryLayout<Int>.stride, byteCount: 2 * MemoryLayout<Int>.stride)
|
||||
expectEqual(2, ptrI[0])
|
||||
expectEqual(3, ptrI[1])
|
||||
|
||||
// Backwards
|
||||
ptrI[0] = 0
|
||||
ptrI[1] = 1
|
||||
(rawPtr + 2 * MemoryLayout<Int>.stride).copyBytes(
|
||||
from: roPtr, count: 2 * MemoryLayout<Int>.stride)
|
||||
(rawPtr + 2 * MemoryLayout<Int>.stride).copyMemory(
|
||||
from: roPtr, byteCount: 2 * MemoryLayout<Int>.stride)
|
||||
expectEqual(0, ptrI[2])
|
||||
expectEqual(1, ptrI[3])
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -202,7 +202,7 @@ CoreAudioTestSuite.test(
|
||||
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.count") {
|
||||
let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16)
|
||||
let rawPtr = UnsafeMutableRawPointer.allocate(
|
||||
bytes: sizeInBytes, alignedTo: MemoryLayout<AudioBufferList>.alignment)
|
||||
byteCount: sizeInBytes, alignment: MemoryLayout<AudioBufferList>.alignment)
|
||||
let ablPtr = rawPtr.bindMemory(to: AudioBufferList.self,
|
||||
capacity: sizeInBytes / MemoryLayout<AudioBufferList>.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<AudioBufferList>.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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Key : Hashable, Value>
|
||||
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<Key : Hashable, Value>
|
||||
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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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..<sizeInBits {
|
||||
@@ -99,7 +99,7 @@ UnsafeBitMapTests.test("subscript")
|
||||
.forEach(in: sizes) {
|
||||
sizeInBits in
|
||||
let bitMap = make(sizeInBits: sizeInBits)
|
||||
defer { bitMap.values.deallocate(capacity: bitMap.numberOfWords) }
|
||||
defer { bitMap.values.deallocate() }
|
||||
|
||||
if sizeInBits != 0 {
|
||||
bitMap.initializeToZero()
|
||||
@@ -122,4 +122,3 @@ UnsafeBitMapTests.test("subscript")
|
||||
}
|
||||
|
||||
runAllTests()
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import StdlibCollectionUnittest
|
||||
// Tests
|
||||
|
||||
func allocateForRawBuffer(count: Int) -> UnsafeMutableRawPointer {
|
||||
return UnsafeMutableRawPointer.allocate(bytes: count, alignedTo: 1)
|
||||
return UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
|
||||
}
|
||||
func allocateForBuffer<T>(count: Int) -> UnsafeMutablePointer<T> {
|
||||
return UnsafeMutablePointer<T>.allocate(capacity: count)
|
||||
@@ -25,12 +25,12 @@ func allocateForBuffer<T>(count: Int) -> UnsafeMutablePointer<T> {
|
||||
func deallocateForRawBuffer(
|
||||
_ memory: UnsafeMutableRawPointer, count: Int
|
||||
) {
|
||||
memory.deallocate(bytes: count, alignedTo: 1)
|
||||
memory.deallocate()
|
||||
}
|
||||
func deallocateForBuffer<T>(
|
||||
_ memory: UnsafeMutablePointer<T>, 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<Int>.stride)
|
||||
let rawbuffer = UnsafeMutableRawBufferPointer.allocate(
|
||||
byteCount: 4 * MemoryLayout<Int>.stride, alignment: MemoryLayout<UInt>.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<Float>.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<Float>.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<Float>.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<Float>.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..<Self.end {
|
||||
memory.initializeMemory(as: UInt8.self, at: i, to: numericCast(i))
|
||||
(memory + i * MemoryLayout<UInt8>.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<UInt8>.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:
|
||||
|
||||
Reference in New Issue
Block a user