1 /*
   2  * Copyright (c) 2001, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "jlong.h"
  30 #include "java_nio_MappedByteBuffer.h"
  31 #include <stdlib.h>
  32 
  33 JNIEXPORT jboolean JNICALL
  34 Java_java_nio_MappedByteBuffer_isLoaded0(JNIEnv *env, jobject obj, jlong address,
  35                                          jlong len, jint numPages)
  36 {
  37     jboolean loaded = JNI_FALSE;
  38     /* Information not available?
  39     MEMORY_BASIC_INFORMATION info;
  40     void *a = (void *) jlong_to_ptr(address);
  41     int result = VirtualQuery(a, &info, (DWORD)len);
  42     */
  43     return loaded;
  44 }
  45 
  46 JNIEXPORT void JNICALL
  47 Java_java_nio_MappedByteBuffer_load0(JNIEnv *env, jobject obj, jlong address,
  48                                      jlong len)
  49 {
  50     // no madvise available
  51 }
  52 
  53 JNIEXPORT void JNICALL
  54 Java_java_nio_MappedByteBuffer_force0(JNIEnv *env, jobject obj, jobject fdo,
  55                                       jlong address, jlong len)
  56 {
  57     void *a = (void *) jlong_to_ptr(address);
  58     BOOL result;
  59     int retry;
  60 
  61     /*
  62      * FlushViewOfFile can fail with ERROR_LOCK_VIOLATION if the memory
  63      * system is writing dirty pages to disk. As there is no way to
  64      * synchronize the flushing then we retry a limited number of times.
  65      */
  66     retry = 0;
  67     do {
  68         result = FlushViewOfFile(a, (DWORD)len);
  69         if ((result != 0) || (GetLastError() != ERROR_LOCK_VIOLATION))
  70             break;
  71         retry++;
  72     } while (retry < 3);
  73 
  74     /**
  75      * FlushViewOfFile only initiates the writing of dirty pages to disk
  76      * so we have to call FlushFileBuffers to and ensure they are written.
  77      */
  78     if (result != 0) {
  79         // by right, the jfieldID initialization should be in a static
  80         // initializer but we do it here instead to avoiding needing to
  81         // load nio.dll during startup.
  82         static jfieldID handle_fdID;
  83         HANDLE h;
  84         if (handle_fdID == NULL) {
  85             jclass clazz = (*env)->FindClass(env, "java/io/FileDescriptor");
  86             if (clazz == NULL)
  87                 return; // exception thrown
  88             handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J");
  89         }
  90         h = jlong_to_ptr((*env)->GetLongField(env, fdo, handle_fdID));
  91         result = FlushFileBuffers(h);
  92         if (result == 0 && GetLastError() == ERROR_ACCESS_DENIED) {
  93             // read-only mapping
  94             result = 1;
  95         }
  96     }
  97 
  98     if (result == 0) {
  99         JNU_ThrowIOExceptionWithLastError(env, "Flush failed");
 100     }
 101 }