[foreign-preview] Integrated: 8283175: Add equals/hashcode to MemorySegment
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Tue Mar 15 14:11:50 UTC 2022
On Tue, 15 Mar 2022 12:25:33 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.
This pull request has now been integrated.
Changeset: 5b15ef73
Author: Maurizio Cimadamore <mcimadamore at openjdk.org>
URL: https://git.openjdk.java.net/panama-foreign/commit/5b15ef731c7486bd2f39500491e21cc7ef549c88
Stats: 107 lines in 7 files changed: 91 ins; 4 del; 12 mod
8283175: Add equals/hashcode to MemorySegment
Reviewed-by: sundar
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/671
More information about the panama-dev
mailing list