@@ -101,23 +101,35 @@ public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePoint
101
101
102
102
@_silgen_name ( " swift_retain " )
103
103
public func swift_retain( object: Builtin . RawPointer ) -> Builtin . RawPointer {
104
+ return swift_retain_n ( object: object, n: 1 )
105
+ }
106
+
107
+ @_silgen_name ( " swift_retain_n " )
108
+ public func swift_retain_n( object: Builtin . RawPointer , n: UInt32 ) -> Builtin . RawPointer {
104
109
if Int ( Builtin . ptrtoint_Word ( object) ) == 0 { return object }
105
110
let o = UnsafeMutablePointer < HeapObject > ( object)
106
111
// TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
107
112
if o. pointee. refcount == HeapObject . immortalRefCount { return o. _rawValue }
108
- o. pointee. refcount += 1
113
+ o. pointee. refcount += Int ( n )
109
114
return o. _rawValue
110
115
}
111
116
112
117
@_silgen_name ( " swift_release " )
113
118
public func swift_release( object: Builtin . RawPointer ) {
119
+ swift_release_n ( object: object, n: 1 )
120
+ }
121
+
122
+ @_silgen_name ( " swift_release_n " )
123
+ public func swift_release_n( object: Builtin . RawPointer , n: UInt32 ) {
114
124
if Int ( Builtin . ptrtoint_Word ( object) ) == 0 { return }
115
125
let o = UnsafeMutablePointer < HeapObject > ( object)
116
126
// TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
117
127
if o. pointee. refcount == HeapObject . immortalRefCount { return }
118
- o. pointee. refcount -= 1
128
+ o. pointee. refcount -= Int ( n )
119
129
if ( o. pointee. refcount & HeapObject . refcountMask) == 0 {
120
130
_swift_embedded_invoke_heap_object_destroy ( o)
131
+ } else if ( o. pointee. refcount & HeapObject . refcountMask) < 0 {
132
+ fatalError ( " negative refcount " )
121
133
}
122
134
}
123
135
0 commit comments