Edoardo Luppi
11/06/2023, 1:34 PMval array = allocArray<CHARVar>(size)
can I get the size of the array from the array
itself? Or should I carry the size
around?mbonnin
11/06/2023, 1:36 PMsize
around. My mental model of allocArray
is that it's just like C malloc()
, all it returns is a pointerEdoardo Luppi
11/06/2023, 1:37 PMkevin.cianfarini
11/06/2023, 1:37 PMint a[17];
size_t n = sizeof(a)/sizeof(a[0]);
Edoardo Luppi
11/06/2023, 1:39 PMPair
with the size.mbonnin
11/06/2023, 1:39 PMsizeof
?int a[17]
is not using malloc()
Edoardo Luppi
11/06/2023, 1:40 PMsizeOf
works in K/N honestly. Never bothered to investigate.Oleg Yukhnevich
11/06/2023, 1:41 PMsizeOf<T: CVariable>()
therembonnin
11/06/2023, 1:41 PMsizeof()
👀Edoardo Luppi
11/06/2023, 1:42 PMmbonnin
11/06/2023, 1:42 PMallocArray()
is calling to malloc()
under the hood, right? so the return value doesn't contain a size?Edoardo Luppi
11/06/2023, 1:43 PMalloc(sizeOf<T>() * length, alignOf<T>()).reinterpret<T>().ptr
mbonnin
11/06/2023, 1:44 PMmalloc()
that doesn't go through C-stdlib but looks like the semantics are the same and the size is not returned to the callerOleg Yukhnevich
11/06/2023, 1:44 PMmbonnin
11/06/2023, 1:46 PMsizeOf<Array<IntVar>(17)>()
because Kotlin doesn't carry size information in the type system itselfEdoardo Luppi
11/06/2023, 1:50 PMinternal fun LPWSTR.toLatin1Buffer(): Pair<Int, LPSTR> {
val latin1BufferSize = WideCharToMultiByte(ISO_8859_1, 0u, this, 0, null, 0, null, null)
val latin1buffer = allocArray<CHARVar>(latin1BufferSize)
WideCharToMultiByte(ISO_8859_1, 0u, this, 0, latin1buffer, 0, null, null)
return latin1BufferSize to latin1buffer
}
To:
internal fun LPWSTR.toLatin1Buffer(): ByteArray {
val latin1BufferSize = WideCharToMultiByte(ISO_8859_1, 0u, this, 0, null, 0, null, null)
val latin1buffer = ByteArray(latin1BufferSize)
latin1buffer.usePinned {
WideCharToMultiByte(ISO_8859_1, 0u, this, 0, it.addressOf(0), 0, null, null)
}
return latin1buffer
}
Oleg Yukhnevich
11/06/2023, 1:54 PMEdoardo Luppi
11/06/2023, 1:56 PMallocArray<WCHARVar>(utf16BufferSize)
is UShortArray(utf16BufferSize)
It's definitely not an immediate mapping.mbonnin
11/06/2023, 2:00 PMEdoardo Luppi
11/06/2023, 2:04 PMThis underlying issue is with C thoughThe real issue (at least in case of Windows) is the stupid hungarian notation and all the wide (W) type variants. Too many typealiases.
mbonnin
11/06/2023, 2:04 PMEdoardo Luppi
11/06/2023, 2:04 PMmbonnin
11/06/2023, 2:04 PMEdoardo Luppi
11/06/2023, 2:15 PMinternal value class Utf16String(val buffer: UShortArray)
internal value class Latin1String(val buffer: ByteArray)
internal fun String.toUtf16String(): Utf16String {
// Convert the Kotlin UTF-8 string into a UTF-16 buffer
val utf16BufferSize = MultiByteToWideChar(UTF_8, 0u, this, -1, null, 0)
val utf16Buffer = UShortArray(utf16BufferSize)
utf16Buffer.usePinned {
MultiByteToWideChar(UTF_8, 0u, this, -1, it.addressOf(0), 0)
}
return Utf16String(utf16Buffer)
}
internal fun Utf16String.toLatin1String(): Latin1String {
buffer.usePinned { pinnedBuffer ->
val bufferAddress = pinnedBuffer.addressOf(0)
val latin1BufferSize = WideCharToMultiByte(ISO_8859_1, 0u, bufferAddress, 0, null, 0, null, null)
val latin1Buffer = ByteArray(latin1BufferSize)
latin1Buffer.usePinned {
WideCharToMultiByte(ISO_8859_1, 0u, bufferAddress, 0, it.addressOf(0), 0, null, null)
}
return Latin1String(latin1Buffer)
}
}