Kotlin/Native: how to modify CArrayPointer?

355 views Asked by At

I'm trying to use K/N with libsecret. It includes the following struct:

typedef struct {
    const gchar *name;
    SecretSchemaFlags flags;
    SecretSchemaAttribute attributes[32];
} SecretSchema;

(from here: https://developer.gnome.org/libsecret/0.18/libsecret-SecretSchema.html#SecretSchema)

cinterop generated following class:

@kotlinx.cinterop.internal.CStruct public final class SecretSchema public constructor(rawPtr: kotlinx.cinterop.NativePtr /* = kotlin.native.internal.NativePtr */) : kotlinx.cinterop.CStructVar {
   @kotlinx.cinterop.internal.CStruct.VarType public companion object : kotlinx.cinterop.CStructVar.Type {
   }

   public final val attributes: kotlinx.cinterop.CArrayPointer<org.libsecret.SecretSchemaAttribute> /* = kotlinx.cinterop.CPointer<org.libsecret.SecretSchemaAttribute> */ /* compiled code */

   public final var flags: org.libsecret.SecretSchemaFlags /* = kotlin.UInt */ /* compiled code */

   public final var name: kotlinx.cinterop.CPointer<org.libsecret.gcharVar /* = kotlinx.cinterop.ByteVarOf<kotlin.Byte> */>? /* compiled code */

   public final var reserved: org.libsecret.gint /* = kotlin.Int */ /* compiled code */

   public final var reserved1: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */

   public final var reserved2: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */

   public final var reserved3: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */

   public final var reserved4: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */

   public final var reserved5: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */

   public final var reserved6: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */

   public final var reserved7: org.libsecret.gpointer? /* = kotlinx.cinterop.CPointer<out kotlinx.cinterop.CPointed>? */ /* compiled code */
}

and I'm trying to define it like:

alloc<SecretSchema> {
   name = "com.charlag.tuta-bridge".cstr.ptr
   flags = SECRET_SCHEMA_NONE
}

however, I don't see a way to modify attributes because CArrayPointer (CPointer) does not expose modification methods.

I've also noticed some of the reserved fields. Should I use them instead?

here's example of how it's used in C: https://developer.gnome.org/libsecret/0.18/c-examples.html#c-schema-example

2

There are 2 answers

1
Artyom Degtyarev On

As far as I can see, the most straightforward option here would be to use the [] operator and assign allocated SecretSchemaAttribute to those addresses. To learn more about pointer types interaction, check this documentation page. It might be a bit out-of-date, but further questions could be asked here or at the kotlinlang Slack.

0
charlag On

Seems like there's no way by default, I ended up doing this:

/**
 * This is a version of the function which is missing in standard library. It initializes members
 * of the array.
 * If possible, allocate array of correct elements instead. It might not be possible with C
 * structures.
 */
inline fun <reified T : CVariable> CArrayPointer<T>.setAt(index: Int, value: CValue<T>) {
    value.write(this.rawValue + index * sizeOf<T>())
}

It works for me and uses the same pointer position calculation as access