Skip to content

Commit

Permalink
8323552: AbstractMemorySegmentImpl#mismatch returns -1 when comparing…
Browse files Browse the repository at this point in the history
… distinct areas of the same instance of MemorySegment

Reviewed-by: mcimadamore
  • Loading branch information
minborg committed Mar 25, 2024
1 parent b235682 commit 93579c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -681,10 +681,6 @@ public static long mismatch(MemorySegment srcSegment, long srcFromOffset, long s
long dstBytes = dstToOffset - dstFromOffset;
srcImpl.checkAccess(srcFromOffset, srcBytes, true);
dstImpl.checkAccess(dstFromOffset, dstBytes, true);
if (dstImpl == srcImpl) {
srcImpl.checkValidState();
return -1;
}

long bytes = Math.min(srcBytes, dstBytes);
long i = 0;
Expand Down
31 changes: 29 additions & 2 deletions test/jdk/java/foreign/TestMismatch.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,6 +23,7 @@

/*
* @test
* @bug 8323552
* @run testng TestMismatch
*/

Expand All @@ -44,7 +45,7 @@

public class TestMismatch {

// stores a increasing sequence of values into the memory of the given segment
// stores an increasing sequence of values into the memory of the given segment
static MemorySegment initializeSegment(MemorySegment segment) {
for (int i = 0 ; i < segment.byteSize() ; i++) {
segment.set(ValueLayout.JAVA_BYTE, i, (byte)i);
Expand Down Expand Up @@ -278,6 +279,32 @@ public void testThreadAccess() throws Exception {
}
}

@Test
public void testSameSegment() {
var segment = MemorySegment.ofArray(new byte[]{
1,2,3,4, 1,2,3,4, 1,4});

long match = MemorySegment.mismatch(
segment, 0L, 4L,
segment, 4L, 8L);
assertEquals(match, -1);

long noMatch = MemorySegment.mismatch(
segment, 0L, 4L,
segment, 1L, 5L);
assertEquals(noMatch, 0);

long noMatchEnd = MemorySegment.mismatch(
segment, 0L, 2L,
segment, 8L, 10L);
assertEquals(noMatchEnd, 1);

long same = MemorySegment.mismatch(
segment, 0L, 8L,
segment, 0L, 8L);
assertEquals(same, -1);
}

enum SegmentKind {
NATIVE(i -> Arena.ofAuto().allocate(i, 1)),
ARRAY(i -> MemorySegment.ofArray(new byte[i]));
Expand Down

1 comment on commit 93579c2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.