aboutsummaryrefslogtreecommitdiff
path: root/bindings/java/src/java/com/galois/ppaml/tracer/CWrapper.java
blob: 5a7887ae3f84863b3b677df67c27a2fc0e41b992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* CWrapper -- wrapping C structures
 * Copyright (C) 2014  Galois, Inc.
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact Galois, complete the Web form at
 * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest
 * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */

package com.galois.ppaml.tracer;

/**
 * A class which wraps a C structure and associated functions.
 *
 * <p> This class is intended to work hand-in-hand with Java Native Interface
 * code that follows a four-stage lifecycle:
 *
 * <dl>
 *   <dt>Allocation</dt><dd>The system reserves memory and resources needed by
 *     the structure.</dd>
 *   <dt>Initialization</dt><dd>The system installs data into the structure to
 *     make it ready for use.</dd>
 *   <dt>Finalization</dt><dd>The system cleans up the data structure under the
 *     assumption that it will not be used anymore.</dd>
 *   <dt>Freeing</dt><dd>The system releases memory and resources needed by the
 *     structure.</dd>
 * </dl>
 *
 * This class defines abstract methods associated with each of these phases; by
 * defining contracts on them, it can then consolidate the phases into two
 * concretely-implemented operations:
 *
 * <dl>
 *   <dt>Open</dt><dd>The system allocates and initializes the underlying
 *     structure.</dd>
 *   <dt>Close</dt><dd>The system finalizes and frees the underlying
 *     structure.</dd>
 * </dl>
 *
 * <p> <b>This class is not intended to be instantiated directly.  Only
 * instantiate its concrete subclasses.
 */
abstract class CWrapper {

    /**
     * C pointer to the underlying data structure.
     */
    protected long cPointer;


    /////////////////////////// Low-level life cycle ///////////////////////////

    /**
     * Reserves memory and resources needed by the structure.
     *
     * <p> <b>Precondition:</b> <code>cPointer</code> is <code>0</code>.
     *
     * <p> <b>Postcondition:</b> If allocation was successful,
     * <code>cPointer</code> is not <code>0</code>.
     */
    abstract protected void allocateCPointer();

    /**
     * Installs data into the structure to make it ready for use.
     *
     * <p> <b>Precondition:</b> Allocation succeeded.
     */
    abstract protected void initializeCPointer();

    /**
     * Cleans up the structure under the assumption that it will not be used
     * anymore.
     *
     * <p> <b>Precondition:</b> Initialization succeeded.
     */
    abstract protected void finalizeCPointer();

    /**
     * Releases memory and resources needed by the structure.
     *
     * <p> <b>Precondition:</b> <code>cPointer</code> is not <code>0</code>. <br>
     * <b>Precondition:</b> Finalization succeeded.
     *
     * <p> <b>Postcondition:</b> <code>cPointer</code> is <code>0</code>.
     */
    abstract protected void freeCPointer();


    ////////////////////////// High-level life cycle  //////////////////////////

    /**
     * Allocates and initializes the underlying structure.
     *
     * @throws OpenedException the underlying structure has already been opened.
     *
     * @throws NativeOutOfMemoryError allocation failed.
     */
    protected synchronized void open() {
        if (cPointer != 0) {
            throw new OpenedException();
        }
        allocateCPointer();
        if (cPointer == 0) {
            throw new NativeOutOfMemoryError();
        }
        initializeCPointer();
    }

    /**
     * Finalizes and frees the underlying structure.
     *
     * @throws ClosedException the underlying structure has already been closed.
     */
    public synchronized void close() {
        if (cPointer == 0) {
            throw new ClosedException();
        }
        finalizeCPointer();
        freeCPointer();
        cPointer = 0;
    }


    ////////////////////////// Associated exceptions  //////////////////////////

    /**
     * Signals that the underlying structure is already open.
     */
    public class OpenedException extends java.lang.IllegalStateException {

        static final long serialVersionUID = -7226578680647930527L;

        /**
         * Constructs a <code>CWrapper.OpenedException</code> with no detail
         * message.
         */
        protected OpenedException() {
            super();
        }

    }

    /**
     * Signals that the underlying structure is already closed.
     */
    public class ClosedException extends java.lang.IllegalStateException {

        static final long serialVersionUID = -2471059099211958977L;

        /**
         * Constructs a <code>CWrapper.ClosedException</code> with no detail
         * message.
         */
        protected ClosedException() {
            super();
        }

    }


    ////////////////////////////// Miscellaneous  //////////////////////////////

    @Override
    protected void finalize() {
        close();
    }

    @Override
    protected Object clone() throws java.lang.CloneNotSupportedException {
        throw new java.lang.CloneNotSupportedException();
    }

}