kotlinx-io
kotlinx-io copied to clipboard
There should be a Native IoBuffer constructor public
What if we want an IoBuffer to use as memory backend a specific CPointer ?
There are a lot of use cases for this:
- Manual allocation of a specific known size
- Read from a pointer received from a C/C++ library
- etc.
I suggest the following addtion to the Native IoBuffer class:
constructor(content: CPointer<ByteVar>, contentCapacity: Int) : this(content, contentCapacity, null)
Of course, the memory management of the CPointer's memory is the programmer's repsonsability.
This could be emphasized by using a companion object method instead of a constructor:
companion object {
fun makeView(content: CPointer<ByteVar>, contentCapacity: Int) = IoBuffer(content, contentCapacity, null)
}
First of all, one shouldn't allocate IoBuffer directly. A packet should be used instead in most cases. It's not clear for me whether it should be ByteReadPacket.viewOf(pointer, size) or IoBuffer.viewOf(pointer, size). For now I am thinking of the first option. The only case for the second is very specific performance critical corners.
CPointers were disabled due to iOS related issues with K/N compiler but should be enabled back soon.
I disagree, IoBuffer is very usefull to write.
While ByteReadPacket.viewOf(pointer, size) can be used to read, it could not be used to write on the memory of the Cpointer, which is why I think IoBuffer.viewOf(pointer, size) is very needed.
We're rebooting the kotlinx-io development (see https://github.com/Kotlin/kotlinx-io/issues/131), all issues related to the previous versions will be closed. Consider reopening it if the issue remains (or the feature is still missing) in a new version.