jdk/src/windows/native/java/nio/MappedByteBuffer.c
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2001-2007 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any 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,
       
    35                                         jlong address, jlong len)
       
    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 jint JNICALL
       
    47 Java_java_nio_MappedByteBuffer_load0(JNIEnv *env, jobject obj, jlong address,
       
    48                                      jlong len, jint pageSize)
       
    49 {
       
    50     int *ptr = (int *) jlong_to_ptr(address);
       
    51     int pageIncrement = pageSize / sizeof(int);
       
    52     jlong numPages = (len + pageSize - 1) / pageSize;
       
    53     int i = 0;
       
    54     int j = 0;
       
    55 
       
    56     /* touch every page */
       
    57     for (i=0; i<numPages; i++) {
       
    58         j += *((volatile int *)ptr);
       
    59         ptr += pageIncrement;
       
    60     }
       
    61     return j;
       
    62 }
       
    63 
       
    64 JNIEXPORT void JNICALL
       
    65 Java_java_nio_MappedByteBuffer_force0(JNIEnv *env, jobject obj, jlong address,
       
    66                                       jlong len)
       
    67 {
       
    68     void *a = (void *) jlong_to_ptr(address);
       
    69     int result;
       
    70     int retry;
       
    71 
       
    72     /*
       
    73      * FlushViewOfFile can fail with ERROR_LOCK_VIOLATION if the memory
       
    74      * system is writing dirty pages to disk. As there is no way to
       
    75      * synchronize the flushing then we retry a limited number of times.
       
    76      */
       
    77     retry = 0;
       
    78     do {
       
    79         result = FlushViewOfFile(a, (DWORD)len);
       
    80         if ((result != 0) || (GetLastError() != ERROR_LOCK_VIOLATION))
       
    81             break;
       
    82         retry++;
       
    83     } while (retry < 3);
       
    84 
       
    85     if (result == 0) {
       
    86         JNU_ThrowIOExceptionWithLastError(env, "Flush failed");
       
    87     }
       
    88 }