Skip to content

Commit bcd7d3f

Browse files
committed
[embedded] Implement retain_n and release_n in the embedded runtime
1 parent 1470023 commit bcd7d3f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,31 @@ public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePoint
101101

102102
@_silgen_name("swift_retain")
103103
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 {
104109
if Int(Builtin.ptrtoint_Word(object)) == 0 { return object }
105110
let o = UnsafeMutablePointer<HeapObject>(object)
106111
// TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
107112
if o.pointee.refcount == HeapObject.immortalRefCount { return o._rawValue }
108-
o.pointee.refcount += 1
113+
o.pointee.refcount += Int(n)
109114
return o._rawValue
110115
}
111116

112117
@_silgen_name("swift_release")
113118
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) {
114124
if Int(Builtin.ptrtoint_Word(object)) == 0 { return }
115125
let o = UnsafeMutablePointer<HeapObject>(object)
116126
// TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments.
117127
if o.pointee.refcount == HeapObject.immortalRefCount { return }
118-
o.pointee.refcount -= 1
128+
o.pointee.refcount -= Int(n)
119129
if (o.pointee.refcount & HeapObject.refcountMask) == 0 {
120130
_swift_embedded_invoke_heap_object_destroy(o)
121131
}

0 commit comments

Comments
 (0)