mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +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
@@ -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])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user