mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
Drop support for Swift 6.0 and 6.1
This allows the use of Span types without `#if compiler` guards.
This commit is contained in:
145 files changed
+342
-1047
No files matched your search
@@ -11,8 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
/// A value that manages a contiguous block of memory starting with a header
|
||||
/// value and then followed by a contiguous array of elements. Values of this
|
||||
/// type own the underlying memory, and are non-copyable to ensure that
|
||||
@@ -30,16 +28,16 @@ where Header: ~Copyable
|
||||
/// of the Header and we have overallocated to compensate.
|
||||
@usableFromInline
|
||||
var _storage: UnsafeMutableRawPointer
|
||||
|
||||
|
||||
/// Pointer to the header.
|
||||
@_alwaysEmitIntoClient
|
||||
var _pointer: UnsafeMutablePointer<Header> {
|
||||
Self.headerPointer(fromStorage: _storage)
|
||||
}
|
||||
|
||||
|
||||
/// The element type stored within the buffer.
|
||||
public typealias Element = Header.Element
|
||||
|
||||
|
||||
/// Allocate storage and initialize only the header, leaving the trailing
|
||||
/// elements uninitialized. This is private because it can compromise
|
||||
/// memory safety.
|
||||
@@ -50,10 +48,10 @@ where Header: ~Copyable
|
||||
byteCount: bytes,
|
||||
alignment: alignment
|
||||
)
|
||||
|
||||
|
||||
_pointer.initialize(to: header)
|
||||
}
|
||||
|
||||
|
||||
@available(SwiftStdlib 5.1, *)
|
||||
@_alwaysEmitIntoClient
|
||||
mutating func _initializeTrailingElements<E>(
|
||||
@@ -64,7 +62,7 @@ where Header: ~Copyable
|
||||
let initialized = unsafe output.finalize(for: rawElements)
|
||||
precondition(count == initialized, "TrailingArray initialization underflow")
|
||||
}
|
||||
|
||||
|
||||
/// Allocate an intrusive managed buffer with the given header and calling
|
||||
/// the initializer to fill in the trailing elements.
|
||||
@available(SwiftStdlib 5.1, *)
|
||||
@@ -76,7 +74,7 @@ where Header: ~Copyable
|
||||
self.init(_headerOnly: header)
|
||||
try _initializeTrailingElements(initializer: initializer)
|
||||
}
|
||||
|
||||
|
||||
/// Allocate an intrusive managed buffer with the given header and
|
||||
/// initializing each trailing element with the given `element`.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -84,7 +82,7 @@ where Header: ~Copyable
|
||||
self.init(_headerOnly: header)
|
||||
rawElements.initialize(repeating: element)
|
||||
}
|
||||
|
||||
|
||||
/// Deinitialize each of the trailing elements, then the header, then
|
||||
/// deallocate the underlying storage.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -93,7 +91,7 @@ where Header: ~Copyable
|
||||
_pointer.deinitialize(count: 1)
|
||||
_storage.deallocate()
|
||||
}
|
||||
|
||||
|
||||
/// Take ownership over a pointer to memory containing the header followed
|
||||
/// by the trailing elements.
|
||||
///
|
||||
@@ -117,7 +115,7 @@ where Header: ~Copyable
|
||||
"header pointer does not account for the alignment of the elements"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
/// Return the pointer to the underlying memory, including ownership over
|
||||
/// that memory. The underlying storage will not be freed by this buffer;
|
||||
/// it is the responsibility of the caller. The header pointer and
|
||||
@@ -133,35 +131,35 @@ where Header: ~Copyable
|
||||
discard self
|
||||
return (pointer, storage)
|
||||
}
|
||||
|
||||
|
||||
/// Access the header portion of the buffer.
|
||||
@_alwaysEmitIntoClient
|
||||
public var header: Header {
|
||||
unsafeAddress {
|
||||
UnsafePointer(_pointer)
|
||||
}
|
||||
|
||||
|
||||
unsafeMutableAddress {
|
||||
_pointer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// The number of trailing elements in the value.
|
||||
@_alwaysEmitIntoClient
|
||||
public var count: Int { header.trailingCount }
|
||||
|
||||
|
||||
/// Starting index for accessing the trailing elements. Always 0
|
||||
@_alwaysEmitIntoClient
|
||||
public var startIndex: Int { 0 }
|
||||
|
||||
|
||||
/// Ending index for accessing the trailing elements. Always `count`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var endIndex: Int { count }
|
||||
|
||||
|
||||
/// Indices covering all of the trailing elements. Always `0..<count`.
|
||||
@_alwaysEmitIntoClient
|
||||
public var indices: Range<Int> { 0..<count }
|
||||
|
||||
|
||||
/// Access the trailing element at the given index.
|
||||
@_alwaysEmitIntoClient
|
||||
public subscript(index: Int) -> Element {
|
||||
@@ -169,13 +167,13 @@ where Header: ~Copyable
|
||||
precondition(index >= 0 && index < count)
|
||||
return rawElements[index]
|
||||
}
|
||||
|
||||
|
||||
set {
|
||||
precondition(index >= 0 && index < count)
|
||||
rawElements[index] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Access the flexible array elements.
|
||||
@_alwaysEmitIntoClient
|
||||
var rawElements: UnsafeMutableBufferPointer<Element> {
|
||||
@@ -185,7 +183,7 @@ where Header: ~Copyable
|
||||
count: header.trailingCount
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
/// Accesses the trailing elements following the header.
|
||||
@available(SwiftStdlib 5.1, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -195,7 +193,7 @@ where Header: ~Copyable
|
||||
_overrideLifetime(rawElements.span, borrowing: self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Accesses the trailing elements following the header, allowing mutation
|
||||
/// of those elements.
|
||||
@available(SwiftStdlib 5.1, *)
|
||||
@@ -207,7 +205,7 @@ where Header: ~Copyable
|
||||
return _overrideLifetime(elements, mutating: &self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Execute the given closure, providing it with an unsafe buffer pointer
|
||||
/// referencing the header.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -216,7 +214,7 @@ where Header: ~Copyable
|
||||
) throws(E) -> R {
|
||||
return try body(_pointer)
|
||||
}
|
||||
|
||||
|
||||
/// Execute the given closure, providing it with an unsafe buffer pointer
|
||||
/// referencing the trailing elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -225,7 +223,7 @@ where Header: ~Copyable
|
||||
) throws(E) -> R {
|
||||
return try body(rawElements)
|
||||
}
|
||||
|
||||
|
||||
/// Execute the given closure, providing it with unsafe pointers to the
|
||||
/// header and trailing elements, respectively.
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -234,7 +232,7 @@ where Header: ~Copyable
|
||||
) throws(E) -> R {
|
||||
return try body(_pointer, rawElements)
|
||||
}
|
||||
|
||||
|
||||
/// Determine the allocation size and alignment needed for the given header
|
||||
/// value along with its trailing elements.
|
||||
///
|
||||
@@ -246,16 +244,16 @@ where Header: ~Copyable
|
||||
// The number of bytes needed to contain the header and elements,
|
||||
// assuming that there are no alignment issues.
|
||||
let numBytes = MemoryLayout<Header>.stride + MemoryLayout<Element>.stride * header.trailingCount
|
||||
|
||||
|
||||
let headerAlignment = MemoryLayout<Header>.alignment
|
||||
let elementAlignment = MemoryLayout<Element>.alignment
|
||||
|
||||
|
||||
// If the header provides sufficient alignment for the elements,
|
||||
// we're done.
|
||||
if elementAlignment <= headerAlignment {
|
||||
return (numBytes, MemoryLayout<Header>.alignment)
|
||||
}
|
||||
|
||||
|
||||
// We may have to slide an allocation by up to the difference between
|
||||
// the element and header alignments to ensure that the elements are
|
||||
// appropriately aligned.
|
||||
@@ -263,20 +261,20 @@ where Header: ~Copyable
|
||||
numBytes + elementAlignment - headerAlignment,
|
||||
MemoryLayout<Element>.alignment)
|
||||
}
|
||||
|
||||
|
||||
/// Given storage that is large enough to accommodate padding + the header
|
||||
/// + the elements, return the header pointer from the storage pointer.
|
||||
@_alwaysEmitIntoClient
|
||||
static func headerPointer(fromStorage storage: UnsafeMutableRawPointer) -> UnsafeMutablePointer<Header> {
|
||||
let headerAlignment = MemoryLayout<Header>.alignment
|
||||
let elementAlignment = MemoryLayout<Element>.alignment
|
||||
|
||||
|
||||
// Normal case: the header has sufficient alignment to include the
|
||||
// elements, so the storage refers directly to the header.
|
||||
if elementAlignment <= headerAlignment {
|
||||
return storage.assumingMemoryBound(to: Header.self)
|
||||
}
|
||||
|
||||
|
||||
// The header is stored right before the elements, with padding bytes
|
||||
// between the start of the allocation and up to the header.
|
||||
return storage.advanced(by: MemoryLayout<Header>.stride)
|
||||
@@ -315,7 +313,7 @@ extension TrailingArray where Header: Copyable {
|
||||
return try body(&value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Create a temporary intrusive managed buffer for the given header, whose
|
||||
/// trailing elements are initialized with the given `initializer` function.
|
||||
/// That instance is provided to the given `body` to operate on for the
|
||||
@@ -351,7 +349,7 @@ extension TrailingArray {
|
||||
body: (inout TrailingArray<Header>) throws(E) -> R
|
||||
) throws(E) -> R {
|
||||
let (numBytes, alignment) = allocationSize(header: header)
|
||||
|
||||
|
||||
// Allocate temporary storage large enough for the value we need.
|
||||
let result: Result<R, E> = withUnsafeTemporaryAllocation(
|
||||
byteCount: numBytes,
|
||||
@@ -361,7 +359,7 @@ extension TrailingArray {
|
||||
let storagePointer = buffer.baseAddress!
|
||||
let headerPointer = TrailingArray.headerPointer(fromStorage: storagePointer)
|
||||
headerPointer.initialize(to: header)
|
||||
|
||||
|
||||
/// Create a trailing array over that temporary storage.
|
||||
var managedBuffer = TrailingArray(consuming: headerPointer,
|
||||
storage: storagePointer)
|
||||
@@ -371,19 +369,19 @@ extension TrailingArray {
|
||||
} catch {
|
||||
resultOrError = .failure(error)
|
||||
}
|
||||
|
||||
|
||||
// Deinitialize the elements and header.
|
||||
managedBuffer.rawElements.deinitialize()
|
||||
managedBuffer._pointer.deinitialize(count: 1)
|
||||
|
||||
|
||||
// Tell the trailing buffer not to free the storage.
|
||||
let (finalPointer, finalStorage) = managedBuffer.leakStorage()
|
||||
precondition(finalPointer == headerPointer)
|
||||
precondition(finalStorage == storagePointer)
|
||||
|
||||
|
||||
return resultOrError
|
||||
}
|
||||
|
||||
|
||||
return try result.get()
|
||||
}
|
||||
}
|
||||
@@ -408,5 +406,3 @@ extension TrailingArray where Header.Element: BitwiseCopyable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,17 +11,13 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
/// Describes a type that has some number of elements following it directly
|
||||
/// in memory. Such types are generally used with the `TrailingArray`
|
||||
/// type, which manages storage for the header and its trailing elements.
|
||||
public protocol TrailingElements: ~Copyable {
|
||||
/// The element type of the data that follows the header in memory.
|
||||
associatedtype Element
|
||||
|
||||
|
||||
/// The number of elements following the header.
|
||||
var trailingCount: Int { get }
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,8 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
/// Represents memory containing a header value followed by some extra padding
|
||||
/// following it. Values of this type own the underlying memory, and are
|
||||
/// non-copyable to ensure that ownership of that memory is unique. Memory
|
||||
@@ -33,7 +31,7 @@ public struct TrailingPadding<Header: ~Copyable>: ~Copyable {
|
||||
/// Pointer to the header, followed by the padding.
|
||||
@usableFromInline
|
||||
let _pointer: UnsafeMutablePointer<Header>
|
||||
|
||||
|
||||
/// Create a new instance with the given header and total size. The total
|
||||
/// size includes the storage for the header itself, so it must be at least
|
||||
/// as large as the header.
|
||||
@@ -46,7 +44,7 @@ public struct TrailingPadding<Header: ~Copyable>: ~Copyable {
|
||||
.assumingMemoryBound(to: Header.self)
|
||||
_pointer.initialize(to: header)
|
||||
}
|
||||
|
||||
|
||||
/// Take ownership over a pointer to memory containing the header followed
|
||||
/// by the trailing elements.
|
||||
///
|
||||
@@ -56,26 +54,26 @@ public struct TrailingPadding<Header: ~Copyable>: ~Copyable {
|
||||
public init(consuming pointer: UnsafeMutablePointer<Header>) {
|
||||
self._pointer = pointer
|
||||
}
|
||||
|
||||
|
||||
/// Deinitializes the header, then deallocates the underlying memory.
|
||||
@_alwaysEmitIntoClient
|
||||
deinit {
|
||||
_pointer.deinitialize(count: 1)
|
||||
_pointer.deallocate()
|
||||
}
|
||||
|
||||
|
||||
/// Access the header portion of the value.
|
||||
@_alwaysEmitIntoClient
|
||||
public var header: Header {
|
||||
unsafeAddress {
|
||||
UnsafePointer(_pointer)
|
||||
}
|
||||
|
||||
|
||||
unsafeMutableAddress {
|
||||
_pointer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Executes the given closure with the pointer to the header itself.
|
||||
@_alwaysEmitIntoClient
|
||||
public func withUnsafeMutablePointerToHeader<R: ~Copyable, E>(
|
||||
@@ -83,7 +81,7 @@ public struct TrailingPadding<Header: ~Copyable>: ~Copyable {
|
||||
) throws(E) -> R {
|
||||
return try body(_pointer)
|
||||
}
|
||||
|
||||
|
||||
/// Take ownership over the stored memory, returning its pointer. The
|
||||
/// underlying storage will not be freed by the `TrailingPadding` instance,
|
||||
/// as it is the responsibility of the caller.
|
||||
@@ -112,7 +110,7 @@ extension TrailingPadding where Header: Copyable {
|
||||
) throws(E) -> R {
|
||||
precondition(size >= MemoryLayout<Header>.size,
|
||||
"must allocate enough storage for the underlying stored type")
|
||||
|
||||
|
||||
// Allocate temporary storage large enough for the value we need.
|
||||
let result: Result<R, E> = withUnsafeTemporaryAllocation(
|
||||
byteCount: size,
|
||||
@@ -122,26 +120,24 @@ extension TrailingPadding where Header: Copyable {
|
||||
let pointer = buffer.baseAddress!.assumingMemoryBound(to: Header.self)
|
||||
pointer.initialize(to: header)
|
||||
var tailAllocated = TrailingPadding(consuming: pointer)
|
||||
|
||||
|
||||
do throws(E) {
|
||||
let result = try body(&tailAllocated)
|
||||
|
||||
|
||||
// Tell the tail-allocated buffer not to free the storage.
|
||||
let finalPointer = tailAllocated.leakStorage()
|
||||
precondition(finalPointer == pointer)
|
||||
|
||||
|
||||
return .success(result)
|
||||
} catch {
|
||||
// Tell the tail-allocated buffer not to free the storage.
|
||||
let finalPointer = tailAllocated.leakStorage()
|
||||
precondition(finalPointer == pointer)
|
||||
|
||||
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return try result.get()
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user