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-11300] Provide a RawPointer version of ManagedBuffer #53701

Open
Lukasa opened this issue Aug 13, 2019 · 3 comments
Open

[SR-11300] Provide a RawPointer version of ManagedBuffer #53701

Lukasa opened this issue Aug 13, 2019 · 3 comments
Labels
improvement standard library Area: Standard library umbrella

Comments

@Lukasa
Copy link
Contributor

Lukasa commented Aug 13, 2019

Previous ID SR-11300
Radar rdar://problem/54262180
Original Reporter @Lukasa
Type Improvement
Additional Detail from JIRA
Votes 2
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: f9e37d1629f65a324f493c1dffb093ff

Issue Description:

ManagedBuffer is useful for tail-allocating arrays of data, avoiding an extra level of indirection and saving allocations in the process.

However, some C APIs use "variable length" data structures, usually in the form of a zero-length array declared at the back of the structure, and ask their users to malloc appropriately-sized buffers for them. This doesn't import well into Swift, requiring us to use the "class holding an UnsafePointer" method of CoW-boxing, incurring an extra indirection in order to use the data behind the pointer.

We could resolve this if we had a variation of ManagedBuffer that gives the user an UnsafeMutableRawPointer to the tail storage, instead of a typed pointer. That would allow the user to bind and initialise the memory as they see fit, supporting variable length data structures.

@Lukasa
Copy link
Contributor Author

Lukasa commented Aug 13, 2019

@swift-ci create

@belkadan
Copy link
Contributor

cc @atrick

@atrick
Copy link
Member

atrick commented Aug 14, 2019

Thanks for filing this. It isn't the first time I've seen someone want this.

In the meantime, I think you can create a typed `ManagedBuffer<Uint8>` and downcast from `UnsafePointer<UInt8>` to `UnsafeRawPointer` whenever you use it, possibly with a wrapper type.

I also notice that `ManagedBuffer` vends a standalone pointer instead of a buffer pointer which is not best practice.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants