8 messages in net.java.dev.jna.usersRe: [jna-users] call to sysinfo() cau...
FromSent OnAttachments
Joel UckelmanApr 7, 2009 8:37 am 
Timothy WallApr 7, 2009 8:47 am 
Joel UckelmanApr 7, 2009 9:05 am 
Kevin BurtonApr 7, 2009 9:56 am 
Timothy WallApr 7, 2009 9:58 am 
Joel UckelmanApr 7, 2009 12:19 pm 
Joel UckelmanApr 7, 2009 12:22 pm 
Kevin BurtonApr 7, 2009 1:33 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] call to sysinfo() causes crashActions...
From:Joel Uckelman (ucke@nomic.net)
Date:Apr 7, 2009 9:05:44 am
List:net.java.dev.jna.users

Thus spake Timothy Wall:

On Apr 7, 2009, at 11:38 AM, Joel Uckelman wrote:

Hi,

I'm trying to call the sysinfo() function from the Linux C library:

int sysinfo(struct sysinfo *info);

struct sysinfo { long uptime; /* Seconds since boot */ unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ unsigned long totalram; /* Total usable main memory size */ unsigned long freeram; /* Available memory size */ unsigned long sharedram; /* Amount of shared memory */ unsigned long bufferram; /* Memory used by buffers */ unsigned long totalswap; /* Total swap space size */ unsigned long freeswap; /* swap space still available */ unsigned short procs; /* Number of current processes */ unsigned long totalhigh; /* Total high memory size */ unsigned long freehigh; /* Available high memory size */ unsigned int mem_unit; /* Memory unit size in bytes */ char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */ };

char _f[] != char*, at least not in the context of a struct. You're only allocating a pointer, when you need a character array.

Aha. That was not clear to me from the documentation. From the C side, there's no difference between char[] and char*, and the JNA docs say to use String for char*.

I changed the last element of the structure on the Java side to be this, to match the definition given in the C header:

public byte[] _f = new byte[20-2*NativeLong.SIZE-Native.getNativeSize(int.class)];

After that, it worked perfectly for me on 32-bit machines, but fails with this exception on 64-bit machines:

Exception in thread "main" java.lang.IllegalArgumentException: Arrays of
length zero not allowed in structure

Two 8-byte longs and one 4-byte int are 20 bytes together, so that makes the byte array _f have zero length on a 64-bit machine. In C, that would just make _f point to the end of the struct.