Path: blob/master/src/java.base/unix/native/libjimage/osSupport_unix.cpp
41119 views
/*1* Copyright (c) 2015, 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 <sys/types.h>26#include <sys/stat.h>27#include <sys/mman.h>28#include <fcntl.h>29#include <stdlib.h>30#include <unistd.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) {40return ::open(path, 0);41}4243/**44* Close a file descriptor.45*/46jint osSupport::close(jint fd) {47return ::close(fd);48}4950/**51* Return the size of a regular file.52*/53jlong osSupport::size(const char *path) {54struct stat statbuf;55if (stat(path, &statbuf) < 0 ||56(statbuf.st_mode & S_IFREG) != S_IFREG) {57return -1;58}59return (jsize) statbuf.st_size;60}6162/**63* Read nBytes at offset into a buffer.64*/65jlong osSupport::read(jint fd, char *buf, jlong nBytes, jlong offset) {66return ::pread(fd, buf, nBytes, offset);67}6869/**70* Map nBytes at offset into memory and return the address.71* The system chooses the address.72*/73void* osSupport::map_memory(int fd, const char *filename, size_t file_offset, size_t bytes) {74void* mapped_address = NULL;75mapped_address = (void*) mmap(NULL,76bytes, PROT_READ, MAP_SHARED,77fd, file_offset);78if (mapped_address == MAP_FAILED) {79return NULL;80}81return mapped_address;82}8384/**85* Unmap nBytes of memory at address.86*/87int osSupport::unmap_memory(void *addr, size_t bytes) {88return munmap((char *) addr, bytes) == 0;89}9091/**92* A CriticalSection to protect a small section of code.93*/94void SimpleCriticalSection::enter() {95pthread_mutex_lock(&mutex);96}9798void SimpleCriticalSection::exit() {99pthread_mutex_unlock(&mutex);100101}102103SimpleCriticalSection::SimpleCriticalSection() {104pthread_mutex_init(&mutex, NULL);105}106107//SimpleCriticalSection::~SimpleCriticalSection() {108// pthread_mutex_destroy(&mutex);109//}110111112113