-
Notifications
You must be signed in to change notification settings - Fork 788
Expand file tree
/
Copy pathUnsafe.java
More file actions
7325 lines (6766 loc) · 272 KB
/
Copy pathUnsafe.java
File metadata and controls
7325 lines (6766 loc) · 272 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 9]*/
/*
* Copyright IBM Corp. and others 2017
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*/
package jdk.internal.misc;
import com.ibm.oti.vm.VM;
import com.ibm.oti.vm.VMLangAccess;
import java.lang.reflect.Field;
import java.security.ProtectionDomain;
import java.util.Objects;
/*[IF JAVA_SPEC_VERSION >= 12]*/
import java.nio.ByteBuffer;
import sun.nio.ch.DirectBuffer;
/*[IF JAVA_SPEC_VERSION >= 26]*/
import sun.nio.Cleaner;
/*[ELSE] JAVA_SPEC_VERSION >= 26 */
import jdk.internal.ref.Cleaner;
/*[ENDIF] JAVA_SPEC_VERSION >= 26 */
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
public final class Unsafe {
/* Prevents this class from being instantiated. */
private Unsafe() {}
/* unsafe instance */
private static final Unsafe theUnsafe;
/**
* Represents an invalid field offset value.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long INVALID_FIELD_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int INVALID_FIELD_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of byte array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_BYTE_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_BYTE_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of int array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_INT_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_INT_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of long array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_LONG_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_LONG_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of float array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_FLOAT_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_FLOAT_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of double array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_DOUBLE_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_DOUBLE_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of short array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_SHORT_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_SHORT_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of char array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_CHAR_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_CHAR_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of boolean array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_BOOLEAN_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_BOOLEAN_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Starting offset of Object array.
*/
/*[IF JAVA_SPEC_VERSION >= 25]*/
public static final long ARRAY_OBJECT_BASE_OFFSET;
/*[ELSE] JAVA_SPEC_VERSION >= 25 */
public static final int ARRAY_OBJECT_BASE_OFFSET;
/*[ENDIF] JAVA_SPEC_VERSION >= 25 */
/**
* Index size of byte array in bytes.
*/
public static final int ARRAY_BYTE_INDEX_SCALE;
/**
* Index size of int array in bytes.
*/
public static final int ARRAY_INT_INDEX_SCALE;
/**
* Index size of long array in bytes.
*/
public static final int ARRAY_LONG_INDEX_SCALE;
/**
* Index size of float array in bytes.
*/
public static final int ARRAY_FLOAT_INDEX_SCALE;
/**
* Index size of double array in bytes.
*/
public static final int ARRAY_DOUBLE_INDEX_SCALE;
/**
* Index size of short array in bytes.
*/
public static final int ARRAY_SHORT_INDEX_SCALE;
/**
* Index size of char array in bytes.
*/
public static final int ARRAY_CHAR_INDEX_SCALE;
/**
* Index size of boolean array in bytes.
*/
public static final int ARRAY_BOOLEAN_INDEX_SCALE;
/**
* Index size of Object array in bytes.
*/
public static final int ARRAY_OBJECT_INDEX_SCALE;
/**
* Size of address on machine in use.
*/
public static final int ADDRESS_SIZE;
/*
* internal helpers
*/
/* true if machine is big endian, false otherwise. */
private static final boolean IS_BIG_ENDIAN;
/* true if addresses are aligned in memory, false otherwise. */
private static final boolean UNALIGNED_ACCESS;
/* Size of int in bytes. */
private static final int BYTES_IN_INT = 4;
/* Number of bits in a short. */
private static final int BITS_IN_SHORT = 16;
/* Number of bits in a byte. */
private static final int BITS_IN_BYTE = 8;
/* 8 bit mask (long) */
private static final long BYTE_MASK = 0xFFL;
/* 32 bit mask (long) */
private static final long INT_MASK = 0xFFFFFFFFL;
/* 16 bit mask (long) */
private static final long SHORT_MASK = 0xFFFFL;
/* 8 bit mask (int) */
private static final int BYTE_MASK_INT = (int) BYTE_MASK;
/* 16 bit mask (int) */
private static final int SHORT_MASK_INT = (int) SHORT_MASK;
/* Mask aligns offset to be int addressable (all but bottom two bits). */
private static final int INT_OFFSET_ALIGN_MASK = 0xFFFFFFFC;
/*
* For int shift instructions (ishr, iushr, ishl), only the low 5 bits of the
* shift amount are considered.
*/
private static final int BC_SHIFT_INT_MASK = 0b11111;
/*
* For long shift instructions (lshr, lushr, lshl) only the low 6 bits of the
* shift amount are considered.
*/
private static final int BC_SHIFT_LONG_MASK = 0b111111;
/* Mask byte offset of an int. */
private static final long BYTE_OFFSET_MASK = 0b11L;
/*[IF INLINE-TYPES]*/
private static final class InlineTypesLock { InlineTypesLock() {} }
private static final InlineTypesLock inlineTypesLock = new InlineTypesLock();
/* Field layout constants */
private static final int FIELD_LAYOUT_FLATTENED = 1;
private static final int FIELD_LAYOUT_REFERENCE = 0;
/* Array Layout constants */
public static final int ARRAY_LAYOUT_FLATTENED = 1;
public static final int ARRAY_LAYOUT_REFERENCE = 0;
/*[ENDIF] INLINE-TYPES */
static {
registerNatives();
theUnsafe = new Unsafe();
INVALID_FIELD_OFFSET = -1;
/* All OpenJ9 array types have the same array base offset. To limit
* JNI calls arrayBaseOffset is only called once in this block and the
* value used for each array base offset variable.
*/
ARRAY_BYTE_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
ARRAY_INT_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_LONG_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_FLOAT_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_DOUBLE_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_SHORT_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_CHAR_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_BOOLEAN_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_OBJECT_BASE_OFFSET = ARRAY_BYTE_BASE_OFFSET;
ARRAY_BYTE_INDEX_SCALE = theUnsafe.arrayIndexScale(byte[].class);
ARRAY_INT_INDEX_SCALE = theUnsafe.arrayIndexScale(int[].class);
ARRAY_LONG_INDEX_SCALE = theUnsafe.arrayIndexScale(long[].class);
ARRAY_FLOAT_INDEX_SCALE = theUnsafe.arrayIndexScale(float[].class);
ARRAY_DOUBLE_INDEX_SCALE = theUnsafe.arrayIndexScale(double[].class);
ARRAY_SHORT_INDEX_SCALE = theUnsafe.arrayIndexScale(short[].class);
ARRAY_CHAR_INDEX_SCALE = theUnsafe.arrayIndexScale(char[].class);
ARRAY_BOOLEAN_INDEX_SCALE = theUnsafe.arrayIndexScale(boolean[].class);
ARRAY_OBJECT_INDEX_SCALE = theUnsafe.arrayIndexScale(Object[].class);
ADDRESS_SIZE = VM.ADDRESS_SIZE;
IS_BIG_ENDIAN = VM.IS_BIG_ENDIAN;
/* Unaligned access is currently supported on all platforms */
UNALIGNED_ACCESS = true;
}
/* Attach jdk.internal.misc.Unsafe natives. */
private static native void registerNatives();
/**
* Gets the value of the byte in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return byte value stored in obj
*/
public native byte getByte(Object obj, long offset);
/**
* Sets the value of the byte in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value byte to store in obj
*/
public native void putByte(Object obj, long offset, byte value);
/**
* Gets the value of the int in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return int value stored in obj
*/
public native int getInt(Object obj, long offset);
/**
* Sets the value of the int in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value int to store in obj
*/
public native void putInt(Object obj, long offset, int value);
/**
* Gets the value of the long in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return long value stored in obj
*/
public native long getLong(Object obj, long offset);
/**
* Sets the value of the long in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value long to store in obj
*/
public native void putLong(Object obj, long offset, long value);
/**
* Gets the value of the float in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return float value stored in obj
*/
public native float getFloat(Object obj, long offset);
/**
* Sets the value of the float in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value float to store in obj
*/
public native void putFloat(Object obj, long offset, float value);
/**
* Gets the value of the double in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return double value stored in obj
*/
public native double getDouble(Object obj, long offset);
/**
* Sets the value of the double in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value double to store in obj
*/
public native void putDouble(Object obj, long offset, double value);
/**
* Gets the value of the short in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return short value stored in obj
*/
public native short getShort(Object obj, long offset);
/**
* Sets the value of the short in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value short to store in obj
*/
public native void putShort(Object obj, long offset, short value);
/**
* Gets the value of the char in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return char value stored in obj
*/
public native char getChar(Object obj, long offset);
/**
* Sets the value of the char in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value char to store in obj
*/
public native void putChar(Object obj, long offset, char value);
/**
* Gets the value of the boolean in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return boolean value stored in obj
*/
public native boolean getBoolean(Object obj, long offset);
/**
* Sets the value of the boolean in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value boolean to store in obj
*/
public native void putBoolean(Object obj, long offset, boolean value);
/*[IF JAVA_SPEC_VERSION < 23]*/
/**
* Gets the value of the Object in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return Object value stored in obj
*/
public native Object getObject(Object obj, long offset);
/**
* Sets the value of the Object in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value Object to store in obj
*/
public native void putObject(Object obj, long offset, Object value);
/*[ENDIF] JAVA_SPEC_VERSION < 23 */
/*[IF JAVA_SPEC_VERSION >= 12]*/
/**
* Gets the value of the Object in the obj parameter referenced by offset.
* This is a non-volatile operation.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return Object value stored in obj
*/
public native Object getReference(Object obj, long offset);
/**
* Sets the value of the Object in the obj parameter at memory offset.
* This is a non-volatile operation.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value Object to store in obj
*/
public native void putReference(Object obj, long offset, Object value);
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
/**
* Gets the value of the Object in memory referenced by address.
* This is a non-volatile operation.
*
* NOTE: native implementation is a stub.
*
* @param address position of the object in memory
* @return Object value stored in memory
*/
public native Object getUncompressedObject(long address);
/**
* Allocates a block of memory
*
* @param size number of bytes to allocate
* @return address of allocated buffer
*
* @throws IllegalArgumentException if size is negative
*/
public native long allocateDBBMemory(long size);
/**
* Reallocates a block of memory.
*
* @param address of old buffer
* @param size new size of buffer to allocate
* @return address of new buffer
*
* @throws IllegalArgumentException if size is negative
*/
public native long reallocateDBBMemory(long address, long size);
/**
* Removes the block from the list. Note that the pointers are located
* immediately before the address value.
*
* @param address of buffer
*/
public native void freeDBBMemory(long address);
/**
* Returns the size of a page in memory in bytes.
*
* @return size of memory page
*/
public native int pageSize();
/**
* Creates a class out of a given array of bytes with a ProtectionDomain.
*
* @param name binary name of the class, null if the name is not known
* @param b a byte array of the class data. The bytes should have the format of a valid
* class file as defined by The JVM Spec
* @param offset offset of the start of the class data in b
* @param bLength length of the class data
* @param cl used to load the class being built. If null, the default
* system ClassLoader will be used
* @param pd ProtectionDomain for new class
* @return the Class created from the byte array and ProtectionDomain parameters
*
* @throws IndexOutOfBoundsException if offset or bLength is negative, or if offset + bLength
* is greater than the length of b
*/
public native Class<?> defineClass0(String name, byte[] b, int offset, int bLength, ClassLoader cl,
ProtectionDomain pd);
/**
* Allocate instance of class parameter.
*
* @param c class to allocate
* @return instance of class c
*
* @throws InstantiationException if class c cannot be instantiated
*/
public native Object allocateInstance(Class<?> c) throws InstantiationException;
/**
* Throw Throwable parameter.
*
* @param t Throwable to execute
*
* @throws NullPointerException if Throwable is null
*/
public native void throwException(Throwable t);
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param setValue value that will be set in obj at offset if the comparison is successful
* @return boolean value indicating whether the field was updated
*/
public final native boolean compareAndSetInt(Object obj, long offset, int compareValue, int setValue);
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param exchangeValue value that will be set in obj at offset if the comparison is successful
* @return value in obj at offset before this operation. This will be compareValue if the exchange was successful
*/
public final native int compareAndExchangeInt(Object obj, long offset, int compareValue, int exchangeValue);
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param setValue value that will be set in obj at offset if the comparison is successful
* @return boolean value indicating whether the field was updated
*/
public final native boolean compareAndSetLong(Object obj, long offset, long compareValue, long setValue);
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param exchangeValue value that will be set in obj at offset if the comparison is successful
* @return value in obj at offset before this operation. This will be compareValue if the exchange was successful
*/
public final native long compareAndExchangeLong(Object obj, long offset, long compareValue, long exchangeValue);
/*[IF JAVA_SPEC_VERSION < 23]*/
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param setValue value that will be set in obj at offset if the comparison is successful
* @return boolean value indicating whether the field was updated
*/
public final native boolean compareAndSetObject(Object obj, long offset, Object compareValue, Object setValue);
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param exchangeValue value that will be set in obj at offset if the comparison is successful
* @return value in obj at offset before this operation. This will be compareValue if the exchange was successful
*/
public final native Object compareAndExchangeObject(Object obj, long offset, Object compareValue,
Object exchangeValue);
/*[ENDIF] JAVA_SPEC_VERSION < 23 */
/*[IF JAVA_SPEC_VERSION >= 12]*/
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param setValue value that will be set in obj at offset if the comparison is successful
* @return boolean value indicating whether the field was updated
*/
public final native boolean compareAndSetReference(Object obj, long offset, Object compareValue, Object setValue);
/**
* Atomically sets the parameter value at offset in obj if the compare value
* matches the existing value in the object.
* The get operation has memory semantics of getVolatile.
* The set operation has the memory semantics of setVolatile.
*
* @param obj object into which to store the value
* @param offset location to compare and store value in obj
* @param compareValue value that is expected to be in obj at offset
* @param exchangeValue value that will be set in obj at offset if the comparison is successful
* @return value in obj at offset before this operation. This will be compareValue if the exchange was successful
*/
public final native Object compareAndExchangeReference(Object obj, long offset, Object compareValue,
Object exchangeValue);
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
/**
* Atomically gets the value of the byte in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return byte value stored in obj
*/
public native byte getByteVolatile(Object obj, long offset);
/**
* Atomically sets the value of the byte in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value byte to store in obj
*/
public native void putByteVolatile(Object obj, long offset, byte value);
/**
* Atomically gets the value of the int in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return int value stored in obj
*/
public native int getIntVolatile(Object obj, long offset);
/**
* Atomically sets the value of the int in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value int to store in obj
*/
public native void putIntVolatile(Object obj, long offset, int value);
/**
* Atomically gets the value of the long in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return long value stored in obj
*/
public native long getLongVolatile(Object obj, long offset);
/**
* Atomically sets the value of the long in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value long to store in obj
*/
public native void putLongVolatile(Object obj, long offset, long value);
/**
* Atomically gets the value of the float in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return float value stored in obj
*/
public native float getFloatVolatile(Object obj, long offset);
/**
* Atomically sets the value of the float in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value float to store in obj
*/
public native void putFloatVolatile(Object obj, long offset, float value);
/**
* Atomically gets the value of the double in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return double value stored in obj
*/
public native double getDoubleVolatile(Object obj, long offset);
/**
* Atomically sets the value of the double in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value double to store in obj
*/
public native void putDoubleVolatile(Object obj, long offset, double value);
/**
* Atomically gets the value of the short in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return short value stored in obj
*/
public native short getShortVolatile(Object obj, long offset);
/**
* Atomically sets the value of the short in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value short to store in obj
*/
public native void putShortVolatile(Object obj, long offset, short value);
/**
* Atomically gets the value of the char in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return char value stored in obj
*/
public native char getCharVolatile(Object obj, long offset);
/**
* Atomically sets the value of the char in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value char to store in obj
*/
public native void putCharVolatile(Object obj, long offset, char value);
/**
* Atomically gets the value of the boolean in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return boolean value stored in obj
*/
public native boolean getBooleanVolatile(Object obj, long offset);
/**
* Atomically sets the value of the boolean in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value boolean to store in obj
*/
public native void putBooleanVolatile(Object obj, long offset, boolean value);
/*[IF JAVA_SPEC_VERSION < 23]*/
/**
* Atomically gets the value of the Object in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return Object value stored in obj
*/
public native Object getObjectVolatile(Object obj, long offset);
/**
* Atomically sets the value of the Object in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value Object to store in obj
*/
public native void putObjectVolatile(Object obj, long offset, Object value);
/*[ENDIF] JAVA_SPEC_VERSION < 23 */
/*[IF JAVA_SPEC_VERSION >= 12]*/
/**
* Atomically gets the value of the Object in the obj parameter referenced by offset.
*
* @param obj object from which to retrieve the value
* @param offset position of the value in obj
* @return Object value stored in obj
*/
public native Object getReferenceVolatile(Object obj, long offset);
/**
* Atomically sets the value of the Object in the obj parameter at memory offset.
*
* @param obj object into which to store the value
* @param offset position of the value in obj
* @param value Object to store in obj
*/
public native void putReferenceVolatile(Object obj, long offset, Object value);
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
/**
* Makes permit available for thread parameter.
*
* @param thread the thread to unpark. If null, method has no effect
*/
public native void unpark(Object thread);
/**
* Disables current thread unless permit is available. Thread will not be
* scheduled until unpark provides permit.
*
* @param isAbsolute if true park timeout should be absolute
* @param time parks for time in ns.
* ms is expected if isAbsolute is set to true.
*/
public native void park(boolean isAbsolute, long time);
/**
* Inserts an acquire memory fence, ensuring that no loads before this fence
* are reordered with any loads/stores after the fence.
*/
public native void loadFence();
/**
* Inserts a release memory fence, ensuring that no stores before this fence
* are reordered with any loads/stores after the fence.
*/
public native void storeFence();
/**
* Inserts a complete memory fence, ensuring that no loads/stores before this
* fence are reordered with any loads/stores after the fence.
*/
public native void fullFence();
/*
* jdk.internal.misc.Unsafe natives
*/
/*
* Allocates a block of memory.
* If size passed is 0, no memory will be allocated.
*
* @param size requested size of memory in bytes
* @return starting address of memory
*
* @throws IllegalArgumentException if size is not valid
*/
private native long allocateMemory0(long size);
/*
* Reallocates a block of memory.
* If size passed is 0, no memory will be allocated.
*
* @param address starting address of memory block
* @param size requested size of memory in bytes
* @return starting address of memory
*
* @throws IllegalArgumentException if size is not valid
*/
private native long reallocateMemory0(long address, long size);
/*
* Removes the block from the list. Note that the pointers are located
* immediately before the startIndex value.
*
* @param startIndex memory address
*
* @throws IllegalArgumentException if startIndex is not valid
*/
private native void freeMemory0(long startIndex);
/*
* Set object at offset in obj parameter.
*
* @param obj object into which to store the value
* @param startIndex position of the value in obj
* @param size the number of bytes to be set to the value
* @param replace overwrite memory with this value
*
* @throws IllegalArgumentException if startIndex is illegal in obj, or if size is invalid
*/
private native void setMemory0(Object obj, long startIndex, long size, byte replace);
/*
* Copy bytes from source object to destination.
*
* @param srcObj object to copy from
* @param srcOffset location in srcObj to start copy
* @param destObj object to copy into
* @param destOffset location in destObj to start copy
* @param size the number of bytes to be copied
*
* @throws IllegalArgumentException if srcOffset is illegal in srcObj,
* if destOffset is illegal in destObj, or if size is invalid
*/
private native void copyMemory0(Object srcObj, long srcOffset, Object destObj, long destOffset, long size);
/*
* Copy bytes from source object to destination in reverse order.
* Memory is reversed in elementSize chunks.
*
* @param srcObj object to copy from
* @param srcOffset location in srcObj to start copy
* @param destObj object to copy into
* @param destOffset location in destObj to start copy
* @param copySize the number of bytes to be copied, a multiple of elementSize
* @param elementSize the size in bytes of elements that will be reversed
*
* @throws IllegalArgumentException if srcOffset is illegal in srcObj,
* if destOffset is illegal in destObj, if copySize is invalid or copySize is not
* a multiple of elementSize
*/
private native void copySwapMemory0(Object srcObj, long srcOffset, Object destObj, long destOffset, long copySize,
long elementSize);
/*
* Returns byte offset to field.
*
* @param field which contains desired class or interface
* @return offset to start of class or interface
*
* @throws IllegalArgumentException if field is static
*/