napperley
05/28/2021, 4:39 AMpublic actual fun saveToBuffer(
type: String,
options: Map<String, String>,
bufferSize: Int,
error: Error?
): Pair<ByteArray, Boolean> = memScoped {
val tmpBuffer = allocArray<CPointerVar<ByteVar>>(bufferSize)
val arraySize = options.keys.size
val keys = allocArray<CPointerVar<ByteVar>>(arraySize)
val values = allocArray<CPointerVar<ByteVar>>(arraySize)
options.keys.forEachIndexed { pos, item -> keys[pos]?.pointed?.value = item.toByte() }
options.values.forEachIndexed { pos, item -> values[pos]?.pointed?.value = item.toByte() }
val errorSet = gdk_pixbuf_save_to_bufferv(
pixbuf = gdkPixbufPtr,
type = type,
error = cValuesOf(error?.gErrorPtr),
option_keys = keys,
option_values = values,
buffer_size = null,
buffer = tmpBuffer
) == TRUE
val buffer = ByteArray(bufferSize)
@Suppress("ReplaceRangeToWithUntil")
(0..(bufferSize - 1)).forEach { pos ->
val item = tmpBuffer[pos]
if (item != null) buffer[pos] = item.pointed.value
}
buffer to errorSet
}
1. Are manually allocated C arrays in Kotlin Native null terminated?
2. Is buffer a shallow copy of tmpBuffer?
3. Is there an easier way to do a shallow copy of a byte buffer that has been manually allocated to a new copy of ByteArray?napperley
05/28/2021, 4:41 AMmsink
05/28/2021, 9:00 AMval buffer = ByteArray(bufferSize)
buffer.usePinned { pinned ->
...
buffer = pinned.addressOf(0)
)
...
}
Dominaezzz
05/28/2021, 11:42 AMDominaezzz
05/28/2021, 11:51 AMtoByte()
and your code with never execute it anyway because item is always null.Dominaezzz
05/28/2021, 11:52 AMmsink
05/28/2021, 11:52 AMval keys = allocArray<CPointerVar<ByteVar>>(arraySize) { options.keys[it] }
Dominaezzz
05/28/2021, 12:00 PMDominaezzz
05/28/2021, 12:01 PMnapperley
06/04/2021, 3:02 AMpublic actual fun saveToBuffer(
type: String,
options: Map<String, String>,
bufferSize: Int,
error: Error?
): Pair<ByteArray, Boolean> {
val buffer = ByteArray(bufferSize)
val (keys, values) = options.createByteArrays()
var errorSet = false
val keysPinned = keys.pin()
val valuesPinned = values.pin()
buffer.usePinned { bufferPinned ->
errorSet = gdk_pixbuf_save_to_bufferv(
pixbuf = gdkPixbufPtr,
type = type,
error = cValuesOf(error?.gErrorPtr),
option_keys = cValuesOf(keysPinned.addressOf(0)),
option_values = cValuesOf(valuesPinned.addressOf(0)),
buffer_size = null,
buffer = cValuesOf(bufferPinned.addressOf(0))
) == TRUE
}
keysPinned.unpin()
valuesPinned.unpin()
return buffer to errorSet
}
Is only the first address of the array required for the option_keys, option_values, and buffer parameters?napperley
06/04/2021, 3:20 AM