MemorySegment javadoc (MacOS)

Michael Hall mik3hall at gmail.com
Sat Dec 30 03:10:05 UTC 2023


Having a new arm Mac I am updating some of what I have to build on that.

This no longer worked.

MemorySegment flagsArray = MemorySegment.ofAddress(eventFlagsPtr,
        			paths.length * SIZEOF_FS_EVENT_STREAM_EVENT_FLAGS);

Looking at the MemorySegment documentation

MemorySegment foreign = someSegment.get(ValueLayout.ADDRESS, 0); // size = 0
                                    .reinterpret(4)               // size = 4

(Nit: the semicolon is misplaced)

I thought I should have something like

MemorySegment ptr = MemorySegment.ofAddress(eventFlagsPtr);
MemorySegment flagsArray = ptr.get(ValueLayout.ADDRESS, 0).
							.reinterpret(paths.length * SIZEOF_FS_EVENT_STREAM_EVENT_FLAGS);

This didn’t work with an index out of bounds exception. Which seemed awkward but should of worked?

I went with the more straight forward 

            MemorySegment flagsArray = MemorySegment.ofAddress(eventFlagsPtr).
            		reinterpret(paths.length * SIZEOF_FS_EVENT_STREAM_EVENT_FLAGS);

…which worked. If this is considered a more straightforward jdk21 way of doing the above jdk20 code should it be in the javadoc?

Also I find a little confusing where it indicates 

clients have several options, all of which are unsafe.

But then at the end has…

Implementation Requirements:
Implementations of this interface are immutable, thread-safe and value-based.

Is it safe or unsafe? Unsafe in the sense that the size could be incorrect?

Thanks,

Mike


More information about the panama-dev mailing list