Skip to content

[5.5] Make constants immortal #3064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CoreFoundation/Base.subproj/CFBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,11 @@ CFTypeRef CFMakeCollectable(CFTypeRef cf) CF_AUTOMATED_REFCOUNT_UNAVAILABLE;

#if DEPLOYMENT_RUNTIME_SWIFT

#define _CF_SWIFT_RC_PINNED_FLAG (0x1)
#if TARGET_RT_64_BIT
#define _CF_SWIFT_RC_PINNED_FLAG (0x80000004ffffffff)
#else
#define _CF_SWIFT_RC_PINNED_FLAG (0x800004FF)
#endif
#define _CF_CONSTANT_OBJECT_STRONG_RC ((uintptr_t)_CF_SWIFT_RC_PINNED_FLAG)
#endif

Expand Down
29 changes: 29 additions & 0 deletions Tests/Foundation/Tests/TestBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestBridging : XCTestCase {
return [
("testBridgedDescription", testBridgedDescription),
("testDynamicCast", testDynamicCast),
("testConstantsImmortal", testConstantsImmortal),
]
}

Expand Down Expand Up @@ -70,4 +71,32 @@ class TestBridging : XCTestCase {
let anyArray: Any = [TestClass()]
XCTAssertNotNil(anyArray as? NSObject)
}

func testConstantsImmortal() throws {
func release(_ ptr: UnsafeRawPointer, count: Int) {
let object: Unmanaged<NSNumber> = Unmanaged.fromOpaque(ptr)
for _ in 0..<count {
object.release()
}
}

let trueConstant = NSNumber(value: true)
let falseConstant = NSNumber(value: false)

// To accurately read the whole refcount, we need to read the second
// word of the pointer.
let truePtr = unsafeBitCast(trueConstant, to: UnsafePointer<Int>.self)
let falsePtr = unsafeBitCast(falseConstant, to: UnsafePointer<Int>.self)

let trueRefCount = truePtr.advanced(by: 1).pointee
let falseRefCount = falsePtr.advanced(by: 1).pointee

XCTAssertEqual(trueRefCount, falseRefCount)

release(truePtr, count: 5)
release(falsePtr, count: 5)

XCTAssertEqual(trueRefCount, truePtr.advanced(by: 1).pointee)
XCTAssertEqual(falseRefCount, falsePtr.advanced(by: 1).pointee)
}
}