leerho commented on code in PR #669:
URL: https://github.com/apache/datasketches-java/pull/669#discussion_r2187798899


##########
src/main/java/org/apache/datasketches/common/MemorySegmentStatus.java:
##########
@@ -51,8 +51,41 @@ public interface MemorySegmentStatus {
    * <p>Returns false if either argument is null;</p>
    *
    * @param that The given MemorySegment.
-   * @return true if the backing MemorySegment of this object hierarchy refers 
to the same MemorySegment of <i>that</i>.
+   * @return true if an internally referenced MemorySegment refers to the same 
MemorySegment as <i>that</i>.
    */
   boolean isSameResource(final MemorySegment that);
 
+  /**
+   * Returns true if the two given MemorySegments refer to the same backing 
resource,
+   * which is either an off-heap memory address and size, or the same on-heap 
array object.
+   *
+   * <p>If both segment are off-heap, they both must have the same starting 
address and the same size.</p>
+   *
+   * <p>For on-heap segments, both segments must be based on or derived from 
the same array object and neither segment
+   * can be read-only.</p>
+   *
+   * <p>Returns false if either argument is null;</p>
+   *
+   * @param seg1 The first given MemorySegment
+   * @param seg2 The second given MemorySegment
+   * @return true if both MemorySegments are determined to be the same backing 
memory.
+   */
+  static boolean isSameResource(final MemorySegment seg1, final MemorySegment 
seg2) {
+    if ((seg1 == null) || (seg2 == null)) { return false; }
+    if (!seg1.scope().isAlive() || !seg2.scope().isAlive()) {
+      throw new IllegalArgumentException("Both arguments must be alive.");
+    }
+    final boolean seg1Native = seg1.isNative();
+    final boolean seg2Native = seg2.isNative();
+    if (seg1Native ^ seg2Native) { return false; }
+    if (seg1Native && seg2Native) { //both off heap
+      return (seg1.address() == seg2.address()) && (seg1.byteSize() == 
seg2.byteSize());
+    }
+    //both on heap
+    if (seg1.isReadOnly() || seg2.isReadOnly()) {
+      throw new IllegalArgumentException("Cannot determine 
'isSameBackingMemory(..)' on heap if either MemorySegment is Read-only.");

Review Comment:
   Yes. How dumb! My mistake!  If they are both on heap I can just compare with 
"==".
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to