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