Skip to content

Commit

Permalink
8333884: MemorySegment::reinterpret removes read-only property
Browse files Browse the repository at this point in the history
Reviewed-by: liach, sundar
Backport-of: 6f7f0f1
  • Loading branch information
JornVernee committed Jul 11, 2024
1 parent e991c0f commit 9620b91
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/java.base/share/classes/java/lang/foreign/MemorySegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* asSlice(offset, newSize, 1);
* }
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand All @@ -650,6 +653,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* alignment constraint. The returned segment's address is the address of this
* segment plus the given offset; its size is specified by the given argument.
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand Down Expand Up @@ -677,6 +683,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* asSlice(offset, layout.byteSize(), layout.byteAlignment());
* }
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand All @@ -703,6 +712,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* asSlice(offset, byteSize() - offset);
* }
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand All @@ -719,6 +731,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* Returns a new memory segment that has the same address and scope as this segment,
* but with the provided size.
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand Down Expand Up @@ -757,6 +772,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* scope, and is accessible from any thread. The size of the segment accepted by the
* cleanup action is {@link #byteSize()}.
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand Down Expand Up @@ -805,6 +823,9 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
* scope, and is accessible from any thread. The size of the segment accepted by the
* cleanup action is {@code newSize}.
* <p>
* If this segment is {@linkplain MemorySegment#isReadOnly() read-only},
* the returned segment is also {@linkplain MemorySegment#isReadOnly() read-only}.
* <p>
* The returned memory segment shares a region of backing memory with this segment.
* Hence, no memory will be allocated or freed by this method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public MemorySegment reinterpretInternal(Class<?> callerClass, long newSize, Sco
() -> cleanup.accept(SegmentFactories.makeNativeSegmentUnchecked(address(), newSize)) :
null;
return SegmentFactories.makeNativeSegmentUnchecked(address(), newSize,
(MemorySessionImpl)scope, action);
(MemorySessionImpl)scope, readOnly, action);
}

private AbstractMemorySegmentImpl asSliceNoCheck(long offset, long newSize) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 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 @@ -57,14 +57,18 @@ public class SegmentFactories {
// associated with MemorySegment::ofAddress.

@ForceInline
public static MemorySegment makeNativeSegmentUnchecked(long min, long byteSize, MemorySessionImpl sessionImpl, Runnable action) {
public static MemorySegment makeNativeSegmentUnchecked(long min,
long byteSize,
MemorySessionImpl sessionImpl,
boolean readOnly,
Runnable action) {
ensureInitialized();
if (action == null) {
sessionImpl.checkValidState();
} else {
sessionImpl.addCloseAction(action);
}
return new NativeMemorySegmentImpl(min, byteSize, false, sessionImpl);
return new NativeMemorySegmentImpl(min, byteSize, readOnly, sessionImpl);
}

@ForceInline
Expand Down
12 changes: 8 additions & 4 deletions test/jdk/java/foreign/TestSegments.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 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 @@ -43,7 +43,6 @@
import java.util.function.Supplier;

import static java.lang.foreign.ValueLayout.JAVA_INT;
import static java.lang.foreign.ValueLayout.JAVA_LONG;
import static org.testng.Assert.*;

public class TestSegments {
Expand Down Expand Up @@ -383,9 +382,14 @@ void testReinterpret() {
assertEquals(MemorySegment.ofAddress(42).reinterpret(100, Arena.ofAuto(), null).byteSize(), 100);
// check scope and cleanup
assertEquals(MemorySegment.ofAddress(42).reinterpret(100, arena, s -> counter.incrementAndGet()).scope(), arena.scope());
assertEquals(MemorySegment.ofAddress(42).reinterpret(arena, s -> counter.incrementAndGet()).scope(), arena.scope());
assertEquals(MemorySegment.ofAddress(42).reinterpret(arena, _ -> counter.incrementAndGet()).scope(), arena.scope());
// check read-only state
assertFalse(MemorySegment.ofAddress(42).reinterpret(100).isReadOnly());
assertTrue(MemorySegment.ofAddress(42).asReadOnly().reinterpret(100).isReadOnly());
assertTrue(MemorySegment.ofAddress(42).asReadOnly().reinterpret(100, Arena.ofAuto(), null).isReadOnly());
assertTrue(MemorySegment.ofAddress(42).asReadOnly().reinterpret(arena, _ -> counter.incrementAndGet()).isReadOnly());
}
assertEquals(counter.get(), 2);
assertEquals(counter.get(), 3);
}

@Test
Expand Down

1 comment on commit 9620b91

@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.