Path: blob/master/src/java.base/windows/native/libnio/MappedMemoryUtils.c
41119 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"29#include "java_nio_MappedMemoryUtils.h"30#include <stdlib.h>3132JNIEXPORT jboolean JNICALL33Java_java_nio_MappedMemoryUtils_isLoaded0(JNIEnv *env, jobject obj, jlong address,34jlong len, jlong numPages)35{36jboolean loaded = JNI_FALSE;37/* Information not available?38MEMORY_BASIC_INFORMATION info;39void *a = (void *) jlong_to_ptr(address);40int result = VirtualQuery(a, &info, (DWORD)len);41*/42return loaded;43}4445JNIEXPORT void JNICALL46Java_java_nio_MappedMemoryUtils_load0(JNIEnv *env, jobject obj, jlong address,47jlong len)48{49// no madvise available50}5152JNIEXPORT void JNICALL53Java_java_nio_MappedMemoryUtils_unload0(JNIEnv *env, jobject obj, jlong address,54jlong len)55{56// no madvise available57}5859JNIEXPORT void JNICALL60Java_java_nio_MappedMemoryUtils_force0(JNIEnv *env, jobject obj, jobject fdo,61jlong address, jlong len)62{63void *a = (void *) jlong_to_ptr(address);64BOOL result;65int retry;6667/*68* FlushViewOfFile can fail with ERROR_LOCK_VIOLATION if the memory69* system is writing dirty pages to disk. As there is no way to70* synchronize the flushing then we retry a limited number of times.71*/72retry = 0;73do {74result = FlushViewOfFile(a, (DWORD)len);75if ((result != 0) || (GetLastError() != ERROR_LOCK_VIOLATION))76break;77retry++;78} while (retry < 5);7980/**81* FlushViewOfFile only initiates the writing of dirty pages to disk82* so we have to call FlushFileBuffers to and ensure they are written.83*/84if (result != 0) {85// by right, the jfieldID initialization should be in a static86// initializer but we do it here instead to avoiding needing to87// load nio.dll during startup.88static jfieldID handle_fdID;89HANDLE h;90if (handle_fdID == NULL) {91jclass clazz = (*env)->FindClass(env, "java/io/FileDescriptor");92CHECK_NULL(clazz); //exception thrown93handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J");94CHECK_NULL(handle_fdID);95}96h = jlong_to_ptr((*env)->GetLongField(env, fdo, handle_fdID));97result = FlushFileBuffers(h);98if (result == 0 && GetLastError() == ERROR_ACCESS_DENIED) {99// read-only mapping100result = 1;101}102}103104if (result == 0) {105JNU_ThrowIOExceptionWithLastError(env, "Flush failed");106}107}108109110