1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 *   - Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 *
11 *   - Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 *
15 *   - Neither the name of Oracle nor the names of its
16 *     contributors may be used to endorse or promote products derived
17 *     from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef LIBJIMAGE_OSSUPPORT_HPP
33#define LIBJIMAGE_OSSUPPORT_HPP
34
35#ifdef WIN32
36#include <Windows.h>
37#else
38#include <pthread.h>
39#endif
40
41class osSupport {
42public:
43    /**
44     * Open a regular file read-only.
45     * Return the file descriptor.
46     */
47    static jint openReadOnly(const char *path);
48
49    /**
50     * Close a file descriptor.
51     */
52    static jint close(jint fd);
53
54    /**
55     * Return the size of a regular file.
56     */
57    static jlong size(const char *path);
58
59    /**
60     * Read nBytes at offset into a buffer.
61     */
62    static jlong read(jint fd, char *buf, jlong nBytes, jlong offset);
63
64    /**
65     * Map nBytes at offset into memory and return the address.
66     * The system chooses the address.
67     */
68    static void* map_memory(jint fd, const char *filename, size_t file_offset, size_t bytes);
69
70    /**
71     * Unmap nBytes of memory at address.
72     */
73    static int unmap_memory(void* addr, size_t bytes);
74};
75
76/**
77 * A CriticalSection to protect a small section of code.
78 */
79class SimpleCriticalSection {
80    friend class SimpleCriticalSectionLock;
81private:
82    void enter();
83    void exit();
84public:
85    SimpleCriticalSection();
86    //~SimpleCriticalSection(); // Cretes a dependency on Solaris on a C++ exit registration
87
88private:
89#ifdef WIN32
90    CRITICAL_SECTION critical_section;
91#else
92    pthread_mutex_t mutex;
93#endif // WIN32
94};
95
96/**
97 * SimpleCriticalSectionLock instance.
98 * The constructor locks a SimpleCriticalSection and the
99 * destructor does the unlock.
100 */
101class SimpleCriticalSectionLock {
102private:
103    SimpleCriticalSection *lock;
104public:
105
106    SimpleCriticalSectionLock(SimpleCriticalSection *cslock) {
107        this->lock = cslock;
108        lock->enter();
109    }
110
111    ~SimpleCriticalSectionLock() {
112        lock->exit();
113    }
114};
115
116#endif  // LIBJIMAGE_OSSUPPORT_HPP
117