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 <sys/mman.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 
34 #include "JNIHelp.h"
35 
36 #define NATIVE_METHOD(className, functionName, signature) \
37 { #functionName, signature, (void*)(Java_java_nio_ ## className ## _ ## functionName) }
38 
39 JNIEXPORT jboolean JNICALL
Java_java_nio_MappedByteBuffer_isLoaded0(JNIEnv * env,jobject obj,jlong address,jlong len,jint numPages)40 Java_java_nio_MappedByteBuffer_isLoaded0(JNIEnv *env, jobject obj, jlong address,
41                                          jlong len, jint numPages)
42 {
43     jboolean loaded = JNI_TRUE;
44     int result = 0;
45     int i = 0;
46     void *a = (void *) jlong_to_ptr(address);
47 #ifdef __linux__
48     unsigned char *vec = (unsigned char *)malloc(numPages * sizeof(char));
49 #else
50     char *vec = (char *)malloc(numPages * sizeof(char));
51 #endif
52 
53     if (vec == NULL) {
54         JNU_ThrowOutOfMemoryError(env, NULL);
55         return JNI_FALSE;
56     }
57 
58     result = mincore(a, (size_t)len, vec);
59     if (result == -1) {
60         JNU_ThrowIOExceptionWithLastError(env, "mincore failed");
61         free(vec);
62         return JNI_FALSE;
63     }
64 
65     for (i=0; i<numPages; i++) {
66         if (vec[i] == 0) {
67             loaded = JNI_FALSE;
68             break;
69         }
70     }
71     free(vec);
72     return loaded;
73 }
74 
75 
76 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBuffer_load0(JNIEnv * env,jobject obj,jlong address,jlong len)77 Java_java_nio_MappedByteBuffer_load0(JNIEnv *env, jobject obj, jlong address,
78                                      jlong len)
79 {
80     char *a = (char *)jlong_to_ptr(address);
81     int result = madvise((caddr_t)a, (size_t)len, MADV_WILLNEED);
82     if (result == -1) {
83         JNU_ThrowIOExceptionWithLastError(env, "madvise failed");
84     }
85 }
86 
87 
88 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBuffer_force0(JNIEnv * env,jobject obj,jobject fdo,jlong address,jlong len)89 Java_java_nio_MappedByteBuffer_force0(JNIEnv *env, jobject obj, jobject fdo,
90                                       jlong address, jlong len)
91 {
92     void* a = (void *)jlong_to_ptr(address);
93     int result = msync(a, (size_t)len, MS_SYNC);
94     if (result == -1) {
95         JNU_ThrowIOExceptionWithLastError(env, "msync failed");
96     }
97 }
98 
99 
100 static JNINativeMethod gMethods[] = {
101   NATIVE_METHOD(MappedByteBuffer, isLoaded0, "(JJI)Z"),
102   NATIVE_METHOD(MappedByteBuffer, load0, "(JJ)V"),
103   NATIVE_METHOD(MappedByteBuffer, force0, "(Ljava/io/FileDescriptor;JJ)V"),
104 };
105 
register_java_nio_MappedByteBuffer(JNIEnv * env)106 void register_java_nio_MappedByteBuffer(JNIEnv* env) {
107   jniRegisterNativeMethods(env, "java/nio/MappedByteBuffer", gMethods, NELEM(gMethods));
108 }
109