Adam Cooper
09/07/2022, 9:30 PMusePinned
works in terms of C code? I am wondering why it is necessary, or perhaps if I am misusing it. If I am misusing it, then I would like to know what situations it should be used in. The way I have used it is similar to this:
// declare a 256 byte array to pass to a C function as an output parameter
UByteArray(SHA256_DIGEST_LENGTH).usePinned { signatureBuf ->
c_func(signatureBuf.addressOf(0))
}
My understanding of the equivalent of this in C:
unsigned char signatureBuf[SHA256_DIGEST_LENGTH];
c_func(signatureBuf);
c_func
computes some value and stores it in the address provided by its parameter. This is a simplistic example and obv not good code. Just want to show my understanding of it.Landry Norris
09/07/2022, 9:41 PMAdam Cooper
09/07/2022, 9:43 PMLandry Norris
09/07/2022, 9:43 PMsingatureBuf.addressOf(0)
to c_func
, we know the variable won’t mysteriously move. When it’s no longer needed by C, we unpin the memory, so Kotlin can move it as needed.Adam Cooper
09/07/2022, 9:44 PMLandry Norris
09/07/2022, 9:44 PMusePinned
, as this will unpin the variable when the lambda completes. If some C code uses the saved pointer, it’s possible the variable moved and the pointer points to invalid memory.Adam Cooper
09/07/2022, 9:45 PMAdam Cooper
09/07/2022, 9:46 PMephemient
09/07/2022, 9:46 PMLandry Norris
09/07/2022, 9:47 PMpin()
and unpin()
. If you know for sure that C will never need the pointer after c_func completes, however, then usePinned
is fine.ephemient
09/07/2022, 9:48 PMAdam Cooper
09/07/2022, 9:50 PMHMAC
function. I don't believe it carries any state, so once it returns I can do what I want with the variables.Landry Norris
09/07/2022, 9:51 PMephemient
09/07/2022, 9:51 PMAdam Cooper
09/07/2022, 9:53 PMLandry Norris
09/07/2022, 9:53 PMAdam Cooper
09/07/2022, 9:54 PMLandry Norris
09/07/2022, 9:54 PMLandry Norris
09/07/2022, 9:55 PMAdam Cooper
09/07/2022, 9:55 PMephemient
09/07/2022, 9:56 PMAdam Cooper
09/07/2022, 9:56 PMpublic suspend inline fun ApplicationCall.receiveText(): String {
val charset = try {
request.contentCharset() ?: Charsets.UTF_8
} catch (cause: BadContentTypeFormatException) {
throw BadRequestException("Illegal Content-Type format: ${request.headers[HttpHeaders.ContentType]}", cause)
}
return receiveChannel().readRemaining().readText(charset)
}
ephemient
09/07/2022, 9:57 PM'\0'
differentlyAdam Cooper
09/07/2022, 9:57 PMHMAC
does take a length parameter, so I don't think so, but I'll double check.Adam Cooper
09/07/2022, 9:58 PMAdam Cooper
09/07/2022, 10:53 PM