Obtain MemorySegment from MemoryAddress for a given MemoryLayout

Sebastian Stenzel sebastian.stenzel at gmail.com
Mon Dec 28 18:15:37 UTC 2020


During my latest experiments with Panama, I have to deal a lot with MemoryAddresses pointing to structs, that I need to access from Java:

Let's say I have demo.h declaring my_struct:

```
struct my_struct {
  int foo;
};

```

If I run jextract on demo.h, it'll generate code that allows me to get "foo" from a segment, but not from an address:

```
int getFooFromMyStruct(MemoryAddress addr) {
    var segment = demo_h.my_struct.ofAddressRestricted(addr); // is there a better way, if I know the layout for sure?
    return demo_h.my_struct.foo$get(segment); // segment required here
}
```

Now I'm wondering, if there is any safe (read: not restricted) way to obtain a MemorySegment for a given layout and address?


More information about the panama-dev mailing list