-
Notifications
You must be signed in to change notification settings - Fork 668
/
Unsafe.java
4253 lines (3633 loc) · 182 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, 2017, 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 jdk.internal.misc;
import jdk.internal.HotSpotIntrinsicCandidate;
import jdk.internal.vm.annotation.ForceInline;
import java.lang.reflect.Field;
import java.security.ProtectionDomain;
/**
* 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
*/
/*
* 除了实现sun.misc.Unsafe中列出的方法,还提供了其他更精细的低级别操作,与底层交互紧密
*
* 针对多线程中单变量的可视性,以及多变量的顺序依赖和值依赖,JDK 9 引入四种资源同步的语义,由弱到强分别是:
* Plain, Opaque, Release[write]/Acquire[read], Volatile。【参见VarHandle】
*/
public final class Unsafe {
// 单例对象
private static final Unsafe theUnsafe = new Unsafe();
/**
* 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 = -1;
/* 用于 #arrayBaseOffset 的值*/
/**
* The value of {@code arrayBaseOffset(boolean[].class)}
*/
public static final int ARRAY_BOOLEAN_BASE_OFFSET = theUnsafe.arrayBaseOffset(boolean[].class);
/**
* The value of {@code arrayBaseOffset(byte[].class)}
*/
public static final int ARRAY_BYTE_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
/**
* The value of {@code arrayBaseOffset(short[].class)}
*/
public static final int ARRAY_SHORT_BASE_OFFSET = theUnsafe.arrayBaseOffset(short[].class);
/**
* The value of {@code arrayBaseOffset(char[].class)}
*/
public static final int ARRAY_CHAR_BASE_OFFSET = theUnsafe.arrayBaseOffset(char[].class);
/**
* The value of {@code arrayBaseOffset(int[].class)}
*/
public static final int ARRAY_INT_BASE_OFFSET = theUnsafe.arrayBaseOffset(int[].class);
/**
* The value of {@code arrayBaseOffset(long[].class)}
*/
public static final int ARRAY_LONG_BASE_OFFSET = theUnsafe.arrayBaseOffset(long[].class);
/**
* The value of {@code arrayBaseOffset(float[].class)}
*/
public static final int ARRAY_FLOAT_BASE_OFFSET = theUnsafe.arrayBaseOffset(float[].class);
/**
* The value of {@code arrayBaseOffset(double[].class)}
*/
public static final int ARRAY_DOUBLE_BASE_OFFSET = theUnsafe.arrayBaseOffset(double[].class);
/**
* The value of {@code arrayBaseOffset(Object[].class)}
*/
public static final int ARRAY_OBJECT_BASE_OFFSET = theUnsafe.arrayBaseOffset(Object[].class);
/* 用于 #arrayIndexScale 的值*/
/**
* The value of {@code arrayIndexScale(boolean[].class)}
*/
public static final int ARRAY_BOOLEAN_INDEX_SCALE = theUnsafe.arrayIndexScale(boolean[].class);
/**
* The value of {@code arrayIndexScale(byte[].class)}
*/
public static final int ARRAY_BYTE_INDEX_SCALE = theUnsafe.arrayIndexScale(byte[].class);
/**
* The value of {@code arrayIndexScale(short[].class)}
*/
public static final int ARRAY_SHORT_INDEX_SCALE = theUnsafe.arrayIndexScale(short[].class);
/**
* The value of {@code arrayIndexScale(char[].class)}
*/
public static final int ARRAY_CHAR_INDEX_SCALE = theUnsafe.arrayIndexScale(char[].class);
/**
* The value of {@code arrayIndexScale(int[].class)}
*/
public static final int ARRAY_INT_INDEX_SCALE = theUnsafe.arrayIndexScale(int[].class);
/**
* The value of {@code arrayIndexScale(long[].class)}
*/
public static final int ARRAY_LONG_INDEX_SCALE = theUnsafe.arrayIndexScale(long[].class);
/**
* The value of {@code arrayIndexScale(float[].class)}
*/
public static final int ARRAY_FLOAT_INDEX_SCALE = theUnsafe.arrayIndexScale(float[].class);
/**
* The value of {@code arrayIndexScale(double[].class)}
*/
public static final int ARRAY_DOUBLE_INDEX_SCALE = theUnsafe.arrayIndexScale(double[].class);
/**
* The value of {@code arrayIndexScale(Object[].class)}
*/
public static final int ARRAY_OBJECT_INDEX_SCALE = theUnsafe.arrayIndexScale(Object[].class);
/** The value of {@code addressSize()} */
// 本机指针的大小(以字节为单位)。参见#addressSize方法
public static final int ADDRESS_SIZE = theUnsafe.addressSize0();
static {
registerNatives();
}
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
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}.)
*/
// 返回单例对象,由系统内部的方法调用
public static Unsafe getUnsafe() {
return theUnsafe;
}
/**
* Allocates an instance but does not run any constructor.
* Initializes the class if it has not yet been.
*/
// 不调用构造器就生成对象,但是该对象的字段会被赋为对应类型的"零值",为该对象赋过的默认值也无效
@HotSpotIntrinsicCandidate
public native Object allocateInstance(Class<?> cls) throws InstantiationException;
/**
* 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
*/
// 定义(创建)一个虚拟机匿名类,该类不会被类加载器或系统目录发现
public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
if(hostClass == null || data == null) {
throw new NullPointerException();
}
if(hostClass.isArray() || hostClass.isPrimitive()) {
throw new IllegalArgumentException();
}
return defineAnonymousClass0(hostClass, data, cpPatches);
}
/**
* Tells the VM to define a class, without security checks.
* By default, the class loader and protection domain come from the caller's class.
*/
/*
* 通知虚拟机,定义(创建)一个类,不进行安全检查,返回定义好的类。
*
* className:类名
* bytecodes[off, off+len):创建类时需要使用的字节码
* loader:类加载器
*/
public Class<?> defineClass(String className, byte[] bytecodes, int off, int len, ClassLoader loader, ProtectionDomain protectionDomain) {
if(bytecodes == null) {
throw new NullPointerException();
}
if(len<0) {
throw new ArrayIndexOutOfBoundsException();
}
return defineClass0(className, bytecodes, off, len, loader, protectionDomain);
}
/**
* 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)
*/
// 获取非静态字段f的JVM偏移地址
public long objectFieldOffset(Field f) {
if(f == null) {
throw new NullPointerException();
}
return objectFieldOffset0(f);
}
/**
* Reports the location of the field with a given name in the storage allocation of its class.
*
* @throws NullPointerException if any parameter is {@code null}.
* @throws InternalError if there is no field named {@code name} declared
* in class {@code c}, i.e., if {@code c.getDeclaredField(name)}
* would throw {@code java.lang.NoSuchFieldException}.
* @see #objectFieldOffset(Field)
*/
// 获取非静态字段name在其类中的JVM偏移地址
public long objectFieldOffset(Class<?> c, String name) {
if(c == null || name == null) {
throw new NullPointerException();
}
return objectFieldOffset1(c, name);
}
/**
* 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)
*/
// 获取静态字段f的JVM偏移地址
public long staticFieldOffset(Field f) {
if(f == null) {
throw new NullPointerException();
}
return staticFieldOffset0(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.
*/
// 获取静态字段所属的类对象
public Object staticFieldBase(Field f) {
if (f == null) {
throw new NullPointerException();
}
return staticFieldBase0(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
*/
public boolean shouldBeInitialized(Class<?> c) {
if (c == null) {
throw new NullPointerException();
}
return shouldBeInitialized0(c);
}
/**
* Ensures the given class has been initialized. This is often
* needed in conjunction with obtaining the static field base of a
* class.
*/
public void ensureClassInitialized(Class<?> c) {
if(c == null) {
throw new NullPointerException();
}
ensureClassInitialized0(c);
}
private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
public native Class<?> defineClass0(String name, byte[] b, int off, int len, ClassLoader loader, ProtectionDomain protectionDomain);
private native long objectFieldOffset0(Field f);
private native long objectFieldOffset1(Class<?> c, String name);
private native long staticFieldOffset0(Field f);
private native Object staticFieldBase0(Field f);
private native boolean shouldBeInitialized0(Class<?> c);
private native void ensureClassInitialized0(Class<?> 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字节的本地内存,并返回分配的内存地址
public long allocateMemory(long bytes) {
allocateMemoryChecks(bytes);
if(bytes == 0) {
return 0;
}
long p = allocateMemory0(bytes);
if(p == 0) {
throw new OutOfMemoryError();
}
return p;
}
/**
* 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一致
public long reallocateMemory(long address, long bytes) {
reallocateMemoryChecks(address, bytes);
if(bytes == 0) {
freeMemory(address);
return 0;
}
long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
if(p == 0) {
throw new OutOfMemoryError();
}
return p;
}
/**
* 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填充
public void setMemory(long address, long bytes, byte value) {
setMemory(null, 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填充
public void setMemory(Object o, long offset, long bytes, byte value) {
setMemoryChecks(o, offset, bytes, value);
if(bytes == 0) {
return;
}
setMemory0(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)}.
*/
// 内存数据拷贝
public void copyMemory(long srcAddress, long destAddress, long bytes) {
copyMemory(null, srcAddress, null, 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
*/
// 内存数据拷贝
public void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes) {
copyMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes);
if(bytes == 0) {
return;
}
copyMemory0(srcBase, srcOffset, destBase, destOffset, bytes);
}
/**
* Copies all elements from one block of memory to another block, byte swapping the
* elements on the fly.
*
* This provides a <em>single-register</em> addressing mode, as
* discussed in {@link #getInt(Object, long)}.
*
* Equivalent to {@code copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize)}.
*/
// 内存数据拷贝
public void copySwapMemory(long srcAddress, long destAddress, long bytes, long elemSize) {
copySwapMemory(null, srcAddress, null, destAddress, bytes, elemSize);
}
/**
* Copies all elements from one block of memory to another block,
* *unconditionally* byte swapping the elements on the fly.
*
* <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.
*
* <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 9
*/
// 内存数据拷贝
public void copySwapMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize) {
copySwapMemoryChecks(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
if(bytes == 0) {
return;
}
copySwapMemory0(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
}
/**
* 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申请的内存
public void freeMemory(long address) {
freeMemoryChecks(address);
if(address == 0) {
return;
}
freeMemory0(address);
}
/** @see #getAddress(Object, long) */
// 从本地内存地址address处获取一个本地指针值
@ForceInline
public long getAddress(long address) {
return getAddress(null, 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
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的long型字段的值
@ForceInline
public long getAddress(Object o, long offset) {
// 如果本机指针的宽度小于8字节,则将其作为无符号数扩展为Java的long类型
if (ADDRESS_SIZE == 4) {
// 获取对象o中offset地址处对应的int型字段的值
int x = getInt(o, offset);
// 转为无符号的long
return Integer.toUnsignedLong(x);
} else {
// 获取对象o中offset地址处对应的long型字段的值
return getLong(o, offset);
}
}
/** @see #putAddress(Object, long, long) */
// 向本地内存地址address处存入一个本地指针值x
@ForceInline
public void putAddress(long address, long x) {
putAddress(null, address, x);
}
/**
* 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 #allocateMemory
* @see #putInt(Object, long, int)
*/
// 向对象o中offset地址处存入long型字段的值
@ForceInline
public void putAddress(Object o, long offset, long x) {
if (ADDRESS_SIZE == 4) {
putInt(o, offset, (int)x);
} else {
putLong(o, offset, 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。请注意,其他基本类型的大小(存储在本机内存块中)完全由其信息内容决定。
public int addressSize() {
return ADDRESS_SIZE;
}
private native long allocateMemory0(long bytes);
private native long reallocateMemory0(long address, long bytes);
private native void setMemory0(Object o, long offset, long bytes, byte value);
@HotSpotIntrinsicCandidate
private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
private native void freeMemory0(long address);
private native int addressSize0();
/*▲ 本地内存操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/* 获取/设置字段值 ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ */
/*
* (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)
*
* (2) 基于JVM内存地址,获取/设置字段值,Opaque版本
* - getXXXOpaque(o, offset)
* - putXXXOpaque(o, offset, x)
*
* (3) 基于JVM内存地址,获取/设置字段值,Release/Acquire版本
* - getXXXAcquire(o, offset)
* - putXXXRelease(o, offset, x)
*
* (4) 基于JVM内存地址,获取/设置字段值,Volatile版本
* - getXXXVolatile(o, offset)
* - putXXXVolatile(o, offset, x)
*/
/*▼ (1.1) getXXX/putXXX 获取/设置字段值(基于JVM内存地址) ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的int型字段的值
@HotSpotIntrinsicCandidate
public native byte getByte(Object o, long offset);
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的short型字段的值
@HotSpotIntrinsicCandidate
public native short getShort(Object o, long 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型字段的值
@HotSpotIntrinsicCandidate
public native int getInt(Object o, long offset);
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的long型字段的值
@HotSpotIntrinsicCandidate
public native long getLong(Object o, long offset);
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的float型字段的值
@HotSpotIntrinsicCandidate
public native float getFloat(Object o, long offset);
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的double型字段的值
@HotSpotIntrinsicCandidate
public native double getDouble(Object o, long offset);
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的char型字段的值
@HotSpotIntrinsicCandidate
public native char getChar(Object o, long offset);
/**
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的boolean型字段的值
@HotSpotIntrinsicCandidate
public native boolean getBoolean(Object o, long offset);
/**
* Fetches a reference value from a given Java variable.
*
* @see #getInt(Object, long)
*/
// 获取对象o中offset地址处对应的引用类型字段的值
@HotSpotIntrinsicCandidate
public native Object getObject(Object o, long offset);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的byte型字段为新值x
@HotSpotIntrinsicCandidate
public native void putByte(Object o, long offset, byte x);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的short型字段为新值x
@HotSpotIntrinsicCandidate
public native void putShort(Object o, long offset, short 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
@HotSpotIntrinsicCandidate
public native void putInt(Object o, long offset, int x);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的long型字段为新值x
@HotSpotIntrinsicCandidate
public native void putLong(Object o, long offset, long x);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的float型字段为新值x
@HotSpotIntrinsicCandidate
public native void putFloat(Object o, long offset, float x);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的double型字段为新值x
@HotSpotIntrinsicCandidate
public native void putDouble(Object o, long offset, double x);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的char型字段为新值x
@HotSpotIntrinsicCandidate
public native void putChar(Object o, long offset, char x);
/**
* @see #putInt(Object, long, int)
*/
// 设置对象o中offset地址处对应的boolean型字段为新值x
@HotSpotIntrinsicCandidate
public native void putBoolean(Object o, long offset, boolean 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
@HotSpotIntrinsicCandidate
public native void putObject(Object o, long offset, Object 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.
*