Skip to content
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

[SR-11082] Support withMemoryRebound(to:) with mismatched sizes #53474

Closed
atrick opened this issue Jul 8, 2019 · 3 comments
Closed

[SR-11082] Support withMemoryRebound(to:) with mismatched sizes #53474

atrick opened this issue Jul 8, 2019 · 3 comments
Labels
feature A feature request or implementation manual memory management Area → standard library: Manual memory management APIs standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

Comments

@atrick
Copy link
Member

atrick commented Jul 8, 2019

Previous ID SR-11082
Radar None
Original Reporter @atrick
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature
Assignee None
Priority Medium

md5: 74cbbf4bb5d2a9863759a9e9d0cb9ec6

Issue Description:

We should fix both UnsafePointer and UnsafeBufferPointer to handle converting to a type with a different stride.

For UnsafePointer, document that the capacity argument pertains to the number of elements of the to type (not the from type).

For UnsafeBufferPointer, the capacity is derived from the buffer capacity.

This can be done by adding checks to the stdlib implementation:

First check if the to/from strides are equal. If so, no additional code should be executed as a result of this change.

Next find the stride ratio. Either the from or to type may be larger than the other, but the larger stride must be an exact multiple of the smaller.

For UnsafeBufferPointer, call the first bindMemory with the adjusted capacity. For UnsafePointer the second bindMemory call's capacity changes.

If the to type is larger, add an assertion that the buffer is aligned to to.align.

This merits a discussion on the swift-evolution list. But since it's only broadening the accepted input, I don't think it needs an SE proposal. The documentation does need to change.

This has been discussed before. I balked at supporting this right away because I wanted a zero-abstraction cost guarantee for all new memory binding APIs (relative to simple pointer conversion). But, given the code that I've seen since then, I think it's worth doing and just hoping compile always specializes the call.

@atrick
Copy link
Member Author

atrick commented Jul 8, 2019

I discussed this with ravikandhadai (JIRA User).

@atrick
Copy link
Member Author

atrick commented Apr 7, 2020

Similarly, we can add an

assumingMemoryBound(to:)

to

UnsafeRawBufferPointer

that computes the correct number of elements.

@glessard
Copy link
Contributor

glessard commented Feb 4, 2022

Addressed by SE-0333 and #39529

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@atrick atrick closed this as completed Apr 26, 2022
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented manual memory management Area → standard library: Manual memory management APIs and removed new feature labels Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation manual memory management Area → standard library: Manual memory management APIs standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented
Projects
None yet
Development

No branches or pull requests

3 participants