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