Using MemoryAccess with structured MemoryLayout

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Feb 25 16:18:26 UTC 2021


Hi Markus,
to read inside the struct, you can:

* use the MemoryAccess API - but doing so is limited - e.g.
MemoryAccess only supports access by physical offset or logical index.

* create your own VarHandle which points to the desired part of the
layout, and use that

Here:

https://download.java.net/java/early_access/jdk16/docs/api/jdk.incubator.foreign/jdk/incubator/foreign/MemoryLayout.html

More specifically:


```
SequenceLayout taggedValues = MemoryLayout.ofSequence(5,
    MemoryLayout.ofStruct(
        MemoryLayout.ofValueBits(8, ByteOrder.nativeOrder()).withName("kind"),
        MemoryLayout.ofPaddingBits(24),
        MemoryLayout.ofValueBits(32, ByteOrder.nativeOrder()).withName("value")
    )
).withName("TaggedValues");

```

And

```
VarHandle valueHandle = taggedValues.varHandle(int.class,
                                               PathElement.sequenceElement(),
                                               PathElement.groupElement("value"));
```

Cheers
Maurizio


On Thu, 2021-02-25 at 17:01 +0100, Markus KARG wrote:
> On Windows, many API function have C struct as parameters.
> 
> It is rather straightforward to set up a structured MemoryLayout.
> 
> In case I want to easily poke bytes into that struct, I'd like to use
> MemoryAccess.
> 
> Unfortunately, there seem to be no EASY / SIMPLE way to write:
> 
> MemoryAccess.setIntAt(MEMBER_OF_SUCH_A_STRUCT, VALUE_OF_THAT_MEMBER);
> 
> .or I missed to see it in the JavaDocs.
> 
> Is this possible? If yes, how? If not, why not?
> 
> -Markus
> 
>  
> 


More information about the panama-dev mailing list