-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] AutoreleasingUnsafeMutablePointer: eliminate questionable pointer use #26760
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
lorentey
merged 6 commits into
swiftlang:master
from
lorentey:♪-hold-me-use-me-autorelease-me-♬
Aug 27, 2019
The head ref may contain hidden characters: "\u266A-hold-me-use-me-autorelease-me-\u266C"
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a92bb9
[stdlib] AutoreleasingUnsafeMutablePointer: eliminate questionable po…
lorentey 61c5ac1
[stdlib] AutoreleasingUnsafeMutablePointer: update docs
lorentey b9e3ada
[stdlib] Make Unmanaged.takeUnretainedValue, .takeRetainedValue trans…
lorentey 88630e8
[stdlib] AutoreleasingUnsafeMutablePointer: use typed pointers rather…
lorentey b8dc0f4
[stdlib] AutoreleasingMutableUnsafePointer: switch to _unsafeReferenc…
lorentey d5ea371
[test] Add some high-level tests for AutoreleasingUnsafeMutablePointer
lorentey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// RUN: %target-run-simple-swift | ||
// REQUIRES: executable_test | ||
// REQUIRES: objc_interop | ||
|
||
import StdlibUnittest | ||
import ObjectiveC // for autoreleasepool | ||
|
||
var suite = TestSuite("AutoreleasingUnsafeMutablePointer") | ||
|
||
/// Call `body` passing it an AutoreleasingUnsafeMutablePointer whose pointee | ||
/// has the specified value, allocated in a temporary buffer. | ||
func withAUMP<Pointee: AnyObject>( | ||
as type: Pointee.Type = Pointee.self, | ||
initialValue: Optional<Unmanaged<Pointee>> = nil, | ||
_ body: (AutoreleasingUnsafeMutablePointer<Optional<Pointee>>) -> Void | ||
) { | ||
let p = | ||
UnsafeMutablePointer<Optional<Unmanaged<Pointee>>>.allocate(capacity: 1) | ||
p.initialize(to: initialValue) | ||
body(AutoreleasingUnsafeMutablePointer(p)) | ||
p.deallocate() | ||
} | ||
|
||
/// Call `body` passing it an AutoreleasingUnsafeMutablePointer whose pointee | ||
/// has the specified value, allocated in a temporary buffer. | ||
func withAUMP<Pointee: AnyObject>( | ||
initialValues: [Unmanaged<Pointee>?], | ||
_ body: (AutoreleasingUnsafeMutablePointer<Optional<Pointee>>) -> Void | ||
) { | ||
let p = UnsafeMutablePointer<Optional<Unmanaged<Pointee>>>.allocate( | ||
capacity: initialValues.count | ||
) | ||
for i in 0 ..< initialValues.count { | ||
(p + i).initialize(to: initialValues[i]) | ||
} | ||
|
||
let aump = AutoreleasingUnsafeMutablePointer<Optional<Pointee>>(p) | ||
body(aump) | ||
p.deallocate() | ||
} | ||
|
||
suite.test("helper calls its closure exactly once") { | ||
// `withAUMP` is expected to call its closure exactly once, with an argument | ||
// pointing to a nil value. | ||
var runCount = 0 | ||
withAUMP(as: LifetimeTracked.self) { p in | ||
runCount += 1 | ||
expectNil(p.pointee) | ||
} | ||
expectEqual(runCount, 1) | ||
} | ||
|
||
suite.test("init doesn't autorelease") { | ||
let originalInstances = LifetimeTracked.instances | ||
let unmanaged = Unmanaged.passRetained(LifetimeTracked(42)) | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
|
||
withAUMP(initialValue: unmanaged) { p in noop(p) } | ||
|
||
// Releasing the original reference should immediately deallocate the | ||
// object. | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
unmanaged.release() | ||
expectEqual(LifetimeTracked.instances, originalInstances) | ||
} | ||
|
||
suite.test("getter initially returns the initial value") { | ||
withAUMP(as: LifetimeTracked.self) { p in | ||
expectNil(p.pointee) | ||
} | ||
|
||
let unmanaged = Unmanaged.passRetained(LifetimeTracked(42)) | ||
withAUMP(initialValue: unmanaged) { p in | ||
expectTrue(p.pointee === unmanaged.takeUnretainedValue()) | ||
} | ||
unmanaged.release() | ||
} | ||
|
||
suite.test("pointee getter returns the last value set") { | ||
autoreleasepool { | ||
withAUMP(as: LifetimeTracked.self) { p in | ||
expectNil(p.pointee) | ||
let object = LifetimeTracked(23) | ||
p.pointee = object | ||
expectTrue(p.pointee === object) | ||
let other = LifetimeTracked(42) | ||
p.pointee = other | ||
expectTrue(p.pointee === other) | ||
p.pointee = nil | ||
expectNil(p.pointee) | ||
} | ||
} | ||
} | ||
|
||
suite.test("pointee getter doesn't autorelease") { | ||
let originalInstances = LifetimeTracked.instances | ||
autoreleasepool { | ||
let unmanaged = Unmanaged.passRetained(LifetimeTracked(42)) | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
|
||
withAUMP(initialValue: unmanaged) { p in | ||
expectTrue(p.pointee === unmanaged.takeUnretainedValue()) | ||
} | ||
// Releasing the original reference should immediately deallocate the | ||
// object. | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
unmanaged.release() | ||
expectEqual(LifetimeTracked.instances, originalInstances) | ||
} | ||
} | ||
|
||
suite.test("pointee setter autoreleases the new value") { | ||
let originalInstances = LifetimeTracked.instances | ||
autoreleasepool { | ||
withAUMP(as: LifetimeTracked.self) { p in | ||
let object = LifetimeTracked(42) | ||
// There is one more instance now. | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
p.pointee = object | ||
} | ||
// The strong reference to `object` is gone, but the autoreleasepool | ||
// is still holding onto it. | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
} | ||
// Draining the pool deallocates the instance. | ||
expectEqual(LifetimeTracked.instances, originalInstances) | ||
} | ||
|
||
suite.test("AutoreleasingUnsafeMutablePointer doesn't retain its value") { | ||
let originalInstances = LifetimeTracked.instances | ||
withAUMP(as: LifetimeTracked.self) { p in | ||
autoreleasepool { | ||
let object = LifetimeTracked(42) | ||
p.pointee = object | ||
expectEqual(LifetimeTracked.instances, originalInstances + 1) | ||
} | ||
// Draining the pool deallocates the instance, even though | ||
// the autoreleasing pointer still points to it. | ||
// This is okay as long as the pointer isn't dereferenced. | ||
expectEqual(LifetimeTracked.instances, originalInstances) | ||
} | ||
expectEqual(LifetimeTracked.instances, originalInstances) | ||
} | ||
|
||
suite.test("subscript[0] is the same as pointee.getter") { | ||
withAUMP(as: LifetimeTracked.self) { p in | ||
expectNil(p[0]) | ||
} | ||
|
||
let unmanaged = Unmanaged.passRetained(LifetimeTracked(42)) | ||
withAUMP(initialValue: unmanaged) { p in | ||
expectTrue(p[0] === unmanaged.takeUnretainedValue()) | ||
} | ||
unmanaged.release() | ||
} | ||
|
||
|
||
suite.test("subscript and advanced(by:) works") { | ||
let originalInstances = LifetimeTracked.instances | ||
let count = 100 | ||
|
||
var refs = 0 | ||
let values: [Unmanaged<LifetimeTracked>?] = (0 ..< count).map { i in | ||
if i % 10 == 0 { | ||
refs += 1 | ||
return Unmanaged.passRetained(LifetimeTracked(i)) | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
withAUMP(initialValues: values) { p in | ||
for i in 0 ..< count { | ||
expectTrue(p[i] === values[i]?.takeUnretainedValue()) | ||
expectTrue(p.advanced(by: i).pointee === values[i]?.takeUnretainedValue()) | ||
} | ||
} | ||
|
||
expectEqual(LifetimeTracked.instances, originalInstances + refs) | ||
for unmanaged in values { | ||
unmanaged?.release() | ||
} | ||
expectEqual(LifetimeTracked.instances, originalInstances) | ||
} | ||
|
||
runAllTests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense but is kinda terrifying. I need to think carefully about some of the NSDictionary bridging stuff…
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All +0 references in memory are hazards; they must only be handled through the
Unmanaged
glovebox. E.g., we'll need to audit any and all NSFastEnumeration implementations & usages from Swift code.(Currently Unmanaged doesn't quite get this right either, but Michael is already working on fixing that.)