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 <sys/types.h>
27#include <sys/stat.h>
28#include <sys/mman.h>
29#include <fcntl.h>
30#include <stdlib.h>
31#include <unistd.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 */
40jint osSupport::openReadOnly(const char *path) {
41 return ::open(path, 0);
42}
43
44/**
45 * Close a file descriptor.
46 */
47jint osSupport::close(jint fd) {
48 return ::close(fd);
49}
50
51/**
52 * Return the size of a regular file.
53 */
54jlong 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 (jsize) statbuf.st_size;
61}
62
63/**
64 * Read nBytes at offset into a buffer.
65 */
66jlong osSupport::read(jint fd, char *buf, jlong nBytes, jlong offset) {
67 return ::pread(fd, buf, nBytes, offset);
68}
69
70/**
71 * Map nBytes at offset into memory and return the address.
72 * The system chooses the address.
73 */
74void* osSupport::map_memory(int fd, const char *filename, size_t file_offset, size_t bytes) {
75 void* mapped_address = NULL;
76 mapped_address = (void*) mmap(NULL,
77 bytes, PROT_READ, MAP_SHARED,
78 fd, file_offset);
79 if (mapped_address == MAP_FAILED) {
80 return NULL;
81 }
82 return mapped_address;
83}
84
85/**
86 * Unmap nBytes of memory at address.
87 */
88int osSupport::unmap_memory(void *addr, size_t bytes) {
89 return munmap((char *) addr, bytes) == 0;
90}
91
92/**
93 * A CriticalSection to protect a small section of code.
94 */
95void SimpleCriticalSection::enter() {
96 pthread_mutex_lock(&mutex);
97}
98
99void SimpleCriticalSection::exit() {
100 pthread_mutex_unlock(&mutex);
101
102}
103
104SimpleCriticalSection::SimpleCriticalSection() {
105 pthread_mutex_init(&mutex, NULL);
106}
107
108//SimpleCriticalSection::~SimpleCriticalSection() {
109// pthread_mutex_destroy(&mutex);
110//}
111
112