2

I am trying to interface with a C library libzbc using jnr-ffi.

There is a function:

int zbc_list_zones ( struct zbc_device * dev, uint64_t sector, enum zbc_reporting_options ro, struct zbc_zone ** zones, unsigned int * nr_zones )

How can i read back the array of structures from the parameter zones ?

Second question: is the nr_zones parameter declared correctly? The resulting integer is negative, should be positive. I tried using ShortByReference ,gives a reasonable result but i do not think that is correct.

I tried the following to read the first element but it doesn't work (gives zero result):

PointerByReference zones=new PointerByReference();
IntByReference nr_zones=new IntByReference();
zbc_zone zone=new zbc_zone(runtime);
z.zbc_list_zones(dev, 0, zbc_reporting_options.ZBC_RO_ALL, zones, nr_zones);
zone.useMemory(zones.getValue());
System.out.println("zone 0: "+zone.zbz_length);

zbc_zone structure:

public static class zbc_zone extends Struct 
{
    protected zbc_zone(Runtime runtime) {
        super(runtime);
    }
    public final Unsigned64 zbz_length=new Unsigned64();
    public final Unsigned64 zbz_start=new Unsigned64();
    public final Unsigned64 zbz_write_pointer=new Unsigned64();
    public final Unsigned8 zbz_type=new Unsigned8();
    public final Unsigned8 zbz_conditions=new Unsigned8();
    public final Unsigned8 zbz_attributes=new Unsigned8();
    public final Unsigned8 zbz_pad1=new Unsigned8();
    public final Unsigned8 zbz_pad2=new Unsigned8();
    public final Unsigned8 zbz_pad3=new Unsigned8();
    public final Unsigned8 zbz_pad4=new Unsigned8();
    public final Unsigned8 zbz_pad5=new Unsigned8();
}

JNR interface:

public int zbc_list_zones(@In Pointer dev,@In long sector,
    @In zbc_reporting_options options,@Out PointerByReference zones,
    @Out IntByReference nr_zones);
Sidias-Korrado
  • 383
  • 3
  • 12

1 Answers1

1

There are three methods that will help you:

  1. Pointer#slice - will return a new pointer moved by offset.
  2. Struct#size - returns the size of struct in bytes.
  3. Struct#useMemory - maps memory to struct fields.

So you should create a struct, remember its size, and for every i in n slice i*size from original pointer and use the new pointer's memory in a created structure.

goto1134
  • 107
  • 11