8286709: (fc) FileChannel/FileChannelImpl cleanup

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2022-05-26 18:02:02 +00:00
parent 7eb15593e1
commit 7cb368b34d
2 changed files with 57 additions and 53 deletions

View File

@ -942,7 +942,7 @@ public abstract class FileChannel
* The size of the region to be mapped; must be non-negative and
* no greater than {@link java.lang.Integer#MAX_VALUE}
*
* @return The mapped byte buffer
* @return The mapped byte buffer
*
* @throws NonReadableChannelException
* If the {@code mode} is {@link MapMode#READ_ONLY READ_ONLY} or
@ -997,61 +997,60 @@ public abstract class FileChannel
* of mapped memory associated with the returned mapped memory
* segment is unspecified and should not be relied upon.
*
* @param mode
* The file mapping mode, see
* {@link FileChannel#map(FileChannel.MapMode, long, long)};
* the mapping mode might affect the behavior of the returned memory
* mapped segment (see {@link MemorySegment#force()}).
* @param mode
* The file mapping mode, see
* {@link FileChannel#map(FileChannel.MapMode, long, long)};
* the mapping mode might affect the behavior of the returned
* memory mapped segment (see {@link MemorySegment#force()}).
*
* @param offset
* The offset (expressed in bytes) within the file at which the
* mapped segment is to start.
* @param offset
* The offset (expressed in bytes) within the file at which the
* mapped segment is to start.
*
* @param size
* The size (in bytes) of the mapped memory backing the memory
* segment.
* @param session
* The segment memory session.
* @param size
* The size (in bytes) of the mapped memory backing the memory
* segment.
*
* @return A new mapped memory segment.
* @param session
* The segment memory session.
*
* @throws IllegalArgumentException
* If {@code offset < 0}, {@code size < 0} or
* {@code offset + size} overflows the range of {@code long}.
* @return A new mapped memory segment.
*
* @throws IllegalStateException
* If the {@code session} is not
* {@linkplain MemorySession#isAlive() alive}.
* @throws IllegalArgumentException
* If {@code offset < 0}, {@code size < 0} or
* {@code offset + size} overflows the range of {@code long}.
*
* @throws WrongThreadException
* If this method is called from a thread other than the thread
* {@linkplain MemorySession#ownerThread() owning} the
* {@code session}.
* @throws IllegalStateException
* If the {@code session} is not
* {@linkplain MemorySession#isAlive() alive}.
*
* @throws NonReadableChannelException
* If the {@code mode} is {@link MapMode#READ_ONLY READ_ONLY} or
* an implementation specific map mode requiring read access,
* but this channel was not opened for reading.
* @throws WrongThreadException
* If this method is called from a thread other than the thread
* {@linkplain MemorySession#ownerThread() owning} the
* {@code session}.
*
* @throws NonWritableChannelException
* If the {@code mode} is {@link MapMode#READ_WRITE READ_WRITE},
* {@link MapMode#PRIVATE PRIVATE} or an implementation specific
* map mode requiring write access, but this channel was not
* opened for both reading and writing.
* @throws NonReadableChannelException
* If the {@code mode} is {@link MapMode#READ_ONLY READ_ONLY} or
* an implementation specific map mode requiring read access,
* but this channel was not opened for reading.
*
* @throws IOException
* If some other I/O error occurs.
* @throws NonWritableChannelException
* If the {@code mode} is {@link MapMode#READ_WRITE READ_WRITE},
* {@link MapMode#PRIVATE PRIVATE} or an implementation specific
* map mode requiring write access, but this channel was not
* opened for both reading and writing.
*
* @throws UnsupportedOperationException
* If an unsupported map mode is specified.
* @throws IOException
* If some other I/O error occurs.
*
* @since 19
* @throws UnsupportedOperationException
* If an unsupported map mode is specified.
*
* @since 19
*/
@PreviewFeature(feature=PreviewFeature.Feature.FOREIGN)
public MemorySegment map(MapMode mode, long offset, long size,
MemorySession session)
throws IOException
public MemorySegment map(MapMode mode, long offset, long size, MemorySession session)
throws IOException
{
throw new UnsupportedOperationException();
}

View File

@ -1211,8 +1211,10 @@ public class FileChannelImpl
Objects.requireNonNull(session, "Session is null");
MemorySessionImpl sessionImpl = MemorySessionImpl.toSessionImpl(session);
sessionImpl.checkValidStateSlow();
if (offset < 0) throw new IllegalArgumentException("Requested bytes offset must be >= 0.");
if (size < 0) throw new IllegalArgumentException("Requested bytes size must be >= 0.");
if (offset < 0)
throw new IllegalArgumentException("Requested bytes offset must be >= 0.");
if (size < 0)
throw new IllegalArgumentException("Requested bytes size must be >= 0.");
boolean isSync = isSync(mode);
int prot = toProt(mode);
@ -1222,14 +1224,17 @@ public class FileChannelImpl
modes |= MAP_MEM_SEG_READ_ONLY;
}
if (unmapper != null) {
AbstractMemorySegmentImpl segment = new MappedMemorySegmentImpl(unmapper.address(), unmapper, size,
modes, session);
sessionImpl.addOrCleanupIfFail(new MemorySessionImpl.ResourceList.ResourceCleanup() {
@Override
public void cleanup() {
unmapper.unmap();
}
});
AbstractMemorySegmentImpl segment =
new MappedMemorySegmentImpl(unmapper.address(), unmapper, size,
modes, session);
MemorySessionImpl.ResourceList.ResourceCleanup resource =
new MemorySessionImpl.ResourceList.ResourceCleanup() {
@Override
public void cleanup() {
unmapper.unmap();
}
};
sessionImpl.addOrCleanupIfFail(resource);
return segment;
} else {
return new MappedMemorySegmentImpl.EmptyMappedMemorySegmentImpl(modes, session);