1 /*
   2  * Copyright (c) 2001, 2010, 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 <sys/mman.h>
  32 #include <stddef.h>
  33 #include <stdlib.h>
  34 
  35 JNIEXPORT jboolean JNICALL
  36 Java_java_nio_MappedByteBuffer_isLoaded0(JNIEnv *env, jobject obj, jlong address,
  37                                          jlong len, jint numPages)
  38 {
  39     jboolean loaded = JNI_TRUE;
  40     int result = 0;
  41     int i = 0;
  42     void *a = (void *) jlong_to_ptr(address);
  43 #ifdef __linux__
  44     unsigned char *vec = (unsigned char *)malloc(numPages * sizeof(char));
  45 #else
  46     char *vec = (char *)malloc(numPages * sizeof(char));
  47 #endif
  48 
  49     if (vec == NULL) {
  50         JNU_ThrowOutOfMemoryError(env, NULL);
  51         return JNI_FALSE;
  52     }
  53 
  54     result = mincore(a, (size_t)len, vec);
  55     if (result == -1) {
  56         JNU_ThrowIOExceptionWithLastError(env, "mincore failed");
  57         free(vec);
  58         return JNI_FALSE;
  59     }
  60 
  61     for (i=0; i<numPages; i++) {
  62         if (vec[i] == 0) {
  63             loaded = JNI_FALSE;
  64             break;
  65         }
  66     }
  67     free(vec);
  68     return loaded;
  69 }
  70 
  71 
  72 JNIEXPORT void JNICALL
  73 Java_java_nio_MappedByteBuffer_load0(JNIEnv *env, jobject obj, jlong address,
  74                                      jlong len)
  75 {
  76     char *a = (char *)jlong_to_ptr(address);
  77     int result = madvise((caddr_t)a, (size_t)len, MADV_WILLNEED);
  78     if (result == -1) {
  79         JNU_ThrowIOExceptionWithLastError(env, "madvise failed");
  80     }
  81 }
  82 
  83 
  84 JNIEXPORT void JNICALL
  85 Java_java_nio_MappedByteBuffer_force0(JNIEnv *env, jobject obj, jobject fdo,
  86                                       jlong address, jlong len)
  87 {
  88     void* a = (void *)jlong_to_ptr(address);
  89     int result = msync(a, (size_t)len, MS_SYNC);
  90     if (result == -1) {
  91         JNU_ThrowIOExceptionWithLastError(env, "msync failed");
  92     }
  93 }