-
Notifications
You must be signed in to change notification settings - Fork 668
/
Unsafe.java
1603 lines (1405 loc) · 69.2 KB
/
Unsafe.java
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
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.misc;
import jdk.internal.misc.VM;
import jdk.internal.ref.Cleaner;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.vm.annotation.ForceInline;
import sun.nio.ch.DirectBuffer;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
/**
* A collection of methods for performing low-level, unsafe operations.
* Although the class and all methods are public, use of this class is
* limited because only trusted code can obtain instances of it.
*
* <em>Note:</em> It is the resposibility of the caller to make sure
* arguments are checked before methods of this class are
* called. While some rudimentary checks are performed on the input,
* the checks are best effort and when performance is an overriding
* priority, as when methods of this class are optimized by the
* runtime compiler, some or all checks (if any) may be elided. Hence,
* the caller must not rely on the checks and corresponding
* exceptions!
*
* @author John R. Rose
* @see #getUnsafe
*/
/*
* 用于执行低级别,不安全操作的方法集合。
*
* 该类包装了jdk.internal.misc.Unsafe类中的部分操作
* 虽然类和所有方法都是公共的,但是使用这个类是有限的,因为只有可信代码才能获得它的实例。
*
* 该类支持在任意内存地址位置处读写数据,对于普通用户来说,使用起来还是比较危险的。
*
* 常用的场景:
* --> 创建某个类的对象(不经过构造方法)
* --> 本地内存操作:分配/释放内存,向内存存值,从内存中取值
* --> 获取对象中某字段的地址,获取该字段存储的值(通过地址),为某地址处的字段赋值(可支持Volatile语义)
* --> 对JVM内存中某对象的数组字段/变量直接操作
* --> 原子操作(CAS),设置/更新/增减值
* --> 线程操作
* --> 内存屏障
*/
public final class Unsafe {
// 可通过反射获得此静态域
private static final Unsafe theUnsafe = new Unsafe();
// 另一个内部同名Unsafe类。此类中的方法实际上是委托给内部的theInternalUnsafe实例去完成的
private static final jdk.internal.misc.Unsafe theInternalUnsafe = jdk.internal.misc.Unsafe.getUnsafe();
/**
* This constant differs from all results that will ever be returned from {@link #staticFieldOffset}, {@link #objectFieldOffset}, or {@link #arrayBaseOffset}.
*/
// 无效的JVM内存偏移量标记
public static final int INVALID_FIELD_OFFSET = jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET;
/** The value of {@code addressSize()} */
// 本地指针尺寸,4字节或8字节
public static final int ADDRESS_SIZE = theInternalUnsafe.addressSize();
/* 用于 #arrayBaseOffset 的值*/
/** The value of {@code arrayBaseOffset(boolean[].class)} */
public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(byte[].class)} */
public static final int ARRAY_BYTE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(short[].class)} */
public static final int ARRAY_SHORT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_SHORT_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(char[].class)} */
public static final int ARRAY_CHAR_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_CHAR_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(int[].class)} */
public static final int ARRAY_INT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_INT_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(long[].class)} */
public static final int ARRAY_LONG_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_LONG_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(float[].class)} */
public static final int ARRAY_FLOAT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_FLOAT_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(double[].class)} */
public static final int ARRAY_DOUBLE_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
/** The value of {@code arrayBaseOffset(Object[].class)} */
public static final int ARRAY_OBJECT_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET;
/* 用于 #arrayIndexScale 的值*/
/** The value of {@code arrayIndexScale(boolean[].class)} */
public static final int ARRAY_BOOLEAN_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
/** The value of {@code arrayIndexScale(byte[].class)} */
public static final int ARRAY_BYTE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE;
/** The value of {@code arrayIndexScale(short[].class)} */
public static final int ARRAY_SHORT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
/** The value of {@code arrayIndexScale(char[].class)} */
public static final int ARRAY_CHAR_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_CHAR_INDEX_SCALE;
/** The value of {@code arrayIndexScale(int[].class)} */
public static final int ARRAY_INT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
/** The value of {@code arrayIndexScale(long[].class)} */
public static final int ARRAY_LONG_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
/** The value of {@code arrayIndexScale(float[].class)} */
public static final int ARRAY_FLOAT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE;
/** The value of {@code arrayIndexScale(double[].class)} */
public static final int ARRAY_DOUBLE_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
/** The value of {@code arrayIndexScale(Object[].class)} */
public static final int ARRAY_OBJECT_INDEX_SCALE = jdk.internal.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE;
static {
Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
}
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
private Unsafe() {
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* Provides the caller with the capability of performing unsafe
* operations.
*
* <p>The returned {@code Unsafe} object should be carefully guarded
* by the caller, since it can be used to read and write data at arbitrary
* memory addresses. It must never be passed to untrusted code.
*
* <p>Most methods in this class are very low-level, and correspond to a
* small number of hardware instructions (on typical machines). Compilers
* are encouraged to optimize these methods accordingly.
*
* <p>Here is a suggested idiom for using unsafe operations:
*
* <pre> {@code
* class MyTrustedClass {
* private static final Unsafe unsafe = Unsafe.getUnsafe();
* ...
* private long myCountAddress = ...;
* public int getCount() { return unsafe.getByte(myCountAddress); }
* }}</pre>
*
* (It may assist compilers to make the local variable {@code final}.)
*
* @throws SecurityException if the class loader of the caller
* class is not in the system domain in which all permissions
* are granted.
*/
// 返回单例对象,只能从引导类加载器(bootstrap class loader)加载,被自定义类直接调用会抛出异常
@CallerSensitive
public static Unsafe getUnsafe() {
// 获取getUnsafe()调用者所处的类
Class<?> caller = Reflection.getCallerClass();
// 校验ClassLoader,从自己编写的类中调用此方法会抛出异常
if (!VM.isSystemDomainLoader(caller.getClassLoader())) {
throw new SecurityException("Unsafe");
}
return theUnsafe;
}
/*▼ 构造Unsafe对象 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Allocates an instance but does not run any constructor.
* Initializes the class if it has not yet been.
*/
// 不调用构造方法就生成对象,但是该对象的字段会被赋为对应类型的"零值",为该对象赋过的默认值也无效
@ForceInline
public Object allocateInstance(Class<?> cls) throws InstantiationException {
return theInternalUnsafe.allocateInstance(cls);
}
/**
* Defines a class but does not make it known to the class loader or system dictionary.
* <p>
* For each CP entry, the corresponding CP patch must either be null or have
* the a format that matches its tag:
* <ul>
* <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
* <li>Utf8: a string (must have suitable syntax if used as signature or name)
* <li>Class: any java.lang.Class object
* <li>String: any object (not just a java.lang.String)
* <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
* </ul>
* @param hostClass context for linkage, access control, protection domain, and class loader
* @param data bytes of a class file
* @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
*/
// 定义(创建)一个虚拟机匿名类,该类不会被类加载器或系统目录发现
@ForceInline
public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
return theInternalUnsafe.defineAnonymousClass(hostClass, data, cpPatches);
}
/*▲ 构造Unsafe对象 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 杂项 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Reports the location of a given field in the storage allocation of its
* class. Do not expect to perform any sort of arithmetic on this offset;
* it is just a cookie which is passed to the unsafe heap memory accessors.
*
* <p>Any given field will always have the same offset and base, and no
* two distinct fields of the same class will ever have the same offset
* and base.
*
* <p>As of 1.4.1, offsets for fields are represented as long values,
* although the Sun JVM does not use the most significant 32 bits.
* However, JVM implementations which store static fields at absolute
* addresses can use long offsets and null base pointers to express
* the field locations in a form usable by {@link #getInt(Object, long)}.
* Therefore, code which will be ported to such JVMs on 64-bit platforms
* must preserve all bits of static field offsets.
*
* @see #getInt(Object, long)
*/
/*
* 获取非静态字段的JVM偏移地址
*
* 通过该方法可以计算一个对象在内存中的空间大小,方法是:
* 通过反射得到它的所有Field(包括父类继承得到的),找出Field中偏移量最大值,然后对该最大偏移值填充字节数即为对象大小。
*
* 关于该方法的使用例子可以看下面的修改内存数据的例子;
* putLong,putInt,putDouble,putChar,putObject等方法,直接修改内存数据(可以越过访问权限)
*
* 这里,还有put对应的get方法,很简单就是直接读取内存地址处的数据。
*/
@ForceInline
public long objectFieldOffset(Field f) {
return theInternalUnsafe.objectFieldOffset(f);
}
/**
* Reports the location of a given static field, in conjunction with {@link
* #staticFieldBase}.
* <p>Do not expect to perform any sort of arithmetic on this offset;
* it is just a cookie which is passed to the unsafe heap memory accessors.
*
* <p>Any given field will always have the same offset, and no two distinct
* fields of the same class will ever have the same offset.
*
* <p>As of 1.4.1, offsets for fields are represented as long values,
* although the Sun JVM does not use the most significant 32 bits.
* It is hard to imagine a JVM technology which needs more than
* a few bits to encode an offset within a non-array object,
* However, for consistency with other methods in this class,
* this method reports its result as a long value.
*
* @see #getInt(Object, long)
*/
// 获取静态字段的JVM偏移地址
@ForceInline
public long staticFieldOffset(Field f) {
return theInternalUnsafe.staticFieldOffset(f);
}
/**
* Reports the location of a given static field, in conjunction with {@link
* #staticFieldOffset}.
* <p>Fetch the base "Object", if any, with which static fields of the
* given class can be accessed via methods like {@link #getInt(Object,
* long)}. This value may be null. This value may refer to an object
* which is a "cookie", not guaranteed to be a real Object, and it should
* not be used in any way except as argument to the get and put routines in
* this class.
*/
// 获取静态字段所属的类对象
@ForceInline
public Object staticFieldBase(Field f) {
return theInternalUnsafe.staticFieldBase(f);
}
/**
* Detects if the given class may need to be initialized.
* This is often needed in conjunction with obtaining the static field base of a class.
*
* @return false only if a call to {@code ensureClassInitialized} would have no effect
*/
@ForceInline
public boolean shouldBeInitialized(Class<?> c) {
return theInternalUnsafe.shouldBeInitialized(c);
}
/**
* Ensures the given class has been initialized.
* This is often needed in conjunction with obtaining the static field base of a class.
*/
@ForceInline
public void ensureClassInitialized(Class<?> c) {
theInternalUnsafe.ensureClassInitialized(c);
}
/*▲ 杂项 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 本地内存操作 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Allocates a new block of native memory, of the given size in bytes. The
* contents of the memory are uninitialized; they will generally be
* garbage. The resulting native pointer will never be zero, and will be
* aligned for all value types. Dispose of this memory by calling {@link
* #freeMemory}, or resize it with {@link #reallocateMemory}.
*
* <em>Note:</em> It is the resposibility of the caller to make
* sure arguments are checked before the methods are called. While
* some rudimentary checks are performed on the input, the checks
* are best effort and when performance is an overriding priority,
* as when methods of this class are optimized by the runtime
* compiler, some or all checks (if any) may be elided. Hence, the
* caller must not rely on the checks and corresponding
* exceptions!
*
* @throws RuntimeException if the size is negative or too large
* for the native size_t type
*
* @throws OutOfMemoryError if the allocation is refused by the system
*
* @see #getByte(long)
* @see #putByte(long, byte)
*/
// 申请bytes字节的本地内存,并返回分配的内存地址
@ForceInline
public long allocateMemory(long bytes) {
return theInternalUnsafe.allocateMemory(bytes);
}
/**
* Resizes a new block of native memory, to the given size in bytes. The
* contents of the new block past the size of the old block are
* uninitialized; they will generally be garbage. The resulting native
* pointer will be zero if and only if the requested size is zero. The
* resulting native pointer will be aligned for all value types. Dispose
* of this memory by calling {@link #freeMemory}, or resize it with {@link
* #reallocateMemory}. The address passed to this method may be null, in
* which case an allocation will be performed.
*
* <em>Note:</em> It is the resposibility of the caller to make
* sure arguments are checked before the methods are called. While
* some rudimentary checks are performed on the input, the checks
* are best effort and when performance is an overriding priority,
* as when methods of this class are optimized by the runtime
* compiler, some or all checks (if any) may be elided. Hence, the
* caller must not rely on the checks and corresponding
* exceptions!
*
* @throws RuntimeException if the size is negative or too large
* for the native size_t type
*
* @throws OutOfMemoryError if the allocation is refused by the system
*
* @see #allocateMemory
*/
// 在地址address的基础上扩容,如果address为0,则效果与#allocateMemory一致
@ForceInline
public long reallocateMemory(long address, long bytes) {
return theInternalUnsafe.reallocateMemory(address, bytes);
}
/**
* Sets all bytes in a given block of memory to a fixed value (usually zero).
* This provides a <em>single-register</em> addressing mode, as discussed in {@link #getInt(Object,long)}.
*
* <p>Equivalent to {@code setMemory(null, address, bytes, value)}.
*/
// 为申请的内存批量填充初值,通常用0填充
@ForceInline
public void setMemory(long address, long bytes, byte value) {
theInternalUnsafe.setMemory(address, bytes, value);
}
/**
* Sets all bytes in a given block of memory to a fixed value
* (usually zero).
*
* <p>This method determines a block's base address by means of two parameters,
* and so it provides (in effect) a <em>double-register</em> addressing mode,
* as discussed in {@link #getInt(Object,long)}. When the object reference is null,
* the offset supplies an absolute base address.
*
* <p>The stores are in coherent (atomic) units of a size determined
* by the address and length parameters. If the effective address and
* length are all even modulo 8, the stores take place in 'long' units.
* If the effective address and length are (resp.) even modulo 4 or 2,
* the stores take place in units of 'int' or 'short'.
*
* <em>Note:</em> It is the resposibility of the caller to make
* sure arguments are checked before the methods are called. While
* some rudimentary checks are performed on the input, the checks
* are best effort and when performance is an overriding priority,
* as when methods of this class are optimized by the runtime
* compiler, some or all checks (if any) may be elided. Hence, the
* caller must not rely on the checks and corresponding
* exceptions!
*
* @throws RuntimeException if any of the arguments is invalid
*
* @since 1.7
*/
// 为对象o处的内存批量填充初值,通常用0填充
@ForceInline
public void setMemory(Object o, long offset, long bytes, byte value) {
theInternalUnsafe.setMemory(o, offset, bytes, value);
}
/**
* Sets all bytes in a given block of memory to a copy of another
* block. This provides a <em>single-register</em> addressing mode,
* as discussed in {@link #getInt(Object,long)}.
*
* Equivalent to {@code copyMemory(null, srcAddress, null, destAddress, bytes)}.
*/
// 内存数据拷贝
@ForceInline
public void copyMemory(long srcAddress, long destAddress, long bytes) {
theInternalUnsafe.copyMemory(srcAddress, destAddress, bytes);
}
/**
* Sets all bytes in a given block of memory to a copy of another
* block.
*
* <p>This method determines each block's base address by means of two parameters,
* and so it provides (in effect) a <em>double-register</em> addressing mode,
* as discussed in {@link #getInt(Object, long)}. When the object reference is null,
* the offset supplies an absolute base address.
*
* <p>The transfers are in coherent (atomic) units of a size determined
* by the address and length parameters. If the effective addresses and
* length are all even modulo 8, the transfer takes place in 'long' units.
* If the effective addresses and length are (resp.) even modulo 4 or 2,
* the transfer takes place in units of 'int' or 'short'.
*
* <em>Note:</em> It is the resposibility of the caller to make
* sure arguments are checked before the methods are called. While
* some rudimentary checks are performed on the input, the checks
* are best effort and when performance is an overriding priority,
* as when methods of this class are optimized by the runtime
* compiler, some or all checks (if any) may be elided. Hence, the
* caller must not rely on the checks and corresponding
* exceptions!
*
* @throws RuntimeException if any of the arguments is invalid
* @since 1.7
*/
// 内存数据拷贝
@ForceInline
public void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes) {
theInternalUnsafe.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes);
}
/**
* Disposes of a block of native memory, as obtained from {@link
* #allocateMemory} or {@link #reallocateMemory}. The address passed to
* this method may be null, in which case no action is taken.
*
* <em>Note:</em> It is the resposibility of the caller to make
* sure arguments are checked before the methods are called. While
* some rudimentary checks are performed on the input, the checks
* are best effort and when performance is an overriding priority,
* as when methods of this class are optimized by the runtime
* compiler, some or all checks (if any) may be elided. Hence, the
* caller must not rely on the checks and corresponding
* exceptions!
*
* @throws RuntimeException if any of the arguments is invalid
*
* @see #allocateMemory
*/
// 用于释放allocateMemory和reallocateMemory申请的内存
@ForceInline
public void freeMemory(long address) {
theInternalUnsafe.freeMemory(address);
}
/**
* Fetches a native pointer from a given memory address. If the address is
* zero, or does not point into a block obtained from {@link
* #allocateMemory}, the results are undefined.
*
* <p>If the native pointer is less than 64 bits wide, it is extended as
* an unsigned number to a Java long. The pointer may be indexed by any
* given byte offset, simply by adding that offset (as a simple integer) to
* the long representing the pointer. The number of bytes actually read
* from the target address may be determined by consulting {@link
* #addressSize}.
*
* @see #allocateMemory
*/
// 从本地内存地址address处获取一个本地指针值
@ForceInline
public long getAddress(long address) {
return theInternalUnsafe.getAddress(address);
}
/**
* Stores a native pointer into a given memory address. If the address is
* zero, or does not point into a block obtained from {@link
* #allocateMemory}, the results are undefined.
*
* <p>The number of bytes actually written at the target address may be
* determined by consulting {@link #addressSize}.
*
* @see #getAddress(long)
*/
// 向本地内存地址address处存入一个本地指针值x
@ForceInline
public void putAddress(long address, long x) {
theInternalUnsafe.putAddress(address, x);
}
/**
* Reports the size in bytes of a native pointer, as stored via {@link
* #putAddress}. This value will be either 4 or 8. Note that the sizes of
* other primitive types (as stored in native memory blocks) is determined
* fully by their information content.
*/
// 检查通过{@link #putAddress}存储的本机指针的大小(以字节为单位)。此值为4或8。请注意,其他基本类型的大小(存储在本机内存块中)完全由其信息内容决定。
@ForceInline
public int addressSize() {
return theInternalUnsafe.addressSize();
}
/*▲ 本地内存操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/* 获取/设置字段值 ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ */
/*
* (1.1) 基于JVM内存地址,获取/设置字段值
* - getXXX(o, offset)
* 获取对象o中offset地址处对应的字段值
* 对象o可以是数组
* offset的值由#objectFieldOffset或#staticFieldOffset获取
* 也可以由#arrayBaseOffset[B]和#arrayIndexScale[S]共同构成:B + N * S
*
* - putXXX(o, offset, x)
* 设置对象o中offset地址处对应的字段为新值x
*
* (1.2) 基于本地内存地址,获取/设置字段值
* - getXXX(address)
* - putXXX(address, x)
*
* (3) 基于JVM内存地址,获取/设置字段值,Ordered/Lazy版本
* 不保证值的改变被其他线程立即看到。
* - putOrderedXXX(o, offset, x)
*
* (4) 基于JVM内存地址,获取/设置字段值,Volatile版本
* - getXXXVolatile(o, offset)
* - putXXXVolatile(o, offset, x)
*/
/*▼ (1.1) getXXX/putXXX 获取/设置字段值(基于JVM内存地址) ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的byte型字段的值
@ForceInline
public byte getByte(Object o, long offset) {
return theInternalUnsafe.getByte(o, offset);
}
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的short型字段的值
@ForceInline
public short getShort(Object o, long offset) {
return theInternalUnsafe.getShort(o, offset);
}
/**
* Fetches a value from a given Java variable.
* More specifically, fetches a field or array element within the given
* object {@code o} at the given offset, or (if {@code o} is null)
* from the memory address whose numerical value is the given offset.
* <p>
* The results are undefined unless one of the following cases is true:
* <ul>
* <li>The offset was obtained from {@link #objectFieldOffset} on
* the {@link java.lang.reflect.Field} of some Java field and the object
* referred to by {@code o} is of a class compatible with that
* field's class.
*
* <li>The offset and object reference {@code o} (either null or
* non-null) were both obtained via {@link #staticFieldOffset}
* and {@link #staticFieldBase} (respectively) from the
* reflective {@link Field} representation of some Java field.
*
* <li>The object referred to by {@code o} is an array, and the offset
* is an integer of the form {@code B+N*S}, where {@code N} is
* a valid index into the array, and {@code B} and {@code S} are
* the values obtained by {@link #arrayBaseOffset} and {@link
* #arrayIndexScale} (respectively) from the array's class. The value
* referred to is the {@code N}<em>th</em> element of the array.
*
* </ul>
* <p>
* If one of the above cases is true, the call references a specific Java
* variable (field or array element). However, the results are undefined
* if that variable is not in fact of the type returned by this method.
* <p>
* This method refers to a variable by means of two parameters, and so
* it provides (in effect) a <em>double-register</em> addressing mode
* for Java variables. When the object reference is null, this method
* uses its offset as an absolute address. This is similar in operation
* to methods such as {@link #getInt(long)}, which provide (in effect) a
* <em>single-register</em> addressing mode for non-Java variables.
* However, because Java variables may have a different layout in memory
* from non-Java variables, programmers should not assume that these
* two addressing modes are ever equivalent. Also, programmers should
* remember that offsets from the double-register addressing mode cannot
* be portably confused with longs used in the single-register addressing
* mode.
*
* @param o Java heap object in which the variable resides, if any, else
* null
* @param offset indication of where the variable resides in a Java heap
* object, if any, else a memory address locating the variable
* statically
*
* @return the value fetched from the indicated Java variable
*
* @throws RuntimeException No defined exceptions are thrown, not even
* {@link NullPointerException}
*/
// 获取对象o中offset地址处对应的int型字段的值
@ForceInline
public int getInt(Object o, long offset) {
return theInternalUnsafe.getInt(o, offset);
}
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的long型字段的值
@ForceInline
public long getLong(Object o, long offset) {
return theInternalUnsafe.getLong(o, offset);
}
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的float型字段的值
@ForceInline
public float getFloat(Object o, long offset) {
return theInternalUnsafe.getFloat(o, offset);
}
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的double型字段的值
@ForceInline
public double getDouble(Object o, long offset) {
return theInternalUnsafe.getDouble(o, offset);
}
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的char型字段的值
@ForceInline
public char getChar(Object o, long offset) {
return theInternalUnsafe.getChar(o, offset);
}
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的boolean型字段的值
@ForceInline
public boolean getBoolean(Object o, long offset) {
return theInternalUnsafe.getBoolean(o, offset);
}
/**
* Fetches a reference value from a given Java variable.
*
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的引用类型字段的值
@ForceInline
public Object getObject(Object o, long offset) {
return theInternalUnsafe.getObject(o, offset);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的byte型字段为新值x
@ForceInline
public void putByte(Object o, long offset, byte x) {
theInternalUnsafe.putByte(o, offset, x);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的short型字段为新值x
@ForceInline
public void putShort(Object o, long offset, short x) {
theInternalUnsafe.putShort(o, offset, x);
}
/**
* Stores a value into a given Java variable.
* <p>
* The first two parameters are interpreted exactly as with
* {@link #getInt(Object, long)} to refer to a specific
* Java variable (field or array element). The given value
* is stored into that variable.
* <p>
* The variable must be of the same type as the method
* parameter {@code x}.
*
* @param o Java heap object in which the variable resides, if any, else
* null
* @param offset indication of where the variable resides in a Java heap
* object, if any, else a memory address locating the variable
* statically
* @param x the value to store into the indicated Java variable
*
* @throws RuntimeException No defined exceptions are thrown, not even
* {@link NullPointerException}
*/
// 设置对象o中offset地址处对应的int型字段为新值x
@ForceInline
public void putInt(Object o, long offset, int x) {
theInternalUnsafe.putInt(o, offset, x);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的long型字段为新值x
@ForceInline
public void putLong(Object o, long offset, long x) {
theInternalUnsafe.putLong(o, offset, x);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的float型字段为新值x
@ForceInline
public void putFloat(Object o, long offset, float x) {
theInternalUnsafe.putFloat(o, offset, x);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的double型字段为新值x
@ForceInline
public void putDouble(Object o, long offset, double x) {
theInternalUnsafe.putDouble(o, offset, x);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的char型字段为新值x
@ForceInline
public void putChar(Object o, long offset, char x) {
theInternalUnsafe.putChar(o, offset, x);
}
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的boolean型字段为新值x
@ForceInline
public void putBoolean(Object o, long offset, boolean x) {
theInternalUnsafe.putBoolean(o, offset, x);
}
/**
* Stores a reference value into a given Java variable.
* <p>
* Unless the reference {@code x} being stored is either null
* or matches the field type, the results are undefined.
* If the reference {@code o} is non-null, card marks or
* other store barriers for that object (if the VM requires them)
* are updated.
*
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的引用类型字段为新值x
@ForceInline
public void putObject(Object o, long offset, Object x) {
theInternalUnsafe.putObject(o, offset, x);
}
/*▲ (1.1) getXXX/putXXX 获取/设置字段值(基于JVM内存地址) ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (1.2) getXXX/putXXX 获取/设置字段值(基于本地内存地址) ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Fetches a value from a given memory address. If the address is zero, or
* does not point into a block obtained from {@link #allocateMemory}, the
* results are undefined.
*
* @see #allocateMemory
*/
// 获取本地内存中address地址处对应的byte类型字段的值
@ForceInline
public byte getByte(long address) {
return theInternalUnsafe.getByte(address);
}
/**
* @see #getByte(long)
*/
// 获取本地内存中address地址处对应的short类型字段的值
@ForceInline
public short getShort(long address) {
return theInternalUnsafe.getShort(address);
}
/**
* @see #getByte(long)
*/
// 获取本地内存中address地址处对应的int类型字段的值
@ForceInline
public int getInt(long address) {
return theInternalUnsafe.getInt(address);
}
/**
* @see #getByte(long)
*/
// 获取本地内存中address地址处对应的long类型字段的值
@ForceInline
public long getLong(long address) {
return theInternalUnsafe.getLong(address);
}
/**
* @see #getByte(long)
*/
// 获取本地内存中address地址处对应的float类型字段的值
@ForceInline
public float getFloat(long address) {
return theInternalUnsafe.getFloat(address);
}
/**
* @see #getByte(long)
*/
// 获取本地内存中address地址处对应的double类型字段的值
@ForceInline
public double getDouble(long address) {
return theInternalUnsafe.getDouble(address);
}
/**
* @see #getByte(long)
*/
// 获取本地内存中address地址处对应的char类型字段的值
@ForceInline
public char getChar(long address) {
return theInternalUnsafe.getChar(address);
}
/**
* Stores a value into a given memory address. If the address is zero, or
* does not point into a block obtained from {@link #allocateMemory}, the
* results are undefined.
*
* @see #getByte(long)
*/
// 设置本地内存中address地址处对应的byte型字段为新值x
@ForceInline
public void putByte(long address, byte x) {
theInternalUnsafe.putByte(address, x);
}
/**
* @see #putByte(long, byte)
*/
// 设置本地内存中address地址处对应的short型字段为新值x
@ForceInline
public void putShort(long address, short x) {
theInternalUnsafe.putShort(address, x);
}
/**
* @see #putByte(long, byte)
*/
// 设置本地内存中address地址处对应的int型字段为新值x
@ForceInline
public void putInt(long address, int x) {
theInternalUnsafe.putInt(address, x);
}
/**
* @see #putByte(long, byte)
*/
// 设置本地内存中address地址处对应的long型字段为新值x
@ForceInline
public void putLong(long address, long x) {
theInternalUnsafe.putLong(address, x);
}
/**
* @see #putByte(long, byte)
*/
// 设置本地内存中address地址处对应的float型字段为新值x
@ForceInline
public void putFloat(long address, float x) {
theInternalUnsafe.putFloat(address, x);
}
/**
* @see #putByte(long, byte)
*/
// 设置本地内存中address地址处对应的double型字段为新值x
@ForceInline
public void putDouble(long address, double x) {
theInternalUnsafe.putDouble(address, x);
}
/**
* @see #putByte(long, byte)
*/
// 设置本地内存中address地址处对应的char型字段为新值x
@ForceInline
public void putChar(long address, char x) {
theInternalUnsafe.putChar(address, x);
}
/*▲ (1.2) getXXX/putXXX 获取/设置字段值(基于本地内存地址) ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ (3) putOrderedXXX 获取/设置字段值(基于JVM内存地址),Ordered/Lazy版本 █████████████████████████████████████████████████████████████┓ */
/** Ordered/Lazy version of {@link #putIntVolatile(Object, long, int)} */
// 设置对象o中offset地址处对应的int型字段为新值x
@ForceInline
public void putOrderedInt(Object o, long offset, int x) {
theInternalUnsafe.putIntRelease(o, offset, x);
}
/**
* Ordered/Lazy version of {@link #putLongVolatile(Object, long, long)}
*/
// 设置对象o中offset地址处对应的long型字段为新值x
@ForceInline