Skip to content

Commit e1b9488

Browse files
committed
runtime: add a new runtime function swift_setDeallocating.
It's to be used by code produced by the ReleaseDevirtualizer. As the function is only used for non-escaping objects, the deallocating bit is set non-atomically.
1 parent b25019c commit e1b9488

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

docs/Runtime.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Returns a random number. Only used by allocation profiling tools.
160160
000000000002b290 T _swift_isUniquelyReferencedOrPinned_nonNull_native
161161
000000000002af00 T _swift_isUniquelyReferenced_native
162162
000000000002aea0 T _swift_isUniquelyReferenced_nonNull_native
163+
00000000000????? T _swift_setDeallocating
163164
000000000001d280 T _swift_isDeallocating
164165
```
165166

include/swift/Runtime/HeapObject.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ SWIFT_RUNTIME_EXPORT
292292
extern "C" void (*SWIFT_CC(RegisterPreservingCC)
293293
_swift_release_n)(HeapObject *object, uint32_t n);
294294

295+
/// Sets the RC_DEALLOCATING_FLAG flag. This is done non-atomically.
296+
/// The strong reference count of \p object must be 1 and no other thread may
297+
/// retain the object during executing this function.
298+
SWIFT_RUNTIME_EXPORT
299+
extern "C" void swift_setDeallocating(HeapObject *object);
300+
295301
// Refcounting observation hooks for memory tools. Don't use these.
296302
SWIFT_RUNTIME_EXPORT
297303
extern "C" size_t swift_retainCount(HeapObject *object);

stdlib/public/SwiftShims/RefCount.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ class StrongRefCount {
158158
return doDecrementShouldDeallocateN<false>(n);
159159
}
160160

161+
// Set the RC_DEALLOCATING_FLAG flag non-atomically.
162+
//
163+
// Precondition: the reference count must be 1
164+
void decrementFromOneAndDeallocateNonAtomic() {
165+
assert(refCount == RC_ONE && "Expect a count of 1");
166+
refCount = RC_DEALLOCATING_FLAG;
167+
}
168+
161169
// Return the reference count.
162170
// During deallocation the reference count is undefined.
163171
uint32_t getCount() const {

stdlib/public/runtime/HeapObject.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ void SWIFT_RT_ENTRY_IMPL(swift_release_n)(HeapObject *object, uint32_t n)
344344
}
345345
}
346346

347+
void swift::swift_setDeallocating(HeapObject *object) {
348+
object->refCount.decrementFromOneAndDeallocateNonAtomic();
349+
}
350+
347351
size_t swift::swift_retainCount(HeapObject *object) {
348352
return object->refCount.getCount();
349353
}

0 commit comments

Comments
 (0)