Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/native/libjimage/osSupport_windows.cpp
41119 views
1
/*
2
* Copyright (c) 2015, 2018, 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 <windows.h>
27
#include <sys/types.h>
28
#include <sys/stat.h>
29
#include <fcntl.h>
30
#include <io.h>
31
#include <malloc.h>
32
33
#include "jni.h"
34
#include "osSupport.hpp"
35
36
/**
37
* Open a regular file read-only.
38
* Return the file descriptor.
39
*/
40
jint osSupport::openReadOnly(const char *path) {
41
// jimage file descriptors must not be inherited by child processes
42
return ::open(path, O_BINARY | O_NOINHERIT, O_RDONLY);
43
}
44
45
/**
46
* Close a file descriptor.
47
*/
48
jint osSupport::close(jint fd) {
49
return ::close(fd);
50
}
51
52
/**
53
* Return the size of a regular file.
54
*/
55
jlong osSupport::size(const char *path) {
56
struct stat statbuf;
57
if (stat(path, &statbuf) < 0 ||
58
(statbuf.st_mode & S_IFREG) != S_IFREG) {
59
return -1;
60
}
61
return (jlong) statbuf.st_size;
62
}
63
64
/**
65
* Read nBytes at offset into a buffer.
66
*/
67
jlong osSupport::read(jint fd, char *buf, jlong nBytes, jlong offset) {
68
OVERLAPPED ov;
69
DWORD nread;
70
BOOL result;
71
72
ZeroMemory(&ov, sizeof (ov));
73
ov.Offset = (DWORD) offset;
74
ov.OffsetHigh = (DWORD) (offset >> 32);
75
76
HANDLE h = (HANDLE)::_get_osfhandle(fd);
77
78
result = ReadFile(h, (LPVOID) buf, (DWORD) nBytes, &nread, &ov);
79
80
return result ? nread : 0;
81
}
82
83
/**
84
* Map nBytes at offset into memory and return the address.
85
* The system chooses the address.
86
*/
87
void* osSupport::map_memory(jint fd, const char *file_name, size_t file_offset, size_t bytes) {
88
HANDLE hFile;
89
char* base = NULL;
90
91
// Get a handle to the file
92
hFile = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
93
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
94
if (hFile != NULL) {
95
// Create a file mapping handle
96
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0,
97
NULL /* file_name */);
98
if (hMap != NULL) {
99
// Map the file into the address space at the offset
100
base = (char*) MapViewOfFileEx(hMap, FILE_MAP_READ, 0, (DWORD) file_offset,
101
(DWORD) bytes, NULL);
102
CloseHandle(hMap); // The mapping is no longer needed
103
}
104
CloseHandle(hFile); // The file handle is no longer needed
105
}
106
return base;
107
}
108
109
/**
110
* Unmap nBytes of memory at address.
111
*/
112
int osSupport::unmap_memory(void* addr, size_t bytes) {
113
BOOL result = UnmapViewOfFile(addr);
114
return result;
115
}
116
117
/**
118
* A CriticalSection to protect a small section of code.
119
*/
120
void SimpleCriticalSection::enter() {
121
EnterCriticalSection(&critical_section);
122
}
123
124
void SimpleCriticalSection::exit() {
125
LeaveCriticalSection(&critical_section);
126
}
127
128
SimpleCriticalSection::SimpleCriticalSection() {
129
InitializeCriticalSection(&critical_section);
130
}
131
132
//SimpleCriticalSection::~SimpleCriticalSection() {
133
// DeleteCriticalSection(&critical_section);
134
//}
135
136
137