@@ -23,7 +23,12 @@ public struct ClassMetadata {
2323
2424public struct HeapObject {
2525 var metadata : UnsafeMutablePointer < ClassMetadata >
26+
27+ // TODO: This is just an initial support for strong refcounting only. We need
28+ // to think about supporting (or banning) weak and/or unowned references.
2629 var refcount : Int
30+
31+ static let immortalRefCount = - 1
2732}
2833
2934func alignedAlloc( size: Int , alignment: Int ) -> UnsafeMutableRawPointer ? {
@@ -66,7 +71,9 @@ public func swift_deallocClassInstance(object: UnsafeMutablePointer<HeapObject>,
6671@_silgen_name ( " swift_initStackObject " )
6772public func swift_initStackObject( metadata: UnsafeMutablePointer < ClassMetadata > , object: UnsafeMutablePointer < HeapObject > ) -> UnsafeMutablePointer < HeapObject > {
6873 object. pointee. metadata = metadata
69- object. pointee. refcount = - 1
74+
75+ // TODO/FIXME: Making all stack promoted objects immortal is not correct.
76+ object. pointee. refcount = HeapObject . immortalRefCount
7077 return object
7178}
7279
@@ -83,7 +90,7 @@ public func swift_retain(object: Builtin.RawPointer) -> Builtin.RawPointer {
8390 if Int ( Builtin . ptrtoint_Word ( object) ) == 0 { return object }
8491 let o = UnsafeMutablePointer < HeapObject > ( object)
8592 // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
86- if o. pointee. refcount == - 1 { return o. _rawValue }
93+ if o. pointee. refcount == HeapObject . immortalRefCount { return o. _rawValue }
8794 o. pointee. refcount += 1
8895 return o. _rawValue
8996}
@@ -93,7 +100,7 @@ public func swift_release(object: Builtin.RawPointer) {
93100 if Int ( Builtin . ptrtoint_Word ( object) ) == 0 { return }
94101 let o = UnsafeMutablePointer < HeapObject > ( object)
95102 // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
96- if o. pointee. refcount == - 1 { return }
103+ if o. pointee. refcount == HeapObject . immortalRefCount { return }
97104 o. pointee. refcount -= 1
98105 if o. pointee. refcount == 0 {
99106 _swift_runtime_invoke_heap_object_destroy ( o. pointee. metadata. pointee. destroy, o)
@@ -102,10 +109,12 @@ public func swift_release(object: Builtin.RawPointer) {
102109
103110@_silgen_name ( " swift_beginAccess " )
104111public func swift_beginAccess( pointer: UnsafeMutableRawPointer , buffer: UnsafeMutableRawPointer , flags: UInt , pc: UnsafeMutableRawPointer ) {
112+ // TODO: Add actual exclusivity checking.
105113}
106114
107115@_silgen_name ( " swift_endAccess " )
108116public func swift_endAccess( buffer: UnsafeMutableRawPointer ) {
117+ // TODO: Add actual exclusivity checking.
109118}
110119
111120@_silgen_name ( " swift_once " )
0 commit comments