AW: Using MemoryAccess with structured MemoryLayout
Markus KARG
markus at headcrashing.eu
Thu Feb 25 17:31:52 UTC 2021
Maurizio,
thank you for your kind answer.
Yes, indeed I am already using VarHandle currently, but actually I like the idea of MemoryAccess more, as the code looks a bit simpler to me.
What I envision is something like doing this instead, as it spares one code line (the actual invocation of the VarValue):
```
MemorySegment valueSegment = taggedValues.memorySegment(
PathElement.sequenceElement(3),
PathElement.groupElement("value"));
MemoryAccess.setInt(valueSegment, someInteger);
```
It would be cool to have this additional possibility, as it makes using structs rather simple compared to the VarHandle way.
-Markus
-----Ursprüngliche Nachricht-----
Von: Maurizio Cimadamore [mailto:maurizio.cimadamore at oracle.com]
Gesendet: Donnerstag, 25. Februar 2021 17:18
An: markus at headcrashing.eu; panama-dev at openjdk.java.net
Betreff: Re: Using MemoryAccess with structured MemoryLayout
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