MemorySegment javadoc (MacOS)

Per-Ake Minborg per-ake.minborg at oracle.com
Tue Jan 2 08:48:46 UTC 2024


Hi Michael!

Thanks for trying out the FFM API.

I am a bit uncertain as to which versions are used in the various examples you show.

In your first example, you must apply a "reinterpret" operation on the ptr because MemorySegment::ofAddress returns a MemorySegment of size zero. Presumably, the ptr is (at least) of size ValueLayout.ADDRESS.byteSize().

"Unsafe" in the context of MemorySegment operations means bypassing normal Java safety guarantees and, for example, creating a reference that points to arbitrary memory locations that, in turn, could crash the JVM or produce unpredictable results.

Thread safety, on the other hand, is another concept that means no sequence of valid operations can put an object in an illegal state, regardless of which thread operations were invoked.

In other words, you can use a MemorySegment from any number of threads (if allocated from an arena that is not confined). When you are using "unsafe" operations like reinterpret, you need to be careful and make sure you provide the right parameters or the result is unspecified.

Best, Per Minborg
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Michael Hall <mik3hall at gmail.com>
Sent: Saturday, December 30, 2023 4:10 AM
To: panama-dev at openjdk.org <panama-dev at openjdk.org>
Subject: MemorySegment javadoc (MacOS)

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240102/fb858d59/attachment-0001.htm>


More information about the panama-dev mailing list