[foreign-memaccess+abi] RFR: 8300201: When storing MemoryAddress.ofLong(0x0000000080000000L), MemorySegment.get is not equal to MemorySegment.set because of the expanded sign [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Jan 23 18:14:15 UTC 2023


On Mon, 23 Jan 2023 17:47:40 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> This PR proposes to normalize the upper 32-bit to zero on 32-bit systems.
>
> Per Minborg has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Update comments for 32-bit platforms
>  - Update src/java.base/share/classes/java/lang/foreign/MemorySegment.java
>    
>    Co-authored-by: Maurizio Cimadamore <54672762+mcimadamore at users.noreply.github.com>

> Sorry to follow on here, but a question related to this -- you can use the upper 16 bits of a 64-bit pointer for pointer tagging, and also the lower 3-bits if it's on a word-aligned boundary.
> 
> This is really useful for building things like atomic tagged pointers, where you can store an ABA-like counter in the 16 bits and use the 3 bits for whatever userdata tag you want:
> 
> ```
> // A word-aligned, atomic tagged pointer.
> // Uses both the upper 16 bits for storage, and the lower 3 bits for tagging.
> //
> //   64                48                32                16
> // 0xXXXXXXXXXXXXXXXX  0000000000000000  0000000000000000  0000000000000XXX
> //   ^                 ^                                                ^
> //   |                 |                                                +-- Tag (3 bits)
> //   |                 +-- Pointer (48 bits)
> //   +-- Counter (16 bits)
> ```
> 
> Is this possible with `MemoryAddress` and bitmasks currently in Java thinking
> 
> (Or does an optimization like that even make sense on the JVM?)

On 64 bit platforms you can decide how to create your 64-bit addresses with `MemorySegment.ofAddress`. So, if you want to encode extra bit patterns on the high order bits, you can do so. But then you are also responsible for filtering these out, which require some extra case (because you have a memory segment whose base address is essentially invalid and cannot be used for access operation). Probably you will need/want to encapsulate the logic that deals with this segments. To prevent users from accessing these directly (which will likely lead to a crash).

That said, this has nothing to do with this PR, which is about when the user creates a new segment from a long address on a 32-bit system.

-------------

PR: https://git.openjdk.org/panama-foreign/pull/774


More information about the panama-dev mailing list