Reindent TrailingElementsModule

This commit is contained in:
Karoy Lorentey committed 2025-09-26 22:38:16 -07:00
1 parent 346934794a
commit 31d3abcc22
4 files changed
+609 -583

No files matched your search

+359 -348
View File
@@ -19,377 +19,388 @@
/// it is not stored separately.
@frozen
public struct TrailingArray<Header: TrailingElements>: ~Copyable
where Header: ~Copyable
where Header: ~Copyable
{
/// A pointer to the raw, underlying storage. This may point before the
/// Header instance in cases where the alignment of Element exceeds that
/// 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)
/// A pointer to the raw, underlying storage. This may point before the
/// Header instance in cases where the alignment of Element exceeds that
/// 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.
@_alwaysEmitIntoClient
private init(_headerOnly header: consuming Header) {
let (bytes, alignment) = Self.allocationSize(header: header)
_storage = UnsafeMutableRawPointer.allocate(
byteCount: bytes,
alignment: alignment
)
_pointer.initialize(to: header)
}
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
mutating func _initializeTrailingElements<E>(
initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
var output = unsafe OutputSpan(buffer: rawElements, initializedCount: 0)
try initializer(&output)
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, *)
@_alwaysEmitIntoClient
public init<E>(
header: consuming Header,
initializingTrailingElementsWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
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
public init(header: consuming Header, repeating element: Element) {
self.init(_headerOnly: header)
rawElements.initialize(repeating: element)
}
/// Deinitialize each of the trailing elements, then the header, then
/// deallocate the underlying storage.
@_alwaysEmitIntoClient
deinit {
rawElements.deinitialize()
_pointer.deinitialize(count: 1)
_storage.deallocate()
}
/// Take ownership over a pointer to memory containing the header followed
/// by the trailing elements.
///
/// Once this managed buffer instance is no longer used, this memory will
/// be freed.
/// - Parameters:
/// - pointer: A pointer to the header, which should already have been
/// initialized by the caller.
/// - storage: A pointer to the storage containing the header and
/// pointers. This will usually be `UnsafeMutableRawPointer(pointer)`,
/// but may precede `pointer` if the elements have greater alignment
/// than the header.
@_alwaysEmitIntoClient
public init(
consuming pointer: UnsafeMutablePointer<Header>,
storage: UnsafeMutableRawPointer
) {
self._storage = storage
precondition(
self._pointer == pointer,
"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
/// underlying storage are returned separately, to handle cases where
/// the alignment of the elements is greater than than of the header.
@_alwaysEmitIntoClient
public consuming func leakStorage() -> (
pointer: UnsafeMutablePointer<Header>,
storage: UnsafeMutableRawPointer
) {
let pointer = self._pointer
let storage = _storage
discard self
return (pointer, storage)
}
/// Access the header portion of the buffer.
@_alwaysEmitIntoClient
public var header: Header {
unsafeAddress {
UnsafePointer(_pointer)
}
/// 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.
@_alwaysEmitIntoClient
private init(_headerOnly header: consuming Header) {
let (bytes, alignment) = Self.allocationSize(header: header)
_storage = UnsafeMutableRawPointer.allocate(
byteCount: bytes,
alignment: alignment
)
_pointer.initialize(to: header)
unsafeMutableAddress {
_pointer
}
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
mutating func _initializeTrailingElements<E>(
initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
var output = unsafe OutputSpan(buffer: rawElements, initializedCount: 0)
try initializer(&output)
let initialized = unsafe output.finalize(for: rawElements)
precondition(count == initialized, "TrailingArray initialization underflow")
}
/// 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 {
get {
precondition(index >= 0 && index < count)
return rawElements[index]
}
/// Allocate an intrusive managed buffer with the given header and calling
/// the initializer to fill in the trailing elements.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public init<E>(
header: consuming Header,
initializingTrailingElementsWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
self.init(_headerOnly: header)
try _initializeTrailingElements(initializer: initializer)
set {
precondition(index >= 0 && index < count)
rawElements[index] = newValue
}
/// Allocate an intrusive managed buffer with the given header and
/// initializing each trailing element with the given `element`.
@_alwaysEmitIntoClient
public init(header: consuming Header, repeating element: Element) {
self.init(_headerOnly: header)
rawElements.initialize(repeating: element)
}
/// Access the flexible array elements.
@_alwaysEmitIntoClient
var rawElements: UnsafeMutableBufferPointer<Element> {
UnsafeMutableBufferPointer(
start: UnsafeMutableRawPointer(_pointer.advanced(by: 1))
.assumingMemoryBound(to: Element.self),
count: header.trailingCount
)
}
/// Accesses the trailing elements following the header.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public var elements: Span<Element> {
@_lifetime(self)
get {
_overrideLifetime(rawElements.span, borrowing: self)
}
/// Deinitialize each of the trailing elements, then the header, then
/// deallocate the underlying storage.
@_alwaysEmitIntoClient
deinit {
rawElements.deinitialize()
_pointer.deinitialize(count: 1)
_storage.deallocate()
}
/// Accesses the trailing elements following the header, allowing mutation
/// of those elements.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public var mutableElements: MutableSpan<Element> {
@_lifetime(self)
mutating get {
let elements = self.rawElements.mutableSpan
return _overrideLifetime(elements, mutating: &self)
}
/// Take ownership over a pointer to memory containing the header followed
/// by the trailing elements.
///
/// Once this managed buffer instance is no longer used, this memory will
/// be freed.
/// - Parameters:
/// - pointer: A pointer to the header, which should already have been
/// initialized by the caller.
/// - storage: A pointer to the storage containing the header and
/// pointers. This will usually be `UnsafeMutableRawPointer(pointer)`,
/// but may precede `pointer` if the elements have greater alignment
/// than the header.
@_alwaysEmitIntoClient
public init(
consuming pointer: UnsafeMutablePointer<Header>,
storage: UnsafeMutableRawPointer
) {
self._storage = storage
precondition(
self._pointer == pointer,
"header pointer does not account for the alignment of the elements"
)
}
/// Execute the given closure, providing it with an unsafe buffer pointer
/// referencing the header.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointerToHeader<E, R: ~Copyable>(
_ body: (UnsafeMutablePointer<Header>) throws(E) -> R
) throws(E) -> R {
return try body(_pointer)
}
/// Execute the given closure, providing it with an unsafe buffer pointer
/// referencing the trailing elements.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointerToElements<E, R: ~Copyable>(
_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> R
) throws(E) -> R {
return try body(rawElements)
}
/// Execute the given closure, providing it with unsafe pointers to the
/// header and trailing elements, respectively.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointers<E, R: ~Copyable>(
_ body: (UnsafeMutablePointer<Header>, UnsafeMutableBufferPointer<Element>) throws(E) -> R
) 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.
///
/// In cases where the element's alignment exceeds that of the header,
/// this operation will suggest overallocation so that the elements can be
/// properly aligned following the header.
@_alwaysEmitIntoClient
static func allocationSize(header: borrowing Header) -> (size: Int, alignment: Int) {
// 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)
}
/// 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
/// underlying storage are returned separately, to handle cases where
/// the alignment of the elements is greater than than of the header.
@_alwaysEmitIntoClient
public consuming func leakStorage() -> (
pointer: UnsafeMutablePointer<Header>,
storage: UnsafeMutableRawPointer
) {
let pointer = self._pointer
let storage = _storage
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 {
get {
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> {
UnsafeMutableBufferPointer(
start: UnsafeMutableRawPointer(_pointer.advanced(by: 1))
.assumingMemoryBound(to: Element.self),
count: header.trailingCount
)
}
/// Accesses the trailing elements following the header.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public var elements: Span<Element> {
@_lifetime(self)
get {
_overrideLifetime(rawElements.span, borrowing: self)
}
}
/// Accesses the trailing elements following the header, allowing mutation
/// of those elements.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public var mutableElements: MutableSpan<Element> {
@_lifetime(self)
mutating get {
let elements = self.rawElements.mutableSpan
return _overrideLifetime(elements, mutating: &self)
}
}
/// Execute the given closure, providing it with an unsafe buffer pointer
/// referencing the header.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointerToHeader<E, R: ~Copyable>(_ body: (UnsafeMutablePointer<Header>) throws(E) -> R) throws(E) -> R {
return try body(_pointer)
}
/// Execute the given closure, providing it with an unsafe buffer pointer
/// referencing the trailing elements.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointerToElements<E, R: ~Copyable>(_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> R) throws(E) -> R {
return try body(rawElements)
}
/// Execute the given closure, providing it with unsafe pointers to the
/// header and trailing elements, respectively.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointers<E, R: ~Copyable>(
_ body: (UnsafeMutablePointer<Header>, UnsafeMutableBufferPointer<Element>) throws(E) -> R
) 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.
///
/// In cases where the element's alignment exceeds that of the header,
/// this operation will suggest overallocation so that the elements can be
/// properly aligned following the header.
@_alwaysEmitIntoClient
static func allocationSize(header: borrowing Header) -> (size: Int, alignment: Int) {
// 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.
return (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)
.alignedUp(for: Element.self)
.advanced(by: -MemoryLayout<Header>.stride)
.assumingMemoryBound(to: Header.self)
// 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.
return (
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)
.alignedUp(for: Element.self)
.advanced(by: -MemoryLayout<Header>.stride)
.assumingMemoryBound(to: Header.self)
}
}
extension TrailingArray where Header: ~Copyable, Header.Element: BitwiseCopyable {
/// Allocate an intrusive managed buffer with the given header, but leaving the
/// trailing elements uninitialized.
@_alwaysEmitIntoClient
@unsafe
public init(header: consuming Header, uninitializedTrailingElements: ()) {
self.init(_headerOnly: header)
}
/// Allocate an intrusive managed buffer with the given header, but leaving the
/// trailing elements uninitialized.
@_alwaysEmitIntoClient
@unsafe
public init(header: consuming Header, uninitializedTrailingElements: ()) {
self.init(_headerOnly: header)
}
}
extension TrailingArray where Header: Copyable {
/// Create a temporary intrusive managed buffer for the given header, whose
/// trailing elements are initialized to copies of `element`. That instance
/// is provided to the given `body` to operate on for the duration of the
/// call. The temporary is allocated on the stack, unless it is very
/// large according to `withUnsafeTemporaryAllocation`.
@_alwaysEmitIntoClient
public static func withTemporaryValue<R: ~Copyable, E>(
header: consuming Header,
repeating element: Element,
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return try _withTemporaryValue(header: header, uninitializedTrailingElements: ()) { (value) throws(E) in
value.rawElements.initialize(repeating: element)
return try body(&value)
}
/// Create a temporary intrusive managed buffer for the given header, whose
/// trailing elements are initialized to copies of `element`. That instance
/// is provided to the given `body` to operate on for the duration of the
/// call. The temporary is allocated on the stack, unless it is very
/// large according to `withUnsafeTemporaryAllocation`.
@_alwaysEmitIntoClient
public static func withTemporaryValue<R: ~Copyable, E>(
header: consuming Header,
repeating element: Element,
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return try _withTemporaryValue(
header: header, uninitializedTrailingElements: ()
) { (value) throws(E) in
value.rawElements.initialize(repeating: element)
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
/// duration of the call. The temporary is allocated on the stack, unless
/// it is very large according to `withUnsafeTemporaryAllocation`.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public static func withTemporaryValue<R: ~Copyable, E>(
header: consuming Header,
initializingTrailingElementsWith initializer: (inout OutputSpan<Element>) throws(E) -> Void,
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return try _withTemporaryValue(header: header, uninitializedTrailingElements: ()) { (value) throws(E) in
try value._initializeTrailingElements(initializer: initializer)
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
/// duration of the call. The temporary is allocated on the stack, unless
/// it is very large according to `withUnsafeTemporaryAllocation`.
@available(SwiftStdlib 5.1, *)
@_alwaysEmitIntoClient
public static func withTemporaryValue<R: ~Copyable, E>(
header: consuming Header,
initializingTrailingElementsWith initializer: (inout OutputSpan<Element>) throws(E) -> Void,
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return try _withTemporaryValue(
header: header, uninitializedTrailingElements: ()
) { (value) throws(E) in
try value._initializeTrailingElements(initializer: initializer)
return try body(&value)
}
}
}
extension TrailingArray {
/// Create a temporary intrusive managed buffer for the given header, whose
/// trailing elements are left uninitialized. That instance is provided to
/// the given `body` to operate on for the duration of the
/// call. The temporary is allocated on the stack, unless it is very
/// large according to `withUnsafeTemporaryAllocation`.
@_alwaysEmitIntoClient
@unsafe
static func _withTemporaryValue<R: ~Copyable, E>(
header: Header,
uninitializedTrailingElements: (),
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,
alignment: alignment
) { buffer in
// Initialize the header within the temporary allocation.
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)
let resultOrError: Result<R, E>
do throws(E) {
resultOrError = .success(try body(&managedBuffer))
} 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()
/// Create a temporary intrusive managed buffer for the given header, whose
/// trailing elements are left uninitialized. That instance is provided to
/// the given `body` to operate on for the duration of the
/// call. The temporary is allocated on the stack, unless it is very
/// large according to `withUnsafeTemporaryAllocation`.
@_alwaysEmitIntoClient
@unsafe
static func _withTemporaryValue<R: ~Copyable, E>(
header: Header,
uninitializedTrailingElements: (),
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,
alignment: alignment
) { buffer in
// Initialize the header within the temporary allocation.
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)
let resultOrError: Result<R, E>
do throws(E) {
resultOrError = .success(try body(&managedBuffer))
} 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()
}
}
extension TrailingArray where Header.Element: BitwiseCopyable {
/// Create a temporary intrusive managed buffer for the given header, whose
/// trailing elements are left uninitialized. That instance is provided to
/// the given `body` to operate on for the duration of the
/// call. The temporary is allocated on the stack, unless it is very
/// large according to `withUnsafeTemporaryAllocation`.
@_alwaysEmitIntoClient
@unsafe
public static func withTemporaryValue<R: ~Copyable, E>(
header: consuming Header,
uninitializedTrailingElements: (),
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return try _withTemporaryValue(header: header, uninitializedTrailingElements: ()) { (value) throws(E) in
try body(&value)
}
/// Create a temporary intrusive managed buffer for the given header, whose
/// trailing elements are left uninitialized. That instance is provided to
/// the given `body` to operate on for the duration of the
/// call. The temporary is allocated on the stack, unless it is very
/// large according to `withUnsafeTemporaryAllocation`.
@_alwaysEmitIntoClient
@unsafe
public static func withTemporaryValue<R: ~Copyable, E>(
header: consuming Header,
uninitializedTrailingElements: (),
body: (inout TrailingArray<Header>) throws(E) -> R
) throws(E) -> R {
return try _withTemporaryValue(
header: header, uninitializedTrailingElements: ()
) { (value) throws(E) in
try body(&value)
}
}
}
@@ -13,9 +13,9 @@
/// 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 }
/// 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 }
}
@@ -26,112 +26,116 @@
/// `IntrusiveManagedPointer` instead.
@frozen
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.
@_alwaysEmitIntoClient
public init(header: consuming Header, totalSize size: Int) {
precondition(size >= MemoryLayout<Header>.size,
"must allocate enough storage for the underlying stored type")
_pointer = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: MemoryLayout<Header>.alignment)
.assumingMemoryBound(to: Header.self)
_pointer.initialize(to: header)
/// 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.
@_alwaysEmitIntoClient
public init(header: consuming Header, totalSize size: Int) {
precondition(size >= MemoryLayout<Header>.size,
"must allocate enough storage for the underlying stored type")
_pointer = UnsafeMutableRawPointer
.allocate(byteCount: size, alignment: MemoryLayout<Header>.alignment)
.assumingMemoryBound(to: Header.self)
_pointer.initialize(to: header)
}
/// Take ownership over a pointer to memory containing the header followed
/// by the trailing elements.
///
/// Once this padded storage is no longer used, this memory will be
/// deinitialized and freed.
@_alwaysEmitIntoClient
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)
}
/// Take ownership over a pointer to memory containing the header followed
/// by the trailing elements.
///
/// Once this padded storage is no longer used, this memory will be
/// deinitialized and freed.
@_alwaysEmitIntoClient
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>(_ body: (UnsafeMutablePointer<Header>) throws(E) -> R) 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.
@_alwaysEmitIntoClient
public consuming func leakStorage() -> UnsafeMutablePointer<Header> {
let pointer = self._pointer
discard self
return pointer
unsafeMutableAddress {
_pointer
}
}
/// Executes the given closure with the pointer to the header itself.
@_alwaysEmitIntoClient
public func withUnsafeMutablePointerToHeader<R: ~Copyable, E>(
_ body: (UnsafeMutablePointer<Header>) throws(E) -> R
) 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.
@_alwaysEmitIntoClient
public consuming func leakStorage() -> UnsafeMutablePointer<Header> {
let pointer = self._pointer
discard self
return pointer
}
}
extension TrailingPadding: @unchecked Sendable where Header: Sendable, Header: ~Copyable { }
extension TrailingPadding: @unchecked Sendable
where Header: Sendable, Header: ~Copyable { }
extension TrailingPadding where Header: Copyable {
/// Create a temporary for the given header with additional storage,
/// providing that instance to the given `body` to operate on for the
/// duration. The temporary is allocated on the stack (unless it is very
/// large), eliminating the need for heap allocation of variable-size
/// values.
@_alwaysEmitIntoClient
public static func withTemporaryValue<R: ~Copyable, E>(
header: Header,
totalSize size: Int,
body: (inout TrailingPadding<Header>) throws(E) -> R
) 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,
alignment: MemoryLayout<Header>.alignment
) { buffer in
/// Create a tail-allocated storage over that temporary storage.
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()
/// Create a temporary for the given header with additional storage,
/// providing that instance to the given `body` to operate on for the
/// duration. The temporary is allocated on the stack (unless it is very
/// large), eliminating the need for heap allocation of variable-size
/// values.
@_alwaysEmitIntoClient
public static func withTemporaryValue<R: ~Copyable, E>(
header: Header,
totalSize size: Int,
body: (inout TrailingPadding<Header>) throws(E) -> R
) 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,
alignment: MemoryLayout<Header>.alignment
) { buffer in
/// Create a tail-allocated storage over that temporary storage.
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()
}
}
@@ -12,156 +12,167 @@ import Testing
import TrailingElementsModule
struct Point {
var x: Int
var y: Int
var x: Int
var y: Int
}
struct Coordinates {
var numCoordinates: Int
var numCoordinates: Int
}
extension Coordinates: TrailingElements {
typealias Element = Point
var trailingCount: Int { numCoordinates }
typealias Element = Point
var trailingCount: Int { numCoordinates }
}
struct OneByteWithPointers: TrailingElements {
var byte: UInt8
init(count: Int) {
self.byte = UInt8(count)
}
typealias Element = OpaquePointer
var trailingCount: Int { Int(byte) }
var byte: UInt8
init(count: Int) {
self.byte = UInt8(count)
}
typealias Element = OpaquePointer
var trailingCount: Int { Int(byte) }
}
struct ManyBytesWithPointers: TrailingElements {
var bytes: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
init(count: Int) {
self.bytes = (UInt8(count), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}
typealias Element = OpaquePointer
var trailingCount: Int { Int(bytes.0) }
var bytes: (
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8)
init(count: Int) {
self.bytes = (
UInt8(count), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}
typealias Element = OpaquePointer
var trailingCount: Int { Int(bytes.0) }
}
@Suite("Trailing elements tests")
struct TrailingElementsTests {
@Test func simpleCoordinates() {
var coords = TrailingArray(header: Coordinates(numCoordinates: 3)) { outputSpan in
outputSpan.append(Point(x: 1, y: 2))
outputSpan.append(Point(x: 2, y: 3))
outputSpan.append(Point(x: 3, y: 4))
}
#expect(coords[0].x == 1)
#expect(coords[1].x == 2)
#expect(coords[2].x == 3)
coords[1].y = 17
#expect(coords[1].y == 17)
@Test func simpleCoordinates() {
var coords = TrailingArray(
header: Coordinates(numCoordinates: 3)
) { outputSpan in
outputSpan.append(Point(x: 1, y: 2))
outputSpan.append(Point(x: 2, y: 3))
outputSpan.append(Point(x: 3, y: 4))
}
@Test func repeatingInit() {
var coords = TrailingArray(header: Coordinates(numCoordinates: 3), repeating: Point(x: 1, y: 1))
#expect(coords[0].x == 1)
#expect(coords[1].x == 1)
#expect(coords[2].x == 1)
coords[1].x = 17
#expect(coords[0].x == 1)
#expect(coords[1].x == 17)
#expect(coords[2].x == 1)
#expect(coords[0].x == 1)
#expect(coords[1].x == 2)
#expect(coords[2].x == 3)
coords[1].y = 17
#expect(coords[1].y == 17)
}
@Test func repeatingInit() {
var coords = TrailingArray(
header: Coordinates(numCoordinates: 3), repeating: Point(x: 1, y: 1))
#expect(coords[0].x == 1)
#expect(coords[1].x == 1)
#expect(coords[2].x == 1)
coords[1].x = 17
#expect(coords[0].x == 1)
#expect(coords[1].x == 17)
#expect(coords[2].x == 1)
}
@Test func temporaryCoordinates() {
TrailingArray.withTemporaryValue(
header: Coordinates(numCoordinates: 3)
) { outputSpan in
outputSpan.append(Point(x: 1, y: 2))
outputSpan.append(Point(x: 2, y: 3))
outputSpan.append(Point(x: 3, y: 4))
} body: { coords in
#expect(coords[0].x == 1)
#expect(coords[1].x == 2)
#expect(coords[2].x == 3)
}
@Test func temporaryCoordinates() {
TrailingArray.withTemporaryValue(header: Coordinates(numCoordinates: 3)) { outputSpan in
outputSpan.append(Point(x: 1, y: 2))
outputSpan.append(Point(x: 2, y: 3))
outputSpan.append(Point(x: 3, y: 4))
} body: { coords in
#expect(coords[0].x == 1)
#expect(coords[1].x == 2)
#expect(coords[2].x == 3)
}
}
@Test(arguments: 0 ... 10)
func underalignedByteOnHeap(count: Int) {
var pointers = TrailingArray(
header: OneByteWithPointers(count: count)
) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
}
@Test(arguments: 0 ... 10)
func underalignedByteOnHeap(count: Int) {
var pointers = TrailingArray(header: OneByteWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
}
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
#expect(
UnsafeMutableRawPointer(headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)) ==
UnsafeMutableRawPointer(elementsPtr.baseAddress))
}
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
@Test(arguments: 0 ... 10)
func underalignedByteOnStack(count: Int) {
TrailingArray.withTemporaryValue(header: OneByteWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
} body: { pointers in
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
#expect(
UnsafeMutableRawPointer(headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)) ==
UnsafeMutableRawPointer(elementsPtr.baseAddress))
}
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
let p1 = headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
let p2 = elementsPtr.baseAddress
#expect(UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
}
@Test(arguments: 0 ... 10)
func underalignedBytesOnHeap(count: Int) {
var pointers = TrailingArray(header: ManyBytesWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
}
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
#expect(
UnsafeMutableRawPointer(headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)) ==
UnsafeMutableRawPointer(elementsPtr.baseAddress))
}
}
@Test(arguments: 0 ... 10)
func underalignedByteOnStack(count: Int) {
TrailingArray.withTemporaryValue(header: OneByteWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
} body: { pointers in
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
let p1 = headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
let p2 = elementsPtr.baseAddress
#expect(UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
}
}
@Test(arguments: 0 ... 10)
func underalignedBytesOnStack(count: Int) {
TrailingArray.withTemporaryValue(header: ManyBytesWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
} body: { pointers in
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
#expect(
UnsafeMutableRawPointer(headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)) ==
UnsafeMutableRawPointer(elementsPtr.baseAddress))
}
}
}
@Test(arguments: 0 ... 10)
func underalignedBytesOnHeap(count: Int) {
var pointers = TrailingArray(header: ManyBytesWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
}
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
let p1 = headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
let p2 = elementsPtr.baseAddress
#expect(UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
}
}
@Test(arguments: 0 ... 10)
func underalignedBytesOnStack(count: Int) {
TrailingArray.withTemporaryValue(header: ManyBytesWithPointers(count: count)) { outputSpan in
for i in 0..<count {
outputSpan.append(OpaquePointer(bitPattern: i+1)!)
}
} body: { pointers in
for i in 0..<count {
#expect(pointers[i] == OpaquePointer(bitPattern: i+1))
}
pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
let p1 = headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
let p2 = elementsPtr.baseAddress
#expect(UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
}
}
}
}