AW: Using MemoryAccess with structured MemoryLayout

Markus KARG markus at
Thu Feb 25 17:31:52 UTC 2021


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(
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.


-----Ursprüngliche Nachricht-----
Von: Maurizio Cimadamore [mailto:maurizio.cimadamore at] 
Gesendet: Donnerstag, 25. Februar 2021 17:18
An: markus at; panama-dev at
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


More specifically:

SequenceLayout taggedValues = MemoryLayout.ofSequence(5,
        MemoryLayout.ofValueBits(8, ByteOrder.nativeOrder()).withName("kind"),
        MemoryLayout.ofValueBits(32, ByteOrder.nativeOrder()).withName("value")



VarHandle valueHandle = taggedValues.varHandle(int.class,


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:
> .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