You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
Additional Detail from JIRA
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.
The text was updated successfully, but these errors were encountered: