@@ -393,24 +393,27 @@ final class SyntaxDataArena: @unchecked Sendable {
393
393
// The storage of the buffer address is allocated next to the SyntaxData.
394
394
let baseAddressRef = parent. advanced ( by: 1 )
395
395
. unsafeRawPointer
396
- . assumingMemoryBound ( to: UnsafePointer < SyntaxDataReference ?>? . self )
396
+ . assumingMemoryBound ( to: AtomicPointer . self)
397
397
398
398
// If the buffer is already populated, return it.
399
- if let baseAddress = baseAddressRef. pointee {
399
+ if let baseAddress = swiftsyntax_atomic_pointer_get ( baseAddressRef) ? . assumingMemoryBound ( to : SyntaxDataReference ? . self ) {
400
400
return SyntaxDataReferenceBuffer ( UnsafeBufferPointer ( start: baseAddress, count: childCount) )
401
401
}
402
402
403
403
mutex. lock ( )
404
404
defer { mutex. unlock ( ) }
405
405
406
406
// Recheck, maybe some other thread has populated the buffer during acquiring the lock.
407
- if let baseAddress = baseAddressRef. pointee {
407
+ if let baseAddress = swiftsyntax_atomic_pointer_get ( baseAddressRef) ? . assumingMemoryBound ( to : SyntaxDataReference ? . self ) {
408
408
return SyntaxDataReferenceBuffer ( UnsafeBufferPointer ( start: baseAddress, count: childCount) )
409
409
}
410
410
411
411
let buffer = createLayoutDataImpl ( parent)
412
412
// Remeber the base address of the created buffer.
413
- UnsafeMutablePointer ( mutating: baseAddressRef) . pointee = buffer. baseAddress
413
+ swiftsyntax_atomic_pointer_set (
414
+ UnsafeMutablePointer ( mutating: baseAddressRef) ,
415
+ UnsafeRawPointer ( buffer. baseAddress)
416
+ )
414
417
415
418
return SyntaxDataReferenceBuffer ( buffer)
416
419
}
@@ -460,7 +463,7 @@ final class SyntaxDataArena: @unchecked Sendable {
460
463
return min ( totalSize, 4096 )
461
464
}
462
465
463
- /// Allocate and initialize `SyntaxData` with the trailing uninitialized buffer for the references to the children.
466
+ /// Allocate and initialize `SyntaxData` with the trailing pointer storage for the references to the children.
464
467
private static func createDataImpl(
465
468
allocator: BumpPtrAllocator ,
466
469
raw: RawSyntax ,
@@ -474,7 +477,7 @@ final class SyntaxDataArena: @unchecked Sendable {
474
477
var totalSize = MemoryLayout< SyntaxData> . stride
475
478
if childCount != 0 {
476
479
// Tail allocate the storage for the pointer to the lazily allocated layout data.
477
- totalSize &+= MemoryLayout< UnsafePointer < SyntaxDataReference ?>? >. size
480
+ totalSize &+= MemoryLayout< AtomicPointer > . size
478
481
}
479
482
let alignment = MemoryLayout< SyntaxData> . alignment
480
483
let allocated = allocator. allocate ( byteCount: totalSize, alignment: alignment) . baseAddress!
@@ -492,10 +495,10 @@ final class SyntaxDataArena: @unchecked Sendable {
492
495
493
496
if childCount != 0 {
494
497
// Initlaize the tail allocated storage with 'nil'.
495
- allocated
498
+ let layoutBufferBaseAddressRef = allocated
496
499
. advanced ( by: MemoryLayout< SyntaxData> . stride)
497
- . bindMemory ( to: UnsafePointer < SyntaxDataReference ?>? . self , capacity: 1 )
498
- . initialize ( to : nil )
500
+ . bindMemory ( to: AtomicPointer . self, capacity: 1 )
501
+ swiftsyntax_atomic_pointer_set ( layoutBufferBaseAddressRef , nil )
499
502
}
500
503
501
504
return SyntaxDataReference ( UnsafePointer ( dataRef) )
0 commit comments