jnr / jnr-ffi

Java Abstracted Foreign Function Layer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

struct.size

shimanshe opened this issue · comments

I define a union struct,

public class ifr_ifru extends Union {
    public static final int IF_NAMESIZE = 16;
    public static final int IFNAMSIZ = IF_NAMESIZE;
    public final sockaddr ifru_addr;
    public final sockaddr ifru_dstaddr;
    public final sockaddr ifru_broadaddr;
    public final sockaddr ifru_netmask;
    public final sockaddr ifru_hwaddr;
    public final Signed16 ifru_flags;
    public final Signed32 ifru_ivalue;
    public final Signed32 ifru_mtu;
    public final ifmap ifru_map;
    public final Signed8[] ifru_slave;
    public final Signed8[] ifru_newname;
    public final Pointer ifru_data;

    protected ifr_ifru(Runtime runtime) {
        super(runtime);
        ifru_addr = inner(new sockaddr(runtime));
        ifru_dstaddr = inner(new sockaddr(runtime));
        ifru_broadaddr = inner(new sockaddr(runtime));
        ifru_netmask = inner(new sockaddr(runtime));
        ifru_hwaddr = inner(new sockaddr(runtime));
        ifru_flags = new Signed16();
        ifru_ivalue = new Signed32();
        ifru_mtu = new Signed32();
        ifru_map = inner(new ifmap(runtime));
        ifru_slave = array(new Signed8[IFNAMSIZ]);
        ifru_newname = array(new Signed8[IFNAMSIZ]);
        ifru_data = new Pointer();
    }
}

and Struct.size(ifr_ifru) is 48, but in c, the struct size is 16, where'is the problem?

Could you show me the C struct you are trying to emulate? Ideally provide a small zipfile or repository that I can build and run to see the problem.

structure ifreq defined in linux header file <net/if.h>, the c code is ,

#include <net/if.h>
printf("sizeof(ifreq)=%d\n", sizeof(struct ifreq));

and my jnr define is,

union {
    struct sockaddr ifru_addr;
    struct sockaddr ifru_dstaddr;
    struct sockaddr ifru_broadaddr;
    struct sockaddr ifru_netmask;
    struct sockaddr ifru_hwaddr;
    short int ifru_flags;
    int ifru_ivalue;
    int ifru_mtu;
    struct ifmap ifru_map;
    char ifru_slave[IFNAMSIZ];
    char ifru_newname[IFNAMSIZ];
    __caddr_t ifru_data;
} ifr_ifru;
typedef char *__caddr_t;
public class ifr_ifru extends Union {
    public static final int IF_NAMESIZE = 16;
    public static final int IFNAMSIZ = IF_NAMESIZE;
    public final sockaddr ifru_addr;
    public final sockaddr ifru_dstaddr;
    public final sockaddr ifru_broadaddr;
    public final sockaddr ifru_netmask;
    public final sockaddr ifru_hwaddr;
    public final Signed16 ifru_flags;
    public final Signed32 ifru_ivalue;
    public final Signed32 ifru_mtu;
    public final ifmap ifru_map;
    public final Signed8[] ifru_slave;
    public final Signed8[] ifru_newname;
    public final Pointer ifru_data;

    protected ifr_ifru(Runtime runtime) {
        super(runtime);
        ifru_addr = inner(new sockaddr(runtime));
        ifru_dstaddr = inner(new sockaddr(runtime));
        ifru_broadaddr = inner(new sockaddr(runtime));
        ifru_netmask = inner(new sockaddr(runtime));
        ifru_hwaddr = inner(new sockaddr(runtime));
        ifru_flags = new Signed16();
        ifru_ivalue = new Signed32();
        ifru_mtu = new Signed32();
        ifru_map = inner(new ifmap(runtime));
        ifru_slave = array(new Signed8[IFNAMSIZ]);
        ifru_newname = array(new Signed8[IFNAMSIZ]);
        ifru_data = new Pointer();
    }
}
```c
struct sockaddr {
  sa_family_t sa_family;
  char sa_data[14];
};
public class sockaddr extends Struct {
    /* typedef unsigned short int sa_family_t; */
    public final Unsigned16 sa_family = new Unsigned16();
    public final Signed8[] sa_data = array(new Signed8[14]);

    public sockaddr(Runtime runtime) {
        super(runtime);
    }
}
struct ifmap {
    unsigned long int mem_start;
    unsigned long int mem_end;
    unsigned short int base_addr;
    unsigned char irq;
    unsigned char dma;
    unsigned char port;
}
public class ifmap extends Struct {

    public final NativeLong mem_start = new NativeLong(0);
    public final NativeLong mem_end = new NativeLong(0);
    public final Struct.Unsigned16 base_addr = new Struct.Unsigned16();
    public final Struct.Unsigned8 irq = new Struct.Unsigned8();
    public final Struct.Unsigned8 dma = new Struct.Unsigned8();
    public final Struct.Unsigned8 port = new Struct.Unsigned8();

    public ifmap(Runtime runtime) {
        super(runtime);
    }
}

Ok thank you I will try to reproduce the issue.

I tried to edit your comment to show code properly, please make changes if I did anything wrong. Please put code blocks inside triple backquotes as described here (under Code): https://guides.github.com/features/mastering-markdown/