Struct with Dynamic Layout
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Mon Aug 10 10:15:05 UTC 2020
On 08/08/2020 05:08, Ardika Rommy Sanjaya wrote:
> In native c/c++ size of long is depends on the platform, ex "struct
> timeval" in Unix and Windows has a different size.
>
> // for unix
> @NativeStruct("[i64(tv_sec)i32(tv_usec)x32](timeval)")
>
> // for windows
> @NativeStruct("[i32(tv_sec)i32(tv_usec)](timeval)")
>
> Is it possible to create a new dynamic layout where I can use a single
> struct class with different sizes on a different platform?
I see that you are referring to NativeStruct annotations, which were
part of the older annotation-driven Panama story.
You can find the latest documents explaining the current state of
foreign interop here:
http://cr.openjdk.java.net/~mcimadamore/panama/foreign-memaccess.html
and
http://cr.openjdk.java.net/~mcimadamore/panama/ffi.html
To describe layouts we now use layout objects, with an API. The FFI
support introduces a set of (basic, for now) platform-dependent layouts
to work with C:
CSupport.C_INT, CSupport.C_DOUBLE
You can create a complex struct layouts where the leaf value layouts are
those defined in CSupport; that might work.
That said, note that, as your example demonstrates, it's not just a
matter of swapping 32 for 64 - in most cases padding and sizes are affected.
So I think the most robust way to do things would be to replicate what
CSupport does:
static final MemoryLayout TIMEVAL =
switch (CSupport.getSystemLinker().name()) {
CSupport.Win64.NAME -> // win64 layout
CSupport.SysV.NAME -> // linux layout
CSupport.AArch64.NAME -> // aarch layout
};
Hope this helps
Maurizio
>
> Thanks,
> Rommy
More information about the panama-dev
mailing list