[foreign-preview] RFR: 8283175: Add equals/hashcode to MemorySegment [v2]
Athijegannathan Sundararajan
sundar at openjdk.java.net
Tue Mar 15 13:21:54 UTC 2022
On Tue, 15 Mar 2022 12:42:29 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> When looking at the API, we realized that `MemorySegment` is the only class not to expose equals/hashcode (`MemoryLayout`, `MemoryAddress` and `MemorySession` do). This makes it difficult to e.g. use a `MemorySegment` as a map key.
>>
>> This patch adds equals/hashcode; the behavior is that of a shallow comparison - e.g. equals makes sure that the two segments point to the same memory region and have same spatial and temporal bounds. Read-only status is ignored by the comparison (on the basis that the segment is still pointing to the same memory region). And, since comparing memory session uses `MemorySession::equals`, segments backed by different "views" of the same session will also turn out to be equal (which also makes sense).
>>
>> To perform a deeper comparison, clients can go through the `MemorySegment::mismatch` path, but I don't think that's a useful implementation for equals/hashCode (even though that's how byte buffers behave - but then, byte buffers do not provide a mismatch method).
>>
>> When fixing this I realized that the equals method for `FunctionDescriptor` did not take into account the variable arity index, so I've fixed that too (and also tweaked the toString method to take that into account as well). Doing this revealed an issue in some of the calling convention tests, where we passed a variable arity descriptor as the expected descriptor, where in reality the calling convention "flattens" the variable arity descriptor into a specialized, fixed-arity one.
>
> Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
>
> Add tests
LGTM
-------------
Marked as reviewed by sundar (Committer).
PR: https://git.openjdk.java.net/panama-foreign/pull/671
More information about the panama-dev
mailing list