[TrailingElementsModule] Underscore internal, ABI-exposed stored properties

This commit is contained in:
Doug Gregor committed 2025-09-08 18:52:47 -07:00
1 parent 2e7fb3e12a
commit ad56b1c8c4
2 files changed
+31 -31

No files matched your search

@@ -12,7 +12,7 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
{
/// The underlying storage.
@usableFromInline
var pointer: UnsafeMutablePointer<Header>
var _pointer: UnsafeMutablePointer<Header>
/// The element type stored within the buffer.
public typealias Element = Header.Element
@@ -22,14 +22,14 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
/// memory safety.
@_alwaysEmitIntoClient
private init(_headerOnly header: consuming Header) {
pointer = UnsafeMutableRawPointer.allocate(
_pointer = UnsafeMutableRawPointer.allocate(
byteCount: Self.allocationSize(
header: header
),
alignment: MemoryLayout<Header>.alignment
).assumingMemoryBound(to: Header.self)
pointer.initialize(to: header)
_pointer.initialize(to: header)
}
@available(SwiftStdlib 5.1, *)
@@ -68,8 +68,8 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
@_alwaysEmitIntoClient
deinit {
rawElements.deinitialize()
pointer.deinitialize(count: 1)
pointer.deallocate()
_pointer.deinitialize(count: 1)
_pointer.deallocate()
}
/// Take ownership over a pointer to memory containing the header followed
@@ -79,7 +79,7 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
/// be freed.
@_alwaysEmitIntoClient
public init(consuming pointer: UnsafeMutablePointer<Header>) {
self.pointer = pointer
self._pointer = pointer
}
/// Return the pointer to the underlying memory, including ownership over
@@ -87,7 +87,7 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
/// it is the responsibility of the caller.
@_alwaysEmitIntoClient
public consuming func takePointer() -> UnsafeMutablePointer<Header> {
let pointer = self.pointer
let pointer = self._pointer
discard self
return pointer
}
@@ -96,11 +96,11 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
@_alwaysEmitIntoClient
public var header: Header {
unsafeAddress {
UnsafePointer(pointer)
UnsafePointer(_pointer)
}
unsafeMutableAddress {
pointer
_pointer
}
}
@@ -138,7 +138,7 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
@_alwaysEmitIntoClient
var rawElements: UnsafeMutableBufferPointer<Element> {
UnsafeMutableBufferPointer(
start: UnsafeMutableRawPointer(pointer.advanced(by: 1))
start: UnsafeMutableRawPointer(_pointer.advanced(by: 1))
.assumingMemoryBound(to: Element.self),
count: header.trailingCount
)
@@ -170,7 +170,7 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
/// referencing the header.
@_alwaysEmitIntoClient
public mutating func withUnsafeMutablePointerToHeader<E, R: ~Copyable>(_ body: (UnsafeMutablePointer<Header>) throws(E) -> R) throws(E) -> R {
return try body(pointer)
return try body(_pointer)
}
/// Execute the given closure, providing it with an unsafe buffer pointer
@@ -186,7 +186,7 @@ public struct IntrusiveManagedBuffer<Header: TrailingElements>: ~Copyable
public mutating func withUnsafeMutablePointers<E, R: ~Copyable>(
_ body: (UnsafeMutablePointer<Header>, UnsafeMutableBufferPointer<Element>) throws(E) -> R
) throws(E) -> R {
return try body(pointer, rawElements)
return try body(_pointer, rawElements)
}
/// Determine the allocation size needed for the given header value.
@@ -223,7 +223,7 @@ extension IntrusiveManagedBuffer where Header: Copyable {
totalSize: allocationSize(header: header)
) { (storage) throws(E) in
/// Create a managed buffer over that temporary storage.
var managedBuffer = IntrusiveManagedBuffer(consuming: storage.pointer)
var managedBuffer = IntrusiveManagedBuffer(consuming: storage._pointer)
managedBuffer.rawElements.initialize(repeating: element)
do throws(E) {
@@ -231,13 +231,13 @@ extension IntrusiveManagedBuffer where Header: Copyable {
// Tell the managed buffer not to free the storage.
let finalPointer = managedBuffer.takePointer()
precondition(finalPointer == storage.pointer)
precondition(finalPointer == storage._pointer)
return result
} catch {
// Tell the managed buffer not to free the storage.
let finalPointer = managedBuffer.takePointer()
precondition(finalPointer == storage.pointer)
precondition(finalPointer == storage._pointer)
throw error
}
@@ -261,7 +261,7 @@ extension IntrusiveManagedBuffer where Header: Copyable {
totalSize: allocationSize(header: header)
) { (storage) throws(E) in
/// Create a managed buffer over that temporary storage.
var managedBuffer = IntrusiveManagedBuffer(consuming: storage.pointer)
var managedBuffer = IntrusiveManagedBuffer(consuming: storage._pointer)
try managedBuffer._initializeTrailingElements(initializer: initializer)
do throws(E) {
@@ -269,13 +269,13 @@ extension IntrusiveManagedBuffer where Header: Copyable {
// Tell the managed buffer not to free the storage.
let finalPointer = managedBuffer.takePointer()
precondition(finalPointer == storage.pointer)
precondition(finalPointer == storage._pointer)
return result
} catch {
// Tell the managed buffer not to free the storage.
let finalPointer = managedBuffer.takePointer()
precondition(finalPointer == storage.pointer)
precondition(finalPointer == storage._pointer)
throw error
}
@@ -301,20 +301,20 @@ extension IntrusiveManagedBuffer where Header.Element: BitwiseCopyable {
totalSize: allocationSize(header: header)
) { (storage) throws(E) in
/// Create a managed buffer over that temporary storage.
var managedBuffer = IntrusiveManagedBuffer(consuming: storage.pointer)
var managedBuffer = IntrusiveManagedBuffer(consuming: storage._pointer)
do throws(E) {
let result = try body(&managedBuffer)
// Tell the managed buffer not to free the storage.
let finalPointer = managedBuffer.takePointer()
precondition(finalPointer == storage.pointer)
precondition(finalPointer == storage._pointer)
return result
} catch {
// Tell the managed buffer not to free the storage.
let finalPointer = managedBuffer.takePointer()
precondition(finalPointer == storage.pointer)
precondition(finalPointer == storage._pointer)
throw error
}
@@ -17,7 +17,7 @@
public struct PaddedStorage<Header: ~Copyable>: ~Copyable {
/// Pointer to the header, followed by the padding.
@usableFromInline
let pointer: UnsafeMutablePointer<Header>
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
@@ -26,9 +26,9 @@ public struct PaddedStorage<Header: ~Copyable>: ~Copyable {
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)
_pointer = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: MemoryLayout<Header>.alignment)
.assumingMemoryBound(to: Header.self)
pointer.initialize(to: header)
_pointer.initialize(to: header)
}
/// Take ownership over a pointer to memory containing the header followed
@@ -38,32 +38,32 @@ public struct PaddedStorage<Header: ~Copyable>: ~Copyable {
/// deinitialized and freed.
@_alwaysEmitIntoClient
public init(consuming pointer: UnsafeMutablePointer<Header>) {
self.pointer = pointer
self._pointer = pointer
}
/// Deinitializes the header, then deallocates the underlying memory.
@_alwaysEmitIntoClient
deinit {
pointer.deinitialize(count: 1)
pointer.deallocate()
_pointer.deinitialize(count: 1)
_pointer.deallocate()
}
/// Access the header portion of the value.
@_alwaysEmitIntoClient
public var header: Header {
unsafeAddress {
UnsafePointer(pointer)
UnsafePointer(_pointer)
}
unsafeMutableAddress {
pointer
_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)
return try body(_pointer)
}
/// Take ownership over the stored memory, returning its pointer. The
@@ -71,7 +71,7 @@ public struct PaddedStorage<Header: ~Copyable>: ~Copyable {
/// as it is the responsibility of the caller.
@_alwaysEmitIntoClient
public consuming func takePointer() -> UnsafeMutablePointer<Header> {
let pointer = self.pointer
let pointer = self._pointer
discard self
return pointer
}