Organized build script
[jpf-core.git] / src / classes / sun / misc / Unsafe.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package sun.misc;
19
20 import java.lang.reflect.Field;
21
22 /**
23  * Unsafe = unwanted. See comments in the native peer. We only have it because
24  * it is required by java.util.concurrent.
25  *
26  * Note that in the real world, this class is only callable from the system library,
27  * not application code
28  */
29
30 public class Unsafe {
31   private static final Unsafe theUnsafe = new Unsafe();
32
33   public static final int INVALID_FIELD_OFFSET = -1;
34
35   public static Unsafe getUnsafe() {
36     return theUnsafe;
37     //return new Unsafe();
38   }
39
40   // field offsets are completely useless between VMs, we just return
41   // a numeric id for the corresponding FieldInfo here
42   public native int fieldOffset (Field f);
43   public native long objectFieldOffset (Field f);
44
45   // those do the usual CAS magic
46   public native boolean compareAndSwapObject (Object oThis, long offset, Object expect, Object update);
47   public native boolean compareAndSwapInt (Object oThis, long offset, int expect, int update);
48   public native boolean compareAndSwapLong (Object oThis, long offset, long expect, long update);
49
50   // that looks like some atomic conditional wait
51   public native void park (boolean isAbsolute, long timeout);
52   public native void unpark (Object thread);
53
54   // various accessors
55   public native int getInt(Object obj, long l);
56   public native int getIntVolatile(Object obj, long l);
57
58   @Deprecated
59   public int getInt(Object obj, int offset) {
60     return getInt(obj, (long) offset);
61   }
62
63   public native void putInt(Object obj, long l, int i);
64   public native void putIntVolatile(Object obj, long l, int i);
65
66   @Deprecated
67   public void putInt(Object obj, int offset, int i) {
68     putInt(obj, (long) offset, i);
69   }
70
71   public native void putOrderedInt(Object obj, long l, int i);
72
73   public native Object getObject(Object obj, long l);
74   public native Object getObjectVolatile(Object obj, long l);
75
76   @Deprecated
77   public Object getObject(Object obj, int offset) {
78     return getObject(obj, (long) offset);
79   }
80
81   public native void putObject(Object obj, long l, Object obj1);
82   public native void putObjectVolatile(Object obj, long l, Object obj1);
83   
84
85   @Deprecated
86   public void putObject(Object obj, int offset, Object obj1) {
87     putObject(obj, (long) offset, obj1);
88   }
89
90   public native void putOrderedObject(Object obj, long l, Object obj1);
91
92   public native boolean getBoolean(Object obj, long l);
93   public native boolean getBooleanVolatile(Object obj, long l);
94
95   @Deprecated
96   public boolean getBoolean(Object obj, int offset) {
97     return getBoolean(obj, (long) offset);
98   }
99
100   public native void putBoolean(Object obj, long l, boolean flag);
101   public native void putBooleanVolatile(Object obj, long l, boolean flag);
102
103   @Deprecated
104   public void putBoolean(Object obj, int offset, boolean flag) {
105     putBoolean(obj, (long) offset, flag);
106   }
107
108   public native byte getByte(Object obj, long l);
109   public native byte getByteVolatile(Object obj, long l);
110
111   @Deprecated
112   public byte getByte(Object obj, int offset) {
113     return getByte(obj, (long) offset);
114   }
115
116   public native void putByte(Object obj, long l, byte byte0);
117   public native void putByteVolatile(Object obj, long l, byte byte0);
118
119   @Deprecated
120   public void putByte(Object obj, int offset, byte byte0) {
121     putByte(obj, (long) offset, byte0);
122   }
123
124   public native short getShort(Object obj, long l);
125   public native short getShortVolatile(Object obj, long l);
126
127   @Deprecated
128   public short getShort(Object obj, int offset) {
129     return getShort(obj, (long) offset);
130   }
131
132   public native void putShort(Object obj, long l, short word0);
133   public native void putShortVolatile(Object obj, long l, short word0);
134
135   @Deprecated
136   public void putShort(Object obj, int offset, short word0) {
137     putShort(obj, (long) offset, word0);
138   }
139
140   public native char getChar(Object obj, long l);
141   public native char getCharVolatile(Object obj, long l);
142
143   @Deprecated
144   public char getChar(Object obj, int offset) {
145     return getChar(obj, (long) offset);
146   }
147
148   public native void putChar(Object obj, long l, char c);
149   public native void putCharVolatile(Object obj, long l, char c);
150
151   @Deprecated
152   public void putChar(Object obj, int offset, char c) {
153     putChar(obj, (long) offset, c);
154   }
155
156   public native long getLong(Object obj, long l);
157   public native long getLongVolatile(Object obj, long l);
158
159   @Deprecated
160   public long getLong(Object obj, int offset) {
161     return getLong(obj, (long) offset);
162   }
163
164   public native void putLong(Object obj, long l, long l1);
165   public native void putLongVolatile(Object obj, long l, long l1);
166
167   public native void putOrderedLong(Object obj, long l, long l1);
168
169   @Deprecated
170   public void putLong(Object obj, int offset, long l1) {
171     putLong(obj, (long) offset, l1);
172   }
173
174   public native float getFloat(Object obj, long l);
175   public native float getFloatVolatile(Object obj, long l);
176
177   @Deprecated
178   public float getFloat(Object obj, int offset) {
179     return getFloat(obj, (long) offset);
180   }
181
182   public native void putFloat(Object obj, long l, float f);
183   public native void putFloatVolatile(Object obj, long l, float f);
184
185   @Deprecated
186   public void putFloat(Object obj, int offset, float f) {
187     putFloat(obj, (long) offset, f);
188   }
189
190   public native double getDouble(Object obj, long l);
191   public native double getDoubleVolatile(Object obj, long l);
192
193   @Deprecated
194   public double getDouble(Object obj, int offset) {
195     return getDouble(obj, (long) offset);
196   }
197
198   public native void putDouble(Object obj, long l, double d);
199   public native void putDoubleVolatile(Object obj, long l, double d);
200
201   @Deprecated
202   public void putDouble(Object obj, int offset, double d) {
203     putDouble(obj, (long) offset, d);
204   }
205
206   public native void ensureClassInitialized(Class<?> cls);
207
208   public native int arrayBaseOffset(Class<?> clazz);
209
210   public native int arrayIndexScale(Class<?> clazz);
211   
212   
213   //--- java.nio finally breaks object boundaries  - hello, evil pointer arithmetic
214   
215   /**
216    * this is really a byte[] allocation (used by java.nio.Bits). Note that
217    * object has to be explicitly freed with freeMemory() (yikes!)
218    */
219   public native long allocateMemory (long bytes);
220   
221   /**
222    * to be used to free allocateMemory() allocated array objects
223    */
224   public native void freeMemory (long byteArrayRef);
225     
226   /**
227    * byte access of allocateMemory() objects. Note that 'address' has
228    * to point into such an object
229    */
230   public native byte getByte (long address);
231   public native void putByte (long address, byte val);
232   
233   public native char getChar (long address);
234   public native void putChar (long address, char val);
235   
236   public native int getInt (long address);
237   public native void putInt (long address, int val);
238   
239   public native long getLong (long address);
240   public native void putLong (long address, long val);
241
242   public native float getFloat (long address);
243   public native void putFloat (long address, float val);
244
245   public native double getDouble (long address);
246   public native void putDouble (long address, double val);
247   
248 }