2727package java .lang .foreign ;
2828
2929import java .io .UncheckedIOException ;
30+ import java .lang .ref .Cleaner ;
3031import java .lang .reflect .Array ;
3132import java .lang .invoke .MethodHandles ;
3233import java .nio .Buffer ;
6667 * Heap segments can be obtained by calling one of the {@link MemorySegment#ofArray(int[])} factory methods.
6768 * These methods return a memory segment backed by the on-heap region that holds the specified Java array.
6869 * <p>
69- * Native segments can be obtained by calling one of the {@link MemorySegment#allocateNative(long, long)}
70+ * Native segments can be obtained by calling one of the {@link MemorySegment#allocateNative(long, long, MemorySession )}
7071 * factory methods, which return a memory segment backed by a newly allocated off-heap region with the given size
7172 * and aligned to the given alignment constraint. Alternatively, native segments can be obtained by
7273 * {@link FileChannel#map(MapMode, long, long, MemorySession) mapping} a file into a new off-heap region
9495 * Every memory segment has a {@linkplain #byteSize() size}. The size of a heap segment is derived from the Java array
9596 * from which it is obtained. This size is predictable across Java runtimes.
9697 * The size of a native segment is either passed explicitly
97- * (as in {@link MemorySegment#allocateNative(long)}) or derived from a {@link MemoryLayout}
98- * (as in {@link MemorySegment#allocateNative(MemoryLayout)}). The size of a memory segment is typically
98+ * (as in {@link MemorySegment#allocateNative(long, MemorySession )}) or derived from a {@link MemoryLayout}
99+ * (as in {@link MemorySegment#allocateNative(MemoryLayout, MemorySession )}). The size of a memory segment is typically
99100 * a positive number but may be <a href="#wrapping-addresses">zero</a>, but never negative.
100101 * <p>
101102 * The address and size of a memory segment jointly ensure that access operations on the segment cannot fall
244245 * <p>
245246 * The alignment constraint used to access a segment is typically dictated by the shape of the data structure stored
246247 * in the segment. For example, if the programmer wishes to store a sequence of 8-byte values in a native segment, then
247- * the segment should be allocated by specifying a 8-byte alignment constraint, either via {@link #allocateNative(long, long)}
248- * or {@link #allocateNative(MemoryLayout)}. These factories ensure that the off-heap region of memory backing
248+ * the segment should be allocated by specifying a 8-byte alignment constraint, either via {@link #allocateNative(long, long, MemorySession )}
249+ * or {@link #allocateNative(MemoryLayout, MemorySession )}. These factories ensure that the off-heap region of memory backing
249250 * the returned segment has a starting address that is 8-byte aligned. Subsequently, the programmer can access the
250251 * segment at the offsets of interest -- 0, 8, 16, 24, etc -- in the knowledge that every such access is aligned.
251252 * <p>
@@ -509,7 +510,7 @@ default MemorySegment asSlice(long offset) {
509510
510511 /**
511512 * Returns {@code true} if this segment is a native segment. A native segment is
512- * created e.g. using the {@link #allocateNative(long)} (and related) factory, or by
513+ * created e.g. using the {@link #allocateNative(long, MemorySession )} (and related) factory, or by
513514 * {@linkplain #ofBuffer(Buffer) wrapping} a {@linkplain ByteBuffer#allocateDirect(int) direct buffer}.
514515 * @return {@code true} if this segment is native segment.
515516 */
@@ -1114,72 +1115,81 @@ static MemorySegment ofAddress(long address, long byteSize, MemorySession sessio
11141115 }
11151116
11161117 /**
1117- * Creates a native segment with the given layout.
1118+ * Creates a native segment with the given layout and memory session.
1119+ * <p>
1120+ * Clients are responsible for ensuring that the memory session associated with the returned segment is
1121+ * closed when segments are no longer in use. Failure to do so will result in off-heap memory leaks. As an
1122+ * alternative to explicitly invoke {@link MemorySession#close()}, sessions backed with a {@link Cleaner}
1123+ * (such as {@linkplain MemorySession#openImplicit()}) can be used, allowing the returned segment to be
1124+ * automatically released some unspecified time after the session is no longer referenced.
11181125 * <p>
11191126 * The {@linkplain #address() address} of the returned memory segment is the starting address of
11201127 * the newly allocated off-heap region backing the segment. Moreover, the {@linkplain #address() address}
11211128 * of the returned segment will be aligned according to the alignment constraint of the provided layout.
11221129 * <p>
1123- * The returned memory segment is associated with a new {@linkplain MemorySession#openImplicit implicit}
1124- * memory session. As such, the off-heap region which backs the returned segment is
1125- * freed <em>automatically</em>, some unspecified time after it is no longer referenced.
1126- * <p>
1127- * Native segments featuring deterministic deallocation can be obtained using the
1128- * {@link MemorySession#allocate(MemoryLayout)} method.
1129- * <p>
11301130 * This is equivalent to the following code:
11311131 * {@snippet lang=java :
1132- * MemorySession.openImplicit()
1133- * .allocate(layout.bytesSize(), layout.bytesAlignment());
1132+ * session.allocate(layout.bytesSize(), layout.bytesAlignment());
11341133 * }
11351134 * <p>
11361135 * The region of off-heap region backing the returned native segment is initialized to zero.
11371136 *
11381137 * @param layout the layout of the off-heap memory region backing the native segment.
1138+ * @param session the session to which the returned segment is associated.
11391139 * @return a new native segment.
1140+ * @throws IllegalStateException if the {@code session} is not {@linkplain MemorySession#isAlive() alive}.
1141+ * @throws WrongThreadException if this method is called from a thread other than the thread owning {@code session}.
11401142 * @see MemorySession#allocate(MemoryLayout)
11411143 */
1142- static MemorySegment allocateNative (MemoryLayout layout ) {
1144+ static MemorySegment allocateNative (MemoryLayout layout , MemorySession session ) {
11431145 Objects .requireNonNull (layout );
1144- return allocateNative (layout .byteSize (), layout .byteAlignment ());
1146+ return session . allocate (layout .byteSize (), layout .byteAlignment ());
11451147 }
11461148
11471149 /**
1148- * Creates a native segment with the given size (in bytes).
1150+ * Creates a native segment with the given size (in bytes) and memory session.
1151+ * <p>
1152+ * Clients are responsible for ensuring that the memory session associated with the returned segment is
1153+ * closed when segments are no longer in use. Failure to do so will result in off-heap memory leaks. As an
1154+ * alternative to explicitly invoke {@link MemorySession#close()}, sessions backed with a {@link Cleaner}
1155+ * (such as {@linkplain MemorySession#openImplicit()}) can be used, allowing the returned segment to be
1156+ * automatically released some unspecified time after the session is no longer referenced.
11491157 * <p>
11501158 * The {@linkplain #address() address} of the returned memory segment is the starting address of
11511159 * the newly allocated off-heap region backing the segment. Moreover, the {@linkplain #address() address}
11521160 * of the returned segment is guaranteed to be at least 1-byte aligned.
11531161 * <p>
1154- * The returned memory segment is associated with a new {@linkplain MemorySession#openImplicit implicit}
1155- * memory session. As such, the off-heap region which backs the returned segment is
1156- * freed <em>automatically</em>, some unspecified time after it is no longer referenced.
1157- * <p>
1158- * Native segments featuring deterministic deallocation can be obtained using the
1159- * {@link MemorySession#allocate(long)} method.
1160- * <p>
11611162 * This is equivalent to the following code:
11621163 * {@snippet lang=java :
1163- * MemorySession.openImplicit()
1164- * .allocate(byteSize, 1)
1164+ * session.allocate(byteSize, 1);
11651165 * }
11661166 * <p>
11671167 * The region of off-heap region backing the returned native segment is initialized to zero.
11681168 * <p>
11691169 * This method corresponds to the {@link ByteBuffer#allocateDirect(int)} method and has similar behavior.
11701170 *
11711171 * @param byteSize the size (in bytes) of the off-heap memory region of memory backing the native memory segment.
1172+ * @param session the session to which the returned segment is associated.
11721173 * @return a new native memory segment.
11731174 * @throws IllegalArgumentException if {@code byteSize < 0}.
1175+ * @throws IllegalStateException if the {@code session} is not {@linkplain MemorySession#isAlive() alive}.
1176+ * @throws WrongThreadException if this method is called from a thread other than the thread owning {@code session}.
11741177 * @see ByteBuffer#allocateDirect(int)
11751178 * @see MemorySession#allocate(long)
11761179 */
1177- static MemorySegment allocateNative (long byteSize ) {
1178- return allocateNative (byteSize , 1L );
1180+ static MemorySegment allocateNative (long byteSize , MemorySession session ) {
1181+ Utils .checkAllocationSizeAndAlign (byteSize , 1L );
1182+ return session .allocate (byteSize , 1L );
11791183 }
11801184
11811185 /**
1182- * Creates a native segment with the given size (in bytes) and alignment (in bytes).
1186+ * Creates a native segment with the given size (in bytes), alignment (in bytes) and session.
1187+ * <p>
1188+ * Clients are responsible for ensuring that the memory session associated with the returned segment is
1189+ * closed when segments are no longer in use. Failure to do so will result in off-heap memory leaks. As an
1190+ * alternative to explicitly invoke {@link MemorySession#close()}, sessions backed with a {@link Cleaner}
1191+ * (such as {@linkplain MemorySession#openImplicit()}) can be used, allowing the returned segment to be
1192+ * automatically released some unspecified time after the session is no longer referenced.
11831193 * <p>
11841194 * The {@linkplain #address() address} of the returned memory segment is the starting address of
11851195 * the newly allocated off-heap region backing the segment. Moreover, the {@linkplain #address() address}
@@ -1189,28 +1199,26 @@ static MemorySegment allocateNative(long byteSize) {
11891199 * memory session. As such, the off-heap region which backs the returned segment is
11901200 * freed <em>automatically</em>, some unspecified time after it is no longer referenced.
11911201 * <p>
1192- * Native segments featuring deterministic deallocation can be obtained using the
1193- * {@link MemorySession#allocate(long,long)} method.
1194- * <p>
11951202 * This is equivalent to the following code:
11961203 * {@snippet lang=java :
1197- * MemorySession.openImplicit()
1198- * .allocate(byteSize, byteAlignment)
1204+ * session.allocate(byteSize, byteAlignment);
11991205 * }
12001206 * <p>
12011207 * The region of off-heap region backing the returned native segment is initialized to zero.
12021208 *
12031209 * @param byteSize the size (in bytes) of the off-heap region of memory backing the native memory segment.
12041210 * @param byteAlignment the alignment constraint (in bytes) of the off-heap region of memory backing the native memory segment.
1211+ * @param session the session to which the returned segment is associated.
12051212 * @return a new native memory segment.
12061213 * @throws IllegalArgumentException if {@code byteSize < 0}, {@code byteAlignment <= 0}, or if {@code byteAlignment}
1207- * is not a power of 2.
1214+ * is not a power of 2.
1215+ * @throws IllegalStateException if the {@code session} is not {@linkplain MemorySession#isAlive() alive}.
1216+ * @throws WrongThreadException if this method is called from a thread other than the thread owning {@code session}.
12081217 * @see MemorySession#allocate(long, long)
12091218 */
1210- static MemorySegment allocateNative (long byteSize , long byteAlignment ) {
1219+ static MemorySegment allocateNative (long byteSize , long byteAlignment , MemorySession session ) {
12111220 Utils .checkAllocationSizeAndAlign (byteSize , byteAlignment );
1212- return MemorySession .openImplicit ()
1213- .allocate (byteSize , byteAlignment );
1221+ return session .allocate (byteSize , byteAlignment );
12141222 }
12151223
12161224 /**
0 commit comments