@@ -23,7 +23,12 @@ public struct ClassMetadata {
23
23
24
24
public struct HeapObject {
25
25
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.
26
29
var refcount : Int
30
+
31
+ static let immortalRefCount = - 1
27
32
}
28
33
29
34
func alignedAlloc( size: Int , alignment: Int ) -> UnsafeMutableRawPointer ? {
@@ -66,7 +71,9 @@ public func swift_deallocClassInstance(object: UnsafeMutablePointer<HeapObject>,
66
71
@_silgen_name ( " swift_initStackObject " )
67
72
public func swift_initStackObject( metadata: UnsafeMutablePointer < ClassMetadata > , object: UnsafeMutablePointer < HeapObject > ) -> UnsafeMutablePointer < HeapObject > {
68
73
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
70
77
return object
71
78
}
72
79
@@ -83,7 +90,7 @@ public func swift_retain(object: Builtin.RawPointer) -> Builtin.RawPointer {
83
90
if Int ( Builtin . ptrtoint_Word ( object) ) == 0 { return object }
84
91
let o = UnsafeMutablePointer < HeapObject > ( object)
85
92
// 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 }
87
94
o. pointee. refcount += 1
88
95
return o. _rawValue
89
96
}
@@ -93,7 +100,7 @@ public func swift_release(object: Builtin.RawPointer) {
93
100
if Int ( Builtin . ptrtoint_Word ( object) ) == 0 { return }
94
101
let o = UnsafeMutablePointer < HeapObject > ( object)
95
102
// 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 }
97
104
o. pointee. refcount -= 1
98
105
if o. pointee. refcount == 0 {
99
106
_swift_runtime_invoke_heap_object_destroy ( o. pointee. metadata. pointee. destroy, o)
@@ -102,10 +109,12 @@ public func swift_release(object: Builtin.RawPointer) {
102
109
103
110
@_silgen_name ( " swift_beginAccess " )
104
111
public func swift_beginAccess( pointer: UnsafeMutableRawPointer , buffer: UnsafeMutableRawPointer , flags: UInt , pc: UnsafeMutableRawPointer ) {
112
+ // TODO: Add actual exclusivity checking.
105
113
}
106
114
107
115
@_silgen_name ( " swift_endAccess " )
108
116
public func swift_endAccess( buffer: UnsafeMutableRawPointer ) {
117
+ // TODO: Add actual exclusivity checking.
109
118
}
110
119
111
120
@_silgen_name ( " swift_once " )
0 commit comments