kotlinx-io icon indicating copy to clipboard operation
kotlinx-io copied to clipboard

There should be a Native IoBuffer constructor public

Open SalomonBrys opened this issue 7 years ago • 2 comments
trafficstars

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)
}

SalomonBrys avatar Sep 04 '18 15:09 SalomonBrys

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.

cy6erGn0m avatar Sep 05 '18 16:09 cy6erGn0m

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.

SalomonBrys avatar Sep 05 '18 16:09 SalomonBrys

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.

fzhinkin avatar Jun 12 '23 11:06 fzhinkin