Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/native/libnio/MappedMemoryUtils.c
41119 views
1
/*
2
* Copyright (c) 2001, 2021, 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_MappedMemoryUtils.h"
31
#include <stdlib.h>
32
33
JNIEXPORT jboolean JNICALL
34
Java_java_nio_MappedMemoryUtils_isLoaded0(JNIEnv *env, jobject obj, jlong address,
35
jlong len, jlong numPages)
36
{
37
jboolean loaded = JNI_FALSE;
38
/* Information not available?
39
MEMORY_BASIC_INFORMATION info;
40
void *a = (void *) jlong_to_ptr(address);
41
int result = VirtualQuery(a, &info, (DWORD)len);
42
*/
43
return loaded;
44
}
45
46
JNIEXPORT void JNICALL
47
Java_java_nio_MappedMemoryUtils_load0(JNIEnv *env, jobject obj, jlong address,
48
jlong len)
49
{
50
// no madvise available
51
}
52
53
JNIEXPORT void JNICALL
54
Java_java_nio_MappedMemoryUtils_unload0(JNIEnv *env, jobject obj, jlong address,
55
jlong len)
56
{
57
// no madvise available
58
}
59
60
JNIEXPORT void JNICALL
61
Java_java_nio_MappedMemoryUtils_force0(JNIEnv *env, jobject obj, jobject fdo,
62
jlong address, jlong len)
63
{
64
void *a = (void *) jlong_to_ptr(address);
65
BOOL result;
66
int retry;
67
68
/*
69
* FlushViewOfFile can fail with ERROR_LOCK_VIOLATION if the memory
70
* system is writing dirty pages to disk. As there is no way to
71
* synchronize the flushing then we retry a limited number of times.
72
*/
73
retry = 0;
74
do {
75
result = FlushViewOfFile(a, (DWORD)len);
76
if ((result != 0) || (GetLastError() != ERROR_LOCK_VIOLATION))
77
break;
78
retry++;
79
} while (retry < 5);
80
81
/**
82
* FlushViewOfFile only initiates the writing of dirty pages to disk
83
* so we have to call FlushFileBuffers to and ensure they are written.
84
*/
85
if (result != 0) {
86
// by right, the jfieldID initialization should be in a static
87
// initializer but we do it here instead to avoiding needing to
88
// load nio.dll during startup.
89
static jfieldID handle_fdID;
90
HANDLE h;
91
if (handle_fdID == NULL) {
92
jclass clazz = (*env)->FindClass(env, "java/io/FileDescriptor");
93
CHECK_NULL(clazz); //exception thrown
94
handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J");
95
CHECK_NULL(handle_fdID);
96
}
97
h = jlong_to_ptr((*env)->GetLongField(env, fdo, handle_fdID));
98
result = FlushFileBuffers(h);
99
if (result == 0 && GetLastError() == ERROR_ACCESS_DENIED) {
100
// read-only mapping
101
result = 1;
102
}
103
}
104
105
if (result == 0) {
106
JNU_ThrowIOExceptionWithLastError(env, "Flush failed");
107
}
108
}
109
110