22//
33// This source file is part of the Swift.org open source project
44//
5- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+ // Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66// Licensed under Apache License v2.0 with Runtime Library Exception
77//
88// See https://swift.org/LICENSE.txt for license information
@@ -608,11 +608,10 @@ extension _ArrayBuffer {
608608 1 //OBJC_ASSOCIATION_RETAIN_NONATOMIC
609609 )
610610 }
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 > {
616615 let unwrapped : _ContiguousArrayBuffer < Element >
617616 // libobjc already provides the necessary memory barriers for
618617 // double checked locking to be safe, per comments on
@@ -633,12 +632,16 @@ extension _ArrayBuffer {
633632 defer { _fixLifetime ( unwrapped) }
634633 objc_sync_exit ( lock)
635634 }
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) )
642645 }
643646
644647 /// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
0 commit comments