src/java.base/windows/native/libjimage/osSupport_windows.cpp
changeset 47216 71c04702a3d5
parent 32757 79d34d4b9627
child 49703 d2bea17edd96
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2015, 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     return ::open(path, 0, 0);
       
    42 }
       
    43 
       
    44 /**
       
    45  * Close a file descriptor.
       
    46  */
       
    47 jint osSupport::close(jint fd) {
       
    48     return ::close(fd);
       
    49 }
       
    50 
       
    51 /**
       
    52  * Return the size of a regular file.
       
    53  */
       
    54 jlong osSupport::size(const char *path) {
       
    55     struct stat statbuf;
       
    56     if (stat(path, &statbuf) < 0 ||
       
    57             (statbuf.st_mode & S_IFREG) != S_IFREG) {
       
    58         return -1;
       
    59     }
       
    60     return (jlong) statbuf.st_size;
       
    61 }
       
    62 
       
    63 /**
       
    64  * Read nBytes at offset into a buffer.
       
    65  */
       
    66 jlong osSupport::read(jint fd, char *buf, jlong nBytes, jlong offset) {
       
    67     OVERLAPPED ov;
       
    68     DWORD nread;
       
    69     BOOL result;
       
    70 
       
    71     ZeroMemory(&ov, sizeof (ov));
       
    72     ov.Offset = (DWORD) offset;
       
    73     ov.OffsetHigh = (DWORD) (offset >> 32);
       
    74 
       
    75     HANDLE h = (HANDLE)::_get_osfhandle(fd);
       
    76 
       
    77     result = ReadFile(h, (LPVOID) buf, (DWORD) nBytes, &nread, &ov);
       
    78 
       
    79     return result ? nread : 0;
       
    80 }
       
    81 
       
    82 /**
       
    83  * Map nBytes at offset into memory and return the address.
       
    84  * The system chooses the address.
       
    85  */
       
    86 void* osSupport::map_memory(jint fd, const char *file_name, size_t file_offset, size_t bytes) {
       
    87     HANDLE hFile;
       
    88     char* base = NULL;
       
    89 
       
    90     // Get a handle to the file
       
    91     hFile = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
       
    92             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
       
    93     if (hFile != NULL) {
       
    94         // Create a file mapping handle
       
    95         HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0,
       
    96                 NULL /* file_name */);
       
    97         if (hMap != NULL) {
       
    98             // Map the file into the address space at the offset
       
    99             base = (char*) MapViewOfFileEx(hMap, FILE_MAP_READ, 0, (DWORD) file_offset,
       
   100                     (DWORD) bytes, NULL);
       
   101             CloseHandle(hMap); // The mapping is no longer needed
       
   102         }
       
   103         CloseHandle(hFile); // The file handle is no longer needed
       
   104     }
       
   105     return base;
       
   106 }
       
   107 
       
   108 /**
       
   109  * Unmap nBytes of memory at address.
       
   110  */
       
   111 int osSupport::unmap_memory(void* addr, size_t bytes) {
       
   112     BOOL result = UnmapViewOfFile(addr);
       
   113     return result;
       
   114 }
       
   115 
       
   116 /**
       
   117  * A CriticalSection to protect a small section of code.
       
   118  */
       
   119 void SimpleCriticalSection::enter() {
       
   120     EnterCriticalSection(&critical_section);
       
   121 }
       
   122 
       
   123 void SimpleCriticalSection::exit() {
       
   124     LeaveCriticalSection(&critical_section);
       
   125 }
       
   126 
       
   127 SimpleCriticalSection::SimpleCriticalSection() {
       
   128     InitializeCriticalSection(&critical_section);
       
   129 }
       
   130 
       
   131 //SimpleCriticalSection::~SimpleCriticalSection() {
       
   132 //    DeleteCriticalSection(&critical_section);
       
   133 //}
       
   134