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