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 package jdk.internal.misc;
27
28 import java.lang.reflect.Field;
29 import java.security.ProtectionDomain;
30
31 import jdk.internal.reflect.CallerSensitive;
32 import jdk.internal.reflect.Reflection;
33 import jdk.internal.misc.VM;
34
35 import jdk.internal.HotSpotIntrinsicCandidate;
36
37
38 /**
39 * A collection of methods for performing low-level, unsafe operations.
40 * Although the class and all methods are public, use of this class is
41 * limited because only trusted code can obtain instances of it.
42 *
43 * <em>Note:</em> It is the resposibility of the caller to make sure
44 * arguments are checked before methods of this class are
45 * called. While some rudimentary checks are performed on the input,
46 * the checks are best effort and when performance is an overriding
47 * priority, as when methods of this class are optimized by the
48 * runtime compiler, some or all checks (if any) may be elided. Hence,
49 * the caller must not rely on the checks and corresponding
50 * exceptions!
51 *
52 * @author John R. Rose
53 * @see #getUnsafe
54 */
55
192 */
193 @HotSpotIntrinsicCandidate
194 public native Object getObject(Object o, long offset);
195
196 /**
197 * Stores a reference value into a given Java variable.
198 * <p>
199 * Unless the reference {@code x} being stored is either null
200 * or matches the field type, the results are undefined.
201 * If the reference {@code o} is non-null, card marks or
202 * other store barriers for that object (if the VM requires them)
203 * are updated.
204 * @see #putInt(Object, long, int)
205 */
206 @HotSpotIntrinsicCandidate
207 public native void putObject(Object o, long offset, Object x);
208
209 /** @see #getInt(Object, long) */
210 @HotSpotIntrinsicCandidate
211 public native boolean getBoolean(Object o, long offset);
212 /** @see #putInt(Object, long, int) */
213 @HotSpotIntrinsicCandidate
214 public native void putBoolean(Object o, long offset, boolean x);
215 /** @see #getInt(Object, long) */
216 @HotSpotIntrinsicCandidate
217 public native byte getByte(Object o, long offset);
218 /** @see #putInt(Object, long, int) */
219 @HotSpotIntrinsicCandidate
220 public native void putByte(Object o, long offset, byte x);
221 /** @see #getInt(Object, long) */
222 @HotSpotIntrinsicCandidate
223 public native short getShort(Object o, long offset);
224 /** @see #putInt(Object, long, int) */
225 @HotSpotIntrinsicCandidate
226 public native void putShort(Object o, long offset, short x);
227 /** @see #getInt(Object, long) */
228 @HotSpotIntrinsicCandidate
229 public native char getChar(Object o, long offset);
230 /** @see #putInt(Object, long, int) */
231 @HotSpotIntrinsicCandidate
232 public native void putChar(Object o, long offset, char x);
233 /** @see #getInt(Object, long) */
234 @HotSpotIntrinsicCandidate
235 public native long getLong(Object o, long offset);
236 /** @see #putInt(Object, long, int) */
237 @HotSpotIntrinsicCandidate
238 public native void putLong(Object o, long offset, long x);
239 /** @see #getInt(Object, long) */
240 @HotSpotIntrinsicCandidate
241 public native float getFloat(Object o, long offset);
242 /** @see #putInt(Object, long, int) */
243 @HotSpotIntrinsicCandidate
244 public native void putFloat(Object o, long offset, float x);
245 /** @see #getInt(Object, long) */
246 @HotSpotIntrinsicCandidate
247 public native double getDouble(Object o, long offset);
248 /** @see #putInt(Object, long, int) */
249 @HotSpotIntrinsicCandidate
250 public native void putDouble(Object o, long offset, double x);
251
252 // These read VM internal data.
253
254 /**
255 * Fetches an uncompressed reference value from a given native variable
256 * ignoring the VM's compressed references mode.
257 *
258 * @param address a memory address locating the variable
259 * @return the value fetched from the indicated native variable
260 */
261 public native Object getUncompressedObject(long address);
262
263 /**
264 * Fetches the {@link java.lang.Class} Java mirror for the given native
265 * metaspace {@code Klass} pointer.
266 *
267 * @param metaspaceKlass a native metaspace {@code Klass} pointer
268 * @return the {@link java.lang.Class} Java mirror
269 */
270 public native Class<?> getJavaMirror(long metaspaceKlass);
271
272 /**
273 * Fetches a native metaspace {@code Klass} pointer for the given Java
274 * object.
275 *
276 * @param o Java heap object for which to fetch the class pointer
277 * @return a native metaspace {@code Klass} pointer
278 */
279 public native long getKlassPointer(Object o);
280
281 // These work on values in the C heap.
282
283 /**
284 * Fetches a value from a given memory address. If the address is zero, or
285 * does not point into a block obtained from {@link #allocateMemory}, the
286 * results are undefined.
287 *
288 * @see #allocateMemory
289 */
290 @HotSpotIntrinsicCandidate
291 public native byte getByte(long address);
292
293 /**
294 * Stores a value into a given memory address. If the address is zero, or
295 * does not point into a block obtained from {@link #allocateMemory}, the
296 * results are undefined.
297 *
298 * @see #getByte(long)
299 */
300 @HotSpotIntrinsicCandidate
301 public native void putByte(long address, byte x);
302
303 /** @see #getByte(long) */
304 @HotSpotIntrinsicCandidate
305 public native short getShort(long address);
306 /** @see #putByte(long, byte) */
307 @HotSpotIntrinsicCandidate
308 public native void putShort(long address, short x);
309 /** @see #getByte(long) */
310 @HotSpotIntrinsicCandidate
311 public native char getChar(long address);
312 /** @see #putByte(long, byte) */
313 @HotSpotIntrinsicCandidate
314 public native void putChar(long address, char x);
315 /** @see #getByte(long) */
316 @HotSpotIntrinsicCandidate
317 public native int getInt(long address);
318 /** @see #putByte(long, byte) */
319 @HotSpotIntrinsicCandidate
320 public native void putInt(long address, int x);
321 /** @see #getByte(long) */
322 @HotSpotIntrinsicCandidate
323 public native long getLong(long address);
324 /** @see #putByte(long, byte) */
325 @HotSpotIntrinsicCandidate
326 public native void putLong(long address, long x);
327 /** @see #getByte(long) */
328 @HotSpotIntrinsicCandidate
329 public native float getFloat(long address);
330 /** @see #putByte(long, byte) */
331 @HotSpotIntrinsicCandidate
332 public native void putFloat(long address, float x);
333 /** @see #getByte(long) */
334 @HotSpotIntrinsicCandidate
335 public native double getDouble(long address);
336 /** @see #putByte(long, byte) */
337 @HotSpotIntrinsicCandidate
338 public native void putDouble(long address, double x);
339
340 /**
341 * Fetches a native pointer from a given memory address. If the address is
342 * zero, or does not point into a block obtained from {@link
343 * #allocateMemory}, the results are undefined.
344 *
345 * <p>If the native pointer is less than 64 bits wide, it is extended as
346 * an unsigned number to a Java long. The pointer may be indexed by any
347 * given byte offset, simply by adding that offset (as a simple integer) to
348 * the long representing the pointer. The number of bytes actually read
349 * from the target address may be determined by consulting {@link
350 * #addressSize}.
351 *
352 * @see #allocateMemory
353 */
354 @HotSpotIntrinsicCandidate
355 public native long getAddress(long address);
356
357 /**
358 * Stores a native pointer into a given memory address. If the address is
359 * zero, or does not point into a block obtained from {@link
360 * #allocateMemory}, the results are undefined.
361 *
362 * <p>The number of bytes actually written at the target address may be
363 * determined by consulting {@link #addressSize}.
364 *
365 * @see #getAddress(long)
366 */
367 @HotSpotIntrinsicCandidate
368 public native void putAddress(long address, long x);
369
370
371
372 /// helper methods for validating various types of objects/values
373
374 /**
375 * Create an exception reflecting that some of the input was invalid
376 *
377 * <em>Note:</em> It is the resposibility of the caller to make
378 * sure arguments are checked before the methods are called. While
379 * some rudimentary checks are performed on the input, the checks
380 * are best effort and when performance is an overriding priority,
381 * as when methods of this class are optimized by the runtime
382 * compiler, some or all checks (if any) may be elided. Hence, the
383 * caller must not rely on the checks and corresponding
384 * exceptions!
385 *
386 * @return an exception object
387 */
388 private RuntimeException invalidInput() {
|
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 package jdk.internal.misc;
27
28 import java.lang.reflect.Field;
29 import java.security.ProtectionDomain;
30
31 import jdk.internal.reflect.CallerSensitive;
32 import jdk.internal.reflect.Reflection;
33 import jdk.internal.misc.VM;
34
35 import jdk.internal.HotSpotIntrinsicCandidate;
36 import jdk.internal.vm.annotation.ForceInline;
37
38
39 /**
40 * A collection of methods for performing low-level, unsafe operations.
41 * Although the class and all methods are public, use of this class is
42 * limited because only trusted code can obtain instances of it.
43 *
44 * <em>Note:</em> It is the resposibility of the caller to make sure
45 * arguments are checked before methods of this class are
46 * called. While some rudimentary checks are performed on the input,
47 * the checks are best effort and when performance is an overriding
48 * priority, as when methods of this class are optimized by the
49 * runtime compiler, some or all checks (if any) may be elided. Hence,
50 * the caller must not rely on the checks and corresponding
51 * exceptions!
52 *
53 * @author John R. Rose
54 * @see #getUnsafe
55 */
56
193 */
194 @HotSpotIntrinsicCandidate
195 public native Object getObject(Object o, long offset);
196
197 /**
198 * Stores a reference value into a given Java variable.
199 * <p>
200 * Unless the reference {@code x} being stored is either null
201 * or matches the field type, the results are undefined.
202 * If the reference {@code o} is non-null, card marks or
203 * other store barriers for that object (if the VM requires them)
204 * are updated.
205 * @see #putInt(Object, long, int)
206 */
207 @HotSpotIntrinsicCandidate
208 public native void putObject(Object o, long offset, Object x);
209
210 /** @see #getInt(Object, long) */
211 @HotSpotIntrinsicCandidate
212 public native boolean getBoolean(Object o, long offset);
213
214 /** @see #putInt(Object, long, int) */
215 @HotSpotIntrinsicCandidate
216 public native void putBoolean(Object o, long offset, boolean x);
217
218 /** @see #getInt(Object, long) */
219 @HotSpotIntrinsicCandidate
220 public native byte getByte(Object o, long offset);
221
222 /** @see #putInt(Object, long, int) */
223 @HotSpotIntrinsicCandidate
224 public native void putByte(Object o, long offset, byte x);
225
226 /** @see #getInt(Object, long) */
227 @HotSpotIntrinsicCandidate
228 public native short getShort(Object o, long offset);
229
230 /** @see #putInt(Object, long, int) */
231 @HotSpotIntrinsicCandidate
232 public native void putShort(Object o, long offset, short x);
233
234 /** @see #getInt(Object, long) */
235 @HotSpotIntrinsicCandidate
236 public native char getChar(Object o, long offset);
237
238 /** @see #putInt(Object, long, int) */
239 @HotSpotIntrinsicCandidate
240 public native void putChar(Object o, long offset, char x);
241
242 /** @see #getInt(Object, long) */
243 @HotSpotIntrinsicCandidate
244 public native long getLong(Object o, long offset);
245
246 /** @see #putInt(Object, long, int) */
247 @HotSpotIntrinsicCandidate
248 public native void putLong(Object o, long offset, long x);
249
250 /** @see #getInt(Object, long) */
251 @HotSpotIntrinsicCandidate
252 public native float getFloat(Object o, long offset);
253
254 /** @see #putInt(Object, long, int) */
255 @HotSpotIntrinsicCandidate
256 public native void putFloat(Object o, long offset, float x);
257
258 /** @see #getInt(Object, long) */
259 @HotSpotIntrinsicCandidate
260 public native double getDouble(Object o, long offset);
261
262 /** @see #putInt(Object, long, int) */
263 @HotSpotIntrinsicCandidate
264 public native void putDouble(Object o, long offset, double x);
265
266 /** @see #getInt(Object, long) */
267 @ForceInline
268 public long getAddress(Object o, long offset) {
269 if (ADDRESS_SIZE == 4) {
270 return Integer.toUnsignedLong(getInt(o, offset));
271 } else {
272 return getLong(o, offset);
273 }
274 }
275
276 /** @see #putInt(Object, long, int) */
277 @ForceInline
278 public void putAddress(Object o, long offset, long x) {
279 if (ADDRESS_SIZE == 4) {
280 putInt(o, offset, (int)x);
281 } else {
282 putLong(o, offset, x);
283 }
284 }
285
286 // These read VM internal data.
287
288 /**
289 * Fetches an uncompressed reference value from a given native variable
290 * ignoring the VM's compressed references mode.
291 *
292 * @param address a memory address locating the variable
293 * @return the value fetched from the indicated native variable
294 */
295 public native Object getUncompressedObject(long address);
296
297 /**
298 * Fetches the {@link java.lang.Class} Java mirror for the given native
299 * metaspace {@code Klass} pointer.
300 *
301 * @param metaspaceKlass a native metaspace {@code Klass} pointer
302 * @return the {@link java.lang.Class} Java mirror
303 */
304 public native Class<?> getJavaMirror(long metaspaceKlass);
305
306 /**
307 * Fetches a native metaspace {@code Klass} pointer for the given Java
308 * object.
309 *
310 * @param o Java heap object for which to fetch the class pointer
311 * @return a native metaspace {@code Klass} pointer
312 */
313 public native long getKlassPointer(Object o);
314
315 // These work on values in the C heap.
316
317 /**
318 * Fetches a value from a given memory address. If the address is zero, or
319 * does not point into a block obtained from {@link #allocateMemory}, the
320 * results are undefined.
321 *
322 * @see #allocateMemory
323 */
324 @ForceInline
325 public byte getByte(long address) {
326 return getByte(null, address);
327 }
328
329 /**
330 * Stores a value into a given memory address. If the address is zero, or
331 * does not point into a block obtained from {@link #allocateMemory}, the
332 * results are undefined.
333 *
334 * @see #getByte(long)
335 */
336 @ForceInline
337 public void putByte(long address, byte x) {
338 putByte(null, address, x);
339 }
340
341 /** @see #getByte(long) */
342 @ForceInline
343 public short getShort(long address) {
344 return getShort(null, address);
345 }
346
347 /** @see #putByte(long, byte) */
348 @ForceInline
349 public void putShort(long address, short x) {
350 putShort(null, address, x);
351 }
352
353 /** @see #getByte(long) */
354 @ForceInline
355 public char getChar(long address) {
356 return getChar(null, address);
357 }
358
359 /** @see #putByte(long, byte) */
360 @ForceInline
361 public void putChar(long address, char x) {
362 putChar(null, address, x);
363 }
364
365 /** @see #getByte(long) */
366 @ForceInline
367 public int getInt(long address) {
368 return getInt(null, address);
369 }
370
371 /** @see #putByte(long, byte) */
372 @ForceInline
373 public void putInt(long address, int x) {
374 putInt(null, address, x);
375 }
376
377 /** @see #getByte(long) */
378 @ForceInline
379 public long getLong(long address) {
380 return getLong(null, address);
381 }
382
383 /** @see #putByte(long, byte) */
384 @ForceInline
385 public void putLong(long address, long x) {
386 putLong(null, address, x);
387 }
388
389 /** @see #getByte(long) */
390 @ForceInline
391 public float getFloat(long address) {
392 return getFloat(null, address);
393 }
394
395 /** @see #putByte(long, byte) */
396 @ForceInline
397 public void putFloat(long address, float x) {
398 putFloat(null, address, x);
399 }
400
401 /** @see #getByte(long) */
402 @ForceInline
403 public double getDouble(long address) {
404 return getDouble(null, address);
405 }
406
407 /** @see #putByte(long, byte) */
408 @ForceInline
409 public void putDouble(long address, double x) {
410 putDouble(null, address, x);
411 }
412
413 /**
414 * Fetches a native pointer from a given memory address. If the address is
415 * zero, or does not point into a block obtained from {@link
416 * #allocateMemory}, the results are undefined.
417 *
418 * <p>If the native pointer is less than 64 bits wide, it is extended as
419 * an unsigned number to a Java long. The pointer may be indexed by any
420 * given byte offset, simply by adding that offset (as a simple integer) to
421 * the long representing the pointer. The number of bytes actually read
422 * from the target address may be determined by consulting {@link
423 * #addressSize}.
424 *
425 * @see #allocateMemory
426 */
427 @ForceInline
428 public long getAddress(long address) {
429 return getAddress(null, address);
430 }
431
432 /**
433 * Stores a native pointer into a given memory address. If the address is
434 * zero, or does not point into a block obtained from {@link
435 * #allocateMemory}, the results are undefined.
436 *
437 * <p>The number of bytes actually written at the target address may be
438 * determined by consulting {@link #addressSize}.
439 *
440 * @see #getAddress(long)
441 */
442 @ForceInline
443 public void putAddress(long address, long x) {
444 putAddress(null, address, x);
445 }
446
447
448
449 /// helper methods for validating various types of objects/values
450
451 /**
452 * Create an exception reflecting that some of the input was invalid
453 *
454 * <em>Note:</em> It is the resposibility of the caller to make
455 * sure arguments are checked before the methods are called. While
456 * some rudimentary checks are performed on the input, the checks
457 * are best effort and when performance is an overriding priority,
458 * as when methods of this class are optimized by the runtime
459 * compiler, some or all checks (if any) may be elided. Hence, the
460 * caller must not rely on the checks and corresponding
461 * exceptions!
462 *
463 * @return an exception object
464 */
465 private RuntimeException invalidInput() {
|