Skip to content

Commit ce62475

Browse files
committed
[stdlib] ManagedBuffer: Support for noncopyable Element
Header is tricky, but it’s on the TODO list for later.
1 parent d7c402f commit ce62475

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

stdlib/public/core/ManagedBuffer.swift

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ internal func _swift_bufferAllocate(
3535
/// - Note: Subclasses must not have any stored properties; any storage
3636
/// needed should be included in `Header`.
3737
@_fixed_layout
38-
open class ManagedBuffer<Header, Element> {
38+
open class ManagedBuffer<Header, Element: ~Copyable> {
3939
/// The stored `Header` instance.
4040
///
4141
/// During instance creation, in particular during
4242
/// `ManagedBuffer.create`'s call to initialize, `ManagedBuffer`'s
4343
/// `header` property is as-yet uninitialized, and therefore
4444
/// reading the `header` property during `ManagedBuffer.create` is undefined.
45+
@_preInverseGenerics
4546
public final var header: Header
4647

4748
#if $Embedded
@@ -54,23 +55,26 @@ open class ManagedBuffer<Header, Element> {
5455
// This is really unfortunate. In Swift 5.0, the method descriptor for this
5556
// initializer was public and subclasses would "inherit" it, referencing its
5657
// method descriptor from their class override table.
58+
@_preInverseGenerics
5759
@usableFromInline
5860
internal init(_doNotCallMe: ()) {
5961
_internalInvariantFailure("Only initialize these by calling create")
6062
}
6163
#endif
6264

65+
@_preInverseGenerics
6366
@inlinable
6467
deinit {}
6568
}
6669

6770
@available(*, unavailable)
68-
extension ManagedBuffer: Sendable {}
71+
extension ManagedBuffer: Sendable where Element: ~Copyable {}
6972

70-
extension ManagedBuffer {
73+
extension ManagedBuffer where Element: ~Copyable {
7174
/// Create a new instance of the most-derived class, calling
7275
/// `factory` on the partially-constructed object to generate
7376
/// an initial `Header`.
77+
@_preInverseGenerics
7478
@inlinable
7579
public final class func create(
7680
minimumCapacity: Int,
@@ -96,6 +100,7 @@ extension ManagedBuffer {
96100
/// This header may be nontrivial to compute; it is usually a good
97101
/// idea to store this information in the "header" area when
98102
/// an instance is created.
103+
@_preInverseGenerics
99104
@inlinable
100105
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
101106
public final var capacity: Int {
@@ -106,12 +111,14 @@ extension ManagedBuffer {
106111
return realCapacity
107112
}
108113

114+
@_preInverseGenerics
109115
@inlinable
110116
internal final var firstElementAddress: UnsafeMutablePointer<Element> {
111117
return UnsafeMutablePointer(
112118
Builtin.projectTailElems(self, Element.self))
113119
}
114120

121+
@_preInverseGenerics
115122
@inlinable
116123
internal final var headerAddress: UnsafeMutablePointer<Header> {
117124
return UnsafeMutablePointer<Header>(Builtin.addressof(&header))
@@ -122,6 +129,7 @@ extension ManagedBuffer {
122129
///
123130
/// - Note: This pointer is valid only for the duration of the
124131
/// call to `body`.
132+
@_preInverseGenerics
125133
@inlinable
126134
public final func withUnsafeMutablePointerToHeader<R>(
127135
_ body: (UnsafeMutablePointer<Header>) throws -> R
@@ -134,6 +142,7 @@ extension ManagedBuffer {
134142
///
135143
/// - Note: This pointer is valid only for the duration of the
136144
/// call to `body`.
145+
@_preInverseGenerics
137146
@inlinable
138147
public final func withUnsafeMutablePointerToElements<R>(
139148
_ body: (UnsafeMutablePointer<Element>) throws -> R
@@ -146,6 +155,7 @@ extension ManagedBuffer {
146155
///
147156
/// - Note: These pointers are valid only for the duration of the
148157
/// call to `body`.
158+
@_preInverseGenerics
149159
@inlinable
150160
public final func withUnsafeMutablePointers<R>(
151161
_ body: (UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>) throws -> R
@@ -191,8 +201,9 @@ extension ManagedBuffer {
191201
/// }
192202
///
193203
@frozen
194-
public struct ManagedBufferPointer<Header, Element> {
204+
public struct ManagedBufferPointer<Header, Element: ~Copyable> {
195205

206+
@_preInverseGenerics
196207
@usableFromInline
197208
internal var _nativeBuffer: Builtin.NativeObject
198209

@@ -211,6 +222,7 @@ public struct ManagedBufferPointer<Header, Element> {
211222
/// `bufferClass` is a non-`@objc` class with no declared stored
212223
/// properties. The `deinit` of `bufferClass` must destroy its
213224
/// stored `Header` and any constructed `Element`s.
225+
@_preInverseGenerics
214226
@inlinable
215227
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
216228
public init(
@@ -224,7 +236,7 @@ public struct ManagedBufferPointer<Header, Element> {
224236

225237
// initialize the header field
226238
try withUnsafeMutablePointerToHeader {
227-
$0.initialize(to:
239+
$0.initialize(to:
228240
try factory(
229241
self.buffer,
230242
{
@@ -240,6 +252,7 @@ public struct ManagedBufferPointer<Header, Element> {
240252
///
241253
/// - Precondition: `buffer` is an instance of a non-`@objc` class whose
242254
/// `deinit` destroys its stored `Header` and any constructed `Element`s.
255+
@_preInverseGenerics
243256
@inlinable
244257
public init(unsafeBufferObject buffer: AnyObject) {
245258
ManagedBufferPointer._checkValidBufferClass(type(of: buffer))
@@ -257,6 +270,7 @@ public struct ManagedBufferPointer<Header, Element> {
257270
/// _debugPreconditions in _checkValidBufferClass for any array. Since we know
258271
/// for the _ContiguousArrayBuffer that this check must always succeed we omit
259272
/// it in this specialized constructor.
273+
@_preInverseGenerics
260274
@inlinable
261275
internal init(_uncheckedUnsafeBufferObject buffer: AnyObject) {
262276
ManagedBufferPointer._internalInvariantValidBufferClass(type(of: buffer))
@@ -274,6 +288,7 @@ public struct ManagedBufferPointer<Header, Element> {
274288
/// `bufferClass` is a non-`@objc` class with no declared stored
275289
/// properties. The `deinit` of `bufferClass` must destroy its
276290
/// stored `Header` and any constructed `Element`s.
291+
@_preInverseGenerics
277292
@inlinable
278293
internal init(
279294
bufferClass: AnyClass,
@@ -290,12 +305,14 @@ public struct ManagedBufferPointer<Header, Element> {
290305

291306
/// Internal version for use by _ContiguousArrayBuffer.init where we know that
292307
/// we have a valid buffer class and that the capacity is >= 0.
308+
@_preInverseGenerics
293309
@inlinable
294310
internal init(
295311
_uncheckedBufferClass: AnyClass,
296312
minimumCapacity: Int
297313
) {
298-
ManagedBufferPointer._internalInvariantValidBufferClass(_uncheckedBufferClass, creating: true)
314+
ManagedBufferPointer._internalInvariantValidBufferClass(
315+
_uncheckedBufferClass, creating: true)
299316
_internalInvariant(
300317
minimumCapacity >= 0,
301318
"ManagedBufferPointer must have non-negative capacity")
@@ -315,14 +332,16 @@ public struct ManagedBufferPointer<Header, Element> {
315332
///
316333
/// - Note: It is an error to use the `header` property of the resulting
317334
/// instance unless it has been initialized.
335+
@_preInverseGenerics
318336
@inlinable
319337
internal init(_ buffer: ManagedBuffer<Header, Element>) {
320338
_nativeBuffer = Builtin.unsafeCastToNativeObject(buffer)
321339
}
322340
}
323341

324-
extension ManagedBufferPointer {
342+
extension ManagedBufferPointer where Element: ~Copyable {
325343
/// The stored `Header` instance.
344+
@_preInverseGenerics
326345
@inlinable
327346
public var header: Header {
328347
_read {
@@ -334,6 +353,7 @@ extension ManagedBufferPointer {
334353
}
335354

336355
/// Returns the object instance being used for storage.
356+
@_preInverseGenerics
337357
@inlinable
338358
public var buffer: AnyObject {
339359
return Builtin.castFromNativeObject(_nativeBuffer)
@@ -344,6 +364,7 @@ extension ManagedBufferPointer {
344364
/// This value may be nontrivial to compute; it is usually a good
345365
/// idea to store this information in the "header" area when
346366
/// an instance is created.
367+
@_preInverseGenerics
347368
@inlinable
348369
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
349370
public var capacity: Int {
@@ -357,6 +378,7 @@ extension ManagedBufferPointer {
357378
///
358379
/// - Note: This pointer is valid only
359380
/// for the duration of the call to `body`.
381+
@_preInverseGenerics
360382
@inlinable
361383
public func withUnsafeMutablePointerToHeader<R>(
362384
_ body: (UnsafeMutablePointer<Header>) throws -> R
@@ -369,6 +391,7 @@ extension ManagedBufferPointer {
369391
///
370392
/// - Note: This pointer is valid only for the duration of the
371393
/// call to `body`.
394+
@_preInverseGenerics
372395
@inlinable
373396
public func withUnsafeMutablePointerToElements<R>(
374397
_ body: (UnsafeMutablePointer<Element>) throws -> R
@@ -381,9 +404,12 @@ extension ManagedBufferPointer {
381404
///
382405
/// - Note: These pointers are valid only for the duration of the
383406
/// call to `body`.
407+
@_preInverseGenerics
384408
@inlinable
385409
public func withUnsafeMutablePointers<R>(
386-
_ body: (UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>) throws -> R
410+
_ body: (
411+
UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>
412+
) throws -> R
387413
) rethrows -> R {
388414
defer { _fixLifetime(_nativeBuffer) }
389415
return try body(_headerPointer, _elementPointer)
@@ -393,13 +419,15 @@ extension ManagedBufferPointer {
393419
/// buffer; otherwise, returns `false`.
394420
///
395421
/// See `isKnownUniquelyReferenced` for details.
422+
@_preInverseGenerics
396423
@inlinable
397424
public mutating func isUniqueReference() -> Bool {
398425
return _isUnique(&_nativeBuffer)
399426
}
400427
}
401428

402-
extension ManagedBufferPointer {
429+
extension ManagedBufferPointer where Element: ~Copyable {
430+
@_preInverseGenerics
403431
@inlinable
404432
internal static func _checkValidBufferClass(
405433
_ bufferClass: AnyClass, creating: Bool = false
@@ -418,6 +446,7 @@ extension ManagedBufferPointer {
418446
)
419447
}
420448

449+
@_preInverseGenerics
421450
@inlinable
422451
internal static func _internalInvariantValidBufferClass(
423452
_ bufferClass: AnyClass, creating: Bool = false
@@ -437,8 +466,9 @@ extension ManagedBufferPointer {
437466
}
438467
}
439468

440-
extension ManagedBufferPointer {
469+
extension ManagedBufferPointer where Element: ~Copyable {
441470
/// The required alignment for allocations of this type, minus 1
471+
@_preInverseGenerics
442472
@inlinable
443473
internal static var _alignmentMask: Int {
444474
return max(
@@ -447,19 +477,22 @@ extension ManagedBufferPointer {
447477
}
448478

449479
/// The actual number of bytes allocated for this object.
480+
@_preInverseGenerics
450481
@inlinable
451482
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
452483
internal var _capacityInBytes: Int {
453484
return _swift_stdlib_malloc_size(_address)
454485
}
455486

456487
/// The address of this instance in a convenient pointer-to-bytes form
488+
@_preInverseGenerics
457489
@inlinable
458490
internal var _address: UnsafeMutableRawPointer {
459491
return UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_nativeBuffer))
460492
}
461493

462494
/// Offset from the allocated storage for `self` to the stored `Header`
495+
@_preInverseGenerics
463496
@inlinable
464497
internal static var _headerOffset: Int {
465498
_onFastPath()
@@ -471,6 +504,7 @@ extension ManagedBufferPointer {
471504
/// An **unmanaged** pointer to the storage for the `Header`
472505
/// instance. Not safe to use without _fixLifetime calls to
473506
/// guarantee it doesn't dangle
507+
@_preInverseGenerics
474508
@inlinable
475509
internal var _headerPointer: UnsafeMutablePointer<Header> {
476510
_onFastPath()
@@ -481,6 +515,7 @@ extension ManagedBufferPointer {
481515
/// An **unmanaged** pointer to the storage for `Element`s. Not
482516
/// safe to use without _fixLifetime calls to guarantee it doesn't
483517
/// dangle.
518+
@_preInverseGenerics
484519
@inlinable
485520
internal var _elementPointer: UnsafeMutablePointer<Element> {
486521
_onFastPath()
@@ -489,6 +524,7 @@ extension ManagedBufferPointer {
489524
}
490525

491526
/// Offset from the allocated storage for `self` to the `Element` storage
527+
@_preInverseGenerics
492528
@inlinable
493529
internal static var _elementOffset: Int {
494530
_onFastPath()

0 commit comments

Comments
 (0)