• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <nativehelper/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 // BEGIN Android-added: Fuchsia holds all pages in memory. http://b/119503290
44 #if defined(__Fuchsia__)
45     return JNI_TRUE;
46 #else
47 // END Android-added: Fuchsia holds all pages in memory. http://b/119503290
48     jboolean loaded = JNI_TRUE;
49     int result = 0;
50     int i = 0;
51     void *a = (void *) jlong_to_ptr(address);
52 #ifdef __linux__
53     unsigned char *vec = (unsigned char *)malloc(numPages * sizeof(char));
54 #else
55     char *vec = (char *)malloc(numPages * sizeof(char));
56 #endif
57 
58     if (vec == NULL) {
59         JNU_ThrowOutOfMemoryError(env, NULL);
60         return JNI_FALSE;
61     }
62 
63     result = mincore(a, (size_t)len, vec);
64     if (result == -1) {
65         JNU_ThrowIOExceptionWithLastError(env, "mincore failed");
66         free(vec);
67         return JNI_FALSE;
68     }
69 
70     for (i=0; i<numPages; i++) {
71         if (vec[i] == 0) {
72             loaded = JNI_FALSE;
73             break;
74         }
75     }
76     free(vec);
77     return loaded;
78 // Android-added: Fuchsia holds all pages in memory. http://b/119503290
79 #endif
80 }
81 
82 
83 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBuffer_load0(JNIEnv * env,jobject obj,jlong address,jlong len)84 Java_java_nio_MappedByteBuffer_load0(JNIEnv *env, jobject obj, jlong address,
85                                      jlong len)
86 {
87     char *a = (char *)jlong_to_ptr(address);
88     int result = madvise((caddr_t)a, (size_t)len, MADV_WILLNEED);
89     if (result == -1) {
90         JNU_ThrowIOExceptionWithLastError(env, "madvise failed");
91     }
92 }
93 
94 
95 JNIEXPORT void JNICALL
Java_java_nio_MappedByteBuffer_force0(JNIEnv * env,jobject obj,jobject fdo,jlong address,jlong len)96 Java_java_nio_MappedByteBuffer_force0(JNIEnv *env, jobject obj, jobject fdo,
97                                       jlong address, jlong len)
98 {
99     void* a = (void *)jlong_to_ptr(address);
100     int result = msync(a, (size_t)len, MS_SYNC);
101     if (result == -1) {
102         JNU_ThrowIOExceptionWithLastError(env, "msync failed");
103     }
104 }
105 
106 
107 static JNINativeMethod gMethods[] = {
108   NATIVE_METHOD(MappedByteBuffer, isLoaded0, "(JJI)Z"),
109   NATIVE_METHOD(MappedByteBuffer, load0, "(JJ)V"),
110   NATIVE_METHOD(MappedByteBuffer, force0, "(Ljava/io/FileDescriptor;JJ)V"),
111 };
112 
register_java_nio_MappedByteBuffer(JNIEnv * env)113 void register_java_nio_MappedByteBuffer(JNIEnv* env) {
114   jniRegisterNativeMethods(env, "java/nio/MappedByteBuffer", gMethods, NELEM(gMethods));
115 }
116