Search code examples
javajava-native-interfacejnagraalvmgraalvm-native-image

Declare "typedef int (*x)(void *)" in java with GraalVM


I'm trying to consume a C library with GraalMV and I have the following struct I don't find how to declare:


typedef int (*mon_handler)(void *);

typedef struct
{
  mon_handler mon_cb;

} opts;

I need to know with which data type declare mon_handler

@CStruct(value="opts")
interface Opts extends Pointerbase{
  
  @CField("mon_handler")
  ???????? getMonHandler();
}

Thanks in advance!!


Solution

  • After the typecasting of the void pointer this simplifies to

    typedef int *mon_handler;
    

    Where mon_handler is a pointer to an int. It's essentially equivalent to

    typedef (int *) mon_handler;
    

    So the correct mapping would be IntByReference in JNA. CIntPointer in GraalVM also matches this type:

    A pointer to a 32-bit C primitive value.