8286671: (fc) Modify sun.nio.ch.FileChannelImpl.map0() to accept a FileDescriptor parameter

Reviewed-by: alanb, jpai
This commit is contained in:
Brian Burkhalter 2022-05-13 17:46:52 +00:00
parent 1e843c3d4f
commit 583a61aabb
3 changed files with 13 additions and 21 deletions

View file

@ -1239,7 +1239,7 @@ public class FileChannelImpl
mapSize = size + pagePosition; mapSize = size + pagePosition;
try { try {
// If map0 did not throw an exception, the address is valid // If map0 did not throw an exception, the address is valid
addr = map0(prot, mapPosition, mapSize, isSync); addr = map0(fd, prot, mapPosition, mapSize, isSync);
} catch (OutOfMemoryError x) { } catch (OutOfMemoryError x) {
// An OutOfMemoryError may indicate that we've exhausted // An OutOfMemoryError may indicate that we've exhausted
// memory so force gc and re-attempt map // memory so force gc and re-attempt map
@ -1250,7 +1250,7 @@ public class FileChannelImpl
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
try { try {
addr = map0(prot, mapPosition, mapSize, isSync); addr = map0(fd, prot, mapPosition, mapSize, isSync);
} catch (OutOfMemoryError y) { } catch (OutOfMemoryError y) {
// After a second OOME, fail // After a second OOME, fail
throw new IOException("Map failed", y); throw new IOException("Map failed", y);
@ -1500,7 +1500,8 @@ public class FileChannelImpl
// -- Native methods -- // -- Native methods --
// Creates a new mapping // Creates a new mapping
private native long map0(int prot, long position, long length, boolean isSync) private native long map0(FileDescriptor fd, int prot, long position,
long length, boolean isSync)
throws IOException; throws IOException;
// Removes an existing mapping // Removes an existing mapping
@ -1514,12 +1515,12 @@ public class FileChannelImpl
// Retrieves the maximum size of a transfer // Retrieves the maximum size of a transfer
private static native int maxDirectTransferSize0(); private static native int maxDirectTransferSize0();
// Caches fieldIDs // Retrieves allocation granularity
private static native long initIDs(); private static native long allocationGranularity0();
static { static {
IOUtil.load(); IOUtil.load();
allocationGranularity = initIDs(); allocationGranularity = allocationGranularity0();
MAX_DIRECT_TRANSFER_SIZE = maxDirectTransferSize0(); MAX_DIRECT_TRANSFER_SIZE = maxDirectTransferSize0();
} }
} }

View file

@ -50,13 +50,10 @@
#include "java_lang_Integer.h" #include "java_lang_Integer.h"
#include <assert.h> #include <assert.h>
static jfieldID chan_fd; /* jobject 'fd' in sun.nio.ch.FileChannelImpl */
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz) Java_sun_nio_ch_FileChannelImpl_allocationGranularity0(JNIEnv *env, jclass clazz)
{ {
jlong pageSize = sysconf(_SC_PAGESIZE); jlong pageSize = sysconf(_SC_PAGESIZE);
chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
return pageSize; return pageSize;
} }
@ -73,11 +70,10 @@ handle(JNIEnv *env, jlong rv, char *msg)
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, jobject fdo,
jint prot, jlong off, jlong len, jboolean map_sync) jint prot, jlong off, jlong len, jboolean map_sync)
{ {
void *mapAddress = 0; void *mapAddress = 0;
jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
jint fd = fdval(env, fdo); jint fd = fdval(env, fdo);
int protections = 0; int protections = 0;
int flags = 0; int flags = 0;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -36,20 +36,16 @@
#include <Mswsock.h> #include <Mswsock.h>
#pragma comment(lib, "Mswsock.lib") #pragma comment(lib, "Mswsock.lib")
static jfieldID chan_fd; /* id for jobject 'fd' in java.io.FileChannel */
/************************************************************** /**************************************************************
* static method to store field ID's in initializers * static method to retrieve the allocation granularity
* and retrieve the allocation granularity
*/ */
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz) Java_sun_nio_ch_FileChannelImpl_allocationGranularity0(JNIEnv *env, jclass clazz)
{ {
SYSTEM_INFO si; SYSTEM_INFO si;
jint align; jint align;
GetSystemInfo(&si); GetSystemInfo(&si);
align = si.dwAllocationGranularity; align = si.dwAllocationGranularity;
chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
return align; return align;
} }
@ -59,7 +55,7 @@ Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
*/ */
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, jobject fdo,
jint prot, jlong off, jlong len, jboolean map_sync) jint prot, jlong off, jlong len, jboolean map_sync)
{ {
void *mapAddress = 0; void *mapAddress = 0;
@ -68,7 +64,6 @@ Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
jlong maxSize = off + len; jlong maxSize = off + len;
jint lowLen = (jint)(maxSize); jint lowLen = (jint)(maxSize);
jint highLen = (jint)(maxSize >> 32); jint highLen = (jint)(maxSize >> 32);
jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
HANDLE fileHandle = (HANDLE)(handleval(env, fdo)); HANDLE fileHandle = (HANDLE)(handleval(env, fdo));
HANDLE mapping; HANDLE mapping;
DWORD mapAccess = FILE_MAP_READ; DWORD mapAccess = FILE_MAP_READ;