2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
@@ -608,11 +608,10 @@ extension _ArrayBuffer {
608
608
1 //OBJC_ASSOCIATION_RETAIN_NONATOMIC
609
609
)
610
610
}
611
-
612
- @_alwaysEmitIntoClient @inline ( never)
613
- internal func withUnsafeBufferPointer_nonNative< R, E> (
614
- _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> R
615
- ) throws ( E) -> R {
611
+
612
+ @_alwaysEmitIntoClient
613
+ internal func getOrAllocateAssociatedObjectBuffer(
614
+ ) -> _ContiguousArrayBuffer < Element > {
616
615
let unwrapped : _ContiguousArrayBuffer < Element >
617
616
// libobjc already provides the necessary memory barriers for
618
617
// double checked locking to be safe, per comments on
@@ -633,12 +632,16 @@ extension _ArrayBuffer {
633
632
defer { _fixLifetime ( unwrapped) }
634
633
objc_sync_exit ( lock)
635
634
}
636
- return try unsafe body(
637
- UnsafeBufferPointer (
638
- start: unwrapped. firstElementAddress,
639
- count: unwrapped. count
640
- )
641
- )
635
+ return unwrapped
636
+ }
637
+
638
+ @_alwaysEmitIntoClient @inline ( never)
639
+ internal func withUnsafeBufferPointer_nonNative< R, E> (
640
+ _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> R
641
+ ) throws ( E) -> R {
642
+ let buffer = getOrAllocateAssociatedObjectBuffer ( )
643
+ let ( pointer, count) = unsafe ( buffer. firstElementAddress, buffer. count)
644
+ return try unsafe body( UnsafeBufferPointer ( start: pointer, count: count) )
642
645
}
643
646
644
647
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
0 commit comments