Path: blob/master/src/java.base/windows/native/libjimage/osSupport_windows.cpp
41119 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <windows.h>26#include <sys/types.h>27#include <sys/stat.h>28#include <fcntl.h>29#include <io.h>30#include <malloc.h>3132#include "jni.h"33#include "osSupport.hpp"3435/**36* Open a regular file read-only.37* Return the file descriptor.38*/39jint osSupport::openReadOnly(const char *path) {40// jimage file descriptors must not be inherited by child processes41return ::open(path, O_BINARY | O_NOINHERIT, O_RDONLY);42}4344/**45* Close a file descriptor.46*/47jint osSupport::close(jint fd) {48return ::close(fd);49}5051/**52* Return the size of a regular file.53*/54jlong osSupport::size(const char *path) {55struct stat statbuf;56if (stat(path, &statbuf) < 0 ||57(statbuf.st_mode & S_IFREG) != S_IFREG) {58return -1;59}60return (jlong) statbuf.st_size;61}6263/**64* Read nBytes at offset into a buffer.65*/66jlong osSupport::read(jint fd, char *buf, jlong nBytes, jlong offset) {67OVERLAPPED ov;68DWORD nread;69BOOL result;7071ZeroMemory(&ov, sizeof (ov));72ov.Offset = (DWORD) offset;73ov.OffsetHigh = (DWORD) (offset >> 32);7475HANDLE h = (HANDLE)::_get_osfhandle(fd);7677result = ReadFile(h, (LPVOID) buf, (DWORD) nBytes, &nread, &ov);7879return result ? nread : 0;80}8182/**83* Map nBytes at offset into memory and return the address.84* The system chooses the address.85*/86void* osSupport::map_memory(jint fd, const char *file_name, size_t file_offset, size_t bytes) {87HANDLE hFile;88char* base = NULL;8990// Get a handle to the file91hFile = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,92OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);93if (hFile != NULL) {94// Create a file mapping handle95HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0,96NULL /* file_name */);97if (hMap != NULL) {98// Map the file into the address space at the offset99base = (char*) MapViewOfFileEx(hMap, FILE_MAP_READ, 0, (DWORD) file_offset,100(DWORD) bytes, NULL);101CloseHandle(hMap); // The mapping is no longer needed102}103CloseHandle(hFile); // The file handle is no longer needed104}105return base;106}107108/**109* Unmap nBytes of memory at address.110*/111int osSupport::unmap_memory(void* addr, size_t bytes) {112BOOL result = UnmapViewOfFile(addr);113return result;114}115116/**117* A CriticalSection to protect a small section of code.118*/119void SimpleCriticalSection::enter() {120EnterCriticalSection(&critical_section);121}122123void SimpleCriticalSection::exit() {124LeaveCriticalSection(&critical_section);125}126127SimpleCriticalSection::SimpleCriticalSection() {128InitializeCriticalSection(&critical_section);129}130131//SimpleCriticalSection::~SimpleCriticalSection() {132// DeleteCriticalSection(&critical_section);133//}134135136137