8308281: Java snippets in the FFM API need to be updated

Reviewed-by: mcimadamore
This commit is contained in:
Per Minborg 2023-05-23 11:29:05 +00:00
parent 26227a6ff8
commit eb11508eff
7 changed files with 734 additions and 49 deletions

View file

@ -48,7 +48,7 @@ import java.lang.foreign.MemorySegment.Scope;
* accessible and their backing regions of memory are never deallocated. Moreover, memory segments allocated with the
* global arena can be {@linkplain MemorySegment#isAccessibleBy(Thread) accessed} from any thread.
* {@snippet lang = java:
* MemorySegment segment = Arena.global().allocate(100, 1);
* MemorySegment segment = Arena.global().allocate(100, 1); // @highlight regex='global()'
* ...
* // segment is never deallocated!
*}
@ -58,9 +58,8 @@ import java.lang.foreign.MemorySegment.Scope;
* of memory backing memory segments allocated with the automatic arena are deallocated at some unspecified time
* <em>after</em> the automatic arena (and all the segments allocated by it) become
* <a href="../../../java/lang/ref/package.html#reachability">unreachable</a>, as shown below:
*
* {@snippet lang = java:
* MemorySegment segment = Arena.ofAuto().allocate(100, 1);
* MemorySegment segment = Arena.ofAuto().allocate(100, 1); // @highlight regex='ofAuto()'
* ...
* segment = null; // the segment region becomes available for deallocation after this point
*}
@ -77,7 +76,7 @@ import java.lang.foreign.MemorySegment.Scope;
*
* {@snippet lang = java:
* MemorySegment segment = null;
* try (Arena arena = Arena.ofConfined()) {
* try (Arena arena = Arena.ofConfined()) { // @highlight regex='ofConfined()'
* segment = arena.allocate(100);
* ...
* } // segment region deallocated here
@ -157,24 +156,25 @@ import java.lang.foreign.MemorySegment.Scope;
*
* {@snippet lang = java:
* class SlicingArena implements Arena {
* final Arena arena = Arena.ofConfined();
* final SegmentAllocator slicingAllocator;
* final Arena arena = Arena.ofConfined();
* final SegmentAllocator slicingAllocator;
*
* SlicingArena(long size) {
* slicingAllocator = SegmentAllocator.slicingAllocator(arena.allocate(size));
* }
* SlicingArena(long size) {
* slicingAllocator = SegmentAllocator.slicingAllocator(arena.allocate(size));
* }
*
* public void allocate(long byteSize, long byteAlignment) {
* return slicingAllocator.allocate(byteSize, byteAlignment);
* }
* public MemorySegment allocate(long byteSize, long byteAlignment) {
* return slicingAllocator.allocate(byteSize, byteAlignment);
* }
*
* public MemorySegment.Scope scope() {
* return arena.scope();
* }
* public MemorySegment.Scope scope() {
* return arena.scope();
* }
*
* public void close() {
* arena.close();
* }
*
* public void close() {
* return arena.close();
* }
* }
* }
*
@ -183,10 +183,10 @@ import java.lang.foreign.MemorySegment.Scope;
*
* {@snippet lang = java:
* try (Arena slicingArena = new SlicingArena(1000)) {
* for (int i = 0 ; i < 10 ; i++) {
* MemorySegment s = slicingArena.allocateArray(JAVA_INT, 1, 2, 3, 4, 5);
* ...
* }
* for (int i = 0; i < 10; i++) {
* MemorySegment s = slicingArena.allocateArray(JAVA_INT, 1, 2, 3, 4, 5);
* ...
* }
* } // all memory allocated is released here
* }
*
@ -253,8 +253,8 @@ public interface Arena extends SegmentAllocator, AutoCloseable {
* {@code S1, S2} returned by this method, the following invariant must hold:
*
* {@snippet lang = java:
* S1.overlappingSlice(S2).isEmpty() == true
*}
* S1.asOverlappingSlice(S2).isEmpty() == true
* }
*
* @param byteSize the size (in bytes) of the off-heap memory block backing the native memory segment.
* @param byteAlignment the alignment constraint (in bytes) of the off-heap region of memory backing the native memory segment.