[foreign-preview] RFR: 8283175: Add equals/hashcode to MemorySegment
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Tue Mar 15 12:32:06 UTC 2022
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.
-------------
Commit messages:
- Add missing <li>
- Fix function descriptor toString
- Merge branch 'foreign-preview' into segment_equals
- Initial push
- Add equals/hashCode to MemorySegment
Changes: https://git.openjdk.java.net/panama-foreign/pull/671/files
Webrev: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=671&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8283175
Stats: 70 lines in 5 files changed: 54 ins; 4 del; 12 mod
Patch: https://git.openjdk.java.net/panama-foreign/pull/671.diff
Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/671/head:pull/671
PR: https://git.openjdk.java.net/panama-foreign/pull/671
More information about the panama-dev
mailing list