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

Make it easier to write into ByteBuffer via writable pointers. #538

Closed
Lukasa opened this issue Jul 31, 2018 · 2 comments
Closed

Make it easier to write into ByteBuffer via writable pointers. #538

Lukasa opened this issue Jul 31, 2018 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Lukasa
Copy link
Contributor

Lukasa commented Jul 31, 2018

The Problem

Plenty of C APIs expose APIs where they write into a buffer provided by the caller. For example, consider OpenSSL's SSL_read, declared like this:

int SSL_read(SSL *ssl, void *buf, int num);

In this model, OpenSSL will write into the pointer at buf. To support this with ByteBuffer, the following code has to be written:

let rc = outputBuffer.writeWithUnsafeMutableBytes { pointer in
    SSL_read(self.ssl, pointer.baseAddress, CInt(pointer.count))
}

The issue here is that this call will give you a pointer to only the writable bytes that actually exist in your ByteBuffer. If there are only 2 bytes, or even 0 bytes, that's all you're going to get. That can make using this API extremely tricky, and requires calls to the (very flawed, see #537) function changeCapacity before using it.

It'd be nice to have a function like ByteBuffer.writeWithUnsafeMutableBytes(minimumWritableSpace: Int, _ block: (UnsafeMutableRawBufferPointer) -> Int). This function would guarantee that the minimum size of the pointer in the block would be appropriately set, performing any CoW or other operation as needed.

This is not a trivial patch, but if you're interested in the internals of ByteBuffer this would be a good thing to build.

@Lukasa Lukasa added enhancement New feature or request help wanted Extra attention is needed labels Jul 31, 2018
@weissi weissi added this to the 2.0.0 milestone Nov 27, 2018
@weissi weissi removed this from the 2.0.0 milestone Feb 25, 2019
@2bjake
Copy link
Contributor

2bjake commented Oct 23, 2019

This is a dup of #1105 right? If so, #1175 addressed this.

@Lukasa
Copy link
Contributor Author

Lukasa commented Oct 23, 2019

Sure is.

@Lukasa Lukasa closed this as completed Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants