Skip to content

Commit

Permalink
Include padding field only if bitness < 64
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Aug 15, 2018
1 parent 73e8e76 commit 73eb1c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
22 changes: 12 additions & 10 deletions contrib/platform/src/com/sun/jna/platform/linux/LibC.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@
import com.sun.jna.platform.unix.LibCAPI;

/**
* <I>libc</I> API
*
* @author Daniel Widdis
* LibC structures and functions unique to Linux
*/
public interface LibC extends LibCAPI, Library {
String NAME = "c";
LibC INSTANCE = Native.load(NAME, LibC.class);

@FieldOrder({ "uptime", "loads", "totalram", "freeram", "sharedram", "bufferram", "totalswap", "freeswap", "procs",
"totalhigh", "freehigh", "mem_unit", "_f" })
"totalhigh", "freehigh", "mem_unit" })
class Sysinfo extends Structure {
public NativeLong uptime; // Seconds since boot
// 1, 5, and 15 minute load averages
Expand All @@ -55,12 +53,16 @@ class Sysinfo extends Structure {
public NativeLong totalhigh; // Total high memory size
public NativeLong freehigh; // Available high memory size
public int mem_unit; // Memory unit size in bytes
// Padding is based on the size of NativeLong. When the size is 4 we
// need 8 bytes of padding. When this size is 8, zero padding is needed
// but we are not allowed to declare an array of size zero. It's safe to
// declare extra padding in the structure; these bytes simply will not
// be written on 64-bit systems.
public byte[] _f = new byte[8]; // padding to 64 bytes
}

/**
* Sysinfo structure for OS bitness < 64.
*/
@FieldOrder({ "uptime", "loads", "totalram", "freeram", "sharedram", "bufferram", "totalswap", "freeswap", "procs",
"totalhigh", "freehigh", "mem_unit", "_f" })
class Sysinfo32 extends Sysinfo {
// Padding to 64 bytes
public byte[] _f = new byte[20 - 2 * NativeLong.SIZE - 4];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import org.junit.Test;

import com.sun.jna.NativeLong;
import com.sun.jna.platform.linux.LibC.Sysinfo;
import com.sun.jna.platform.linux.LibC.Sysinfo32;

import junit.framework.TestCase;

Expand All @@ -38,7 +40,7 @@ public class LibCTest extends TestCase {

@Test
public void testSysinfo() {
Sysinfo info = new Sysinfo();
Sysinfo info = NativeLong.SIZE < 8 ? new Sysinfo32() : new Sysinfo();
assertEquals(0, LibC.INSTANCE.sysinfo(info));

// Get loadavg for comparison (rounds to nearest hundredth)
Expand Down

0 comments on commit 73eb1c4

Please sign in to comment.