2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
20 import java.lang.reflect.Field;
23 * Unsafe = unwanted. See comments in the native peer. We only have it because
24 * it is required by java.util.concurrent.
26 * Note that in the real world, this class is only callable from the system library,
27 * not application code
31 private static final Unsafe theUnsafe = new Unsafe();
33 public static final int INVALID_FIELD_OFFSET = -1;
35 public static Unsafe getUnsafe() {
37 //return new Unsafe();
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);
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);
50 // that looks like some atomic conditional wait
51 public native void park (boolean isAbsolute, long timeout);
52 public native void unpark (Object thread);
55 public native int getInt(Object obj, long l);
56 public native int getIntVolatile(Object obj, long l);
59 public int getInt(Object obj, int offset) {
60 return getInt(obj, (long) offset);
63 public native void putInt(Object obj, long l, int i);
64 public native void putIntVolatile(Object obj, long l, int i);
67 public void putInt(Object obj, int offset, int i) {
68 putInt(obj, (long) offset, i);
71 public native void putOrderedInt(Object obj, long l, int i);
73 public native Object getObject(Object obj, long l);
74 public native Object getObjectVolatile(Object obj, long l);
77 public Object getObject(Object obj, int offset) {
78 return getObject(obj, (long) offset);
81 public native void putObject(Object obj, long l, Object obj1);
82 public native void putObjectVolatile(Object obj, long l, Object obj1);
86 public void putObject(Object obj, int offset, Object obj1) {
87 putObject(obj, (long) offset, obj1);
90 public native void putOrderedObject(Object obj, long l, Object obj1);
92 public native boolean getBoolean(Object obj, long l);
93 public native boolean getBooleanVolatile(Object obj, long l);
96 public boolean getBoolean(Object obj, int offset) {
97 return getBoolean(obj, (long) offset);
100 public native void putBoolean(Object obj, long l, boolean flag);
101 public native void putBooleanVolatile(Object obj, long l, boolean flag);
104 public void putBoolean(Object obj, int offset, boolean flag) {
105 putBoolean(obj, (long) offset, flag);
108 public native byte getByte(Object obj, long l);
109 public native byte getByteVolatile(Object obj, long l);
112 public byte getByte(Object obj, int offset) {
113 return getByte(obj, (long) offset);
116 public native void putByte(Object obj, long l, byte byte0);
117 public native void putByteVolatile(Object obj, long l, byte byte0);
120 public void putByte(Object obj, int offset, byte byte0) {
121 putByte(obj, (long) offset, byte0);
124 public native short getShort(Object obj, long l);
125 public native short getShortVolatile(Object obj, long l);
128 public short getShort(Object obj, int offset) {
129 return getShort(obj, (long) offset);
132 public native void putShort(Object obj, long l, short word0);
133 public native void putShortVolatile(Object obj, long l, short word0);
136 public void putShort(Object obj, int offset, short word0) {
137 putShort(obj, (long) offset, word0);
140 public native char getChar(Object obj, long l);
141 public native char getCharVolatile(Object obj, long l);
144 public char getChar(Object obj, int offset) {
145 return getChar(obj, (long) offset);
148 public native void putChar(Object obj, long l, char c);
149 public native void putCharVolatile(Object obj, long l, char c);
152 public void putChar(Object obj, int offset, char c) {
153 putChar(obj, (long) offset, c);
156 public native long getLong(Object obj, long l);
157 public native long getLongVolatile(Object obj, long l);
160 public long getLong(Object obj, int offset) {
161 return getLong(obj, (long) offset);
164 public native void putLong(Object obj, long l, long l1);
165 public native void putLongVolatile(Object obj, long l, long l1);
167 public native void putOrderedLong(Object obj, long l, long l1);
170 public void putLong(Object obj, int offset, long l1) {
171 putLong(obj, (long) offset, l1);
174 public native float getFloat(Object obj, long l);
175 public native float getFloatVolatile(Object obj, long l);
178 public float getFloat(Object obj, int offset) {
179 return getFloat(obj, (long) offset);
182 public native void putFloat(Object obj, long l, float f);
183 public native void putFloatVolatile(Object obj, long l, float f);
186 public void putFloat(Object obj, int offset, float f) {
187 putFloat(obj, (long) offset, f);
190 public native double getDouble(Object obj, long l);
191 public native double getDoubleVolatile(Object obj, long l);
194 public double getDouble(Object obj, int offset) {
195 return getDouble(obj, (long) offset);
198 public native void putDouble(Object obj, long l, double d);
199 public native void putDoubleVolatile(Object obj, long l, double d);
202 public void putDouble(Object obj, int offset, double d) {
203 putDouble(obj, (long) offset, d);
206 public native void ensureClassInitialized(Class<?> cls);
208 public native int arrayBaseOffset(Class<?> clazz);
210 public native int arrayIndexScale(Class<?> clazz);
213 //--- java.nio finally breaks object boundaries - hello, evil pointer arithmetic
216 * this is really a byte[] allocation (used by java.nio.Bits). Note that
217 * object has to be explicitly freed with freeMemory() (yikes!)
219 public native long allocateMemory (long bytes);
222 * to be used to free allocateMemory() allocated array objects
224 public native void freeMemory (long byteArrayRef);
227 * byte access of allocateMemory() objects. Note that 'address' has
228 * to point into such an object
230 public native byte getByte (long address);
231 public native void putByte (long address, byte val);
233 public native char getChar (long address);
234 public native void putChar (long address, char val);
236 public native int getInt (long address);
237 public native void putInt (long address, int val);
239 public native long getLong (long address);
240 public native void putLong (long address, long val);
242 public native float getFloat (long address);
243 public native void putFloat (long address, float val);
245 public native double getDouble (long address);
246 public native void putDouble (long address, double val);