-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Expand file tree
/
Copy pathUnsafe.java
More file actions
1925 lines (1780 loc) · 71 KB
/
Unsafe.java
File metadata and controls
1925 lines (1780 loc) · 71 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
/*
* Copyright (c) 2000, 2025, 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 java.lang.foreign.Arena;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.VarHandle;
import java.lang.reflect.Field;
import java.security.CodeSource;
import java.util.List;
import java.util.Set;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Stable;
import jdk.internal.misc.VM;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
/**
* 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 responsibility 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!
*
* @apiNote
* This class pre-dates the introduction of {@link VarHandle}, low-level access to
* memory with {@linkplain java.lang.foreign}, and other standard APIs. New code
* should not use this API.
*
* @author John R. Rose
* @see #getUnsafe
*/
public final class Unsafe {
static {
Reflection.registerMethodsToFilter(Unsafe.class, Set.of("getUnsafe"));
}
private Unsafe() {}
private static final Unsafe theUnsafe = new Unsafe();
private static final jdk.internal.misc.Unsafe theInternalUnsafe = jdk.internal.misc.Unsafe.getUnsafe();
/**
* 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.
*/
@CallerSensitive
public static Unsafe getUnsafe() {
Class<?> caller = Reflection.getCallerClass();
if (!VM.isSystemDomainLoader(caller.getClassLoader()))
throw new SecurityException("Unsafe");
return theUnsafe;
}
//| peek and poke operations
//| (compilers should optimize these to memory ops)
// These work on object fields in the Java heap.
// They will not work on elements of packed arrays.
/**
* 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.
*
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfInt, long)} instead.
*
* @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}
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public int getInt(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getInt(o, offset);
}
/**
* 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}.
*
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfInt, long, int)} instead.
*
* @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}
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putInt(Object o, long offset, int x) {
beforeMemoryAccess();
theInternalUnsafe.putInt(o, offset, x);
}
/**
* Fetches a reference value from a given Java variable.
*
* @deprecated Use {@link VarHandle#get(Object...)} instead.
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public Object getObject(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getReference(o, offset);
}
/**
* 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.
*
* @deprecated Use {@link VarHandle#set(Object...)} instead.
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putObject(Object o, long offset, Object x) {
beforeMemoryAccess();
theInternalUnsafe.putReference(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfBoolean, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public boolean getBoolean(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getBoolean(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfBoolean, long, boolean)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putBoolean(Object o, long offset, boolean x) {
beforeMemoryAccess();
theInternalUnsafe.putBoolean(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfByte, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public byte getByte(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getByte(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfByte, long, byte)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putByte(Object o, long offset, byte x) {
beforeMemoryAccess();
theInternalUnsafe.putByte(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfShort, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public short getShort(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getShort(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfShort, long, short)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putShort(Object o, long offset, short x) {
beforeMemoryAccess();
theInternalUnsafe.putShort(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfChar, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public char getChar(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getChar(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfChar, long, char)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putChar(Object o, long offset, char x) {
beforeMemoryAccess();
theInternalUnsafe.putChar(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfLong, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public long getLong(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getLong(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfLong, long, long)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putLong(Object o, long offset, long x) {
beforeMemoryAccess();
theInternalUnsafe.putLong(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfFloat, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public float getFloat(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getFloat(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfFloat, long, float)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putFloat(Object o, long offset, float x) {
beforeMemoryAccess();
theInternalUnsafe.putFloat(o, offset, x);
}
/**
* @deprecated Use {@link VarHandle#get(Object...)} or
* {@link MemorySegment#get(ValueLayout.OfDouble, long)} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public double getDouble(Object o, long offset) {
beforeMemoryAccess();
return theInternalUnsafe.getDouble(o, offset);
}
/**
* @deprecated Use {@link VarHandle#set(Object...)} or
* {@link MemorySegment#set(ValueLayout.OfDouble, long, double)} instead.
*
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putDouble(Object o, long offset, double x) {
beforeMemoryAccess();
theInternalUnsafe.putDouble(o, offset, x);
}
// These work on values in the C heap.
/**
* 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.
*
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #allocateMemory
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public byte getByte(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getByte(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.
*
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putByte(long address, byte x) {
beforeMemoryAccess();
theInternalUnsafe.putByte(address, x);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public short getShort(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getShort(address);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #putByte(long, byte)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putShort(long address, short x) {
beforeMemoryAccess();
theInternalUnsafe.putShort(address, x);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public char getChar(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getChar(address);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #putByte(long, byte)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putChar(long address, char x) {
beforeMemoryAccess();
theInternalUnsafe.putChar(address, x);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public int getInt(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getInt(address);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #putByte(long, byte)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putInt(long address, int x) {
beforeMemoryAccess();
theInternalUnsafe.putInt(address, x);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public long getLong(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getLong(address);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #putByte(long, byte)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putLong(long address, long x) {
beforeMemoryAccess();
theInternalUnsafe.putLong(address, x);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public float getFloat(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getFloat(address);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #putByte(long, byte)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putFloat(long address, float x) {
beforeMemoryAccess();
theInternalUnsafe.putFloat(address, x);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getByte(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public double getDouble(long address) {
beforeMemoryAccess();
return theInternalUnsafe.getDouble(address);
}
/**
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #putByte(long, byte)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putDouble(long address, double x) {
beforeMemoryAccess();
theInternalUnsafe.putDouble(address, x);
}
/**
* 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}.
*
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #allocateMemory
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public long getAddress(long address) {
beforeMemoryAccess();
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}.
*
* @deprecated Use {@link java.lang.foreign} to access off-heap memory.
*
* @see #getAddress(long)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void putAddress(long address, long x) {
beforeMemoryAccess();
theInternalUnsafe.putAddress(address, x);
}
//| wrappers for malloc, realloc, free:
/**
* 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 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}.
*
* <em>Note:</em> It is the responsibility 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!
*
* @deprecated Use {@link java.lang.foreign} to allocate off-heap memory.
*
* @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)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public long allocateMemory(long bytes) {
beforeMemoryAccess();
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 responsibility 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!
*
* @deprecated Use {@link java.lang.foreign} to allocate off-heap memory.
*
* @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
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public long reallocateMemory(long address, long bytes) {
beforeMemoryAccess();
return theInternalUnsafe.reallocateMemory(address, bytes);
}
/**
* 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 responsibility 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!
*
* @deprecated {@link MemorySegment#fill(byte)} fills the contents of a memory
* segment with a given value.
*
* @throws RuntimeException if any of the arguments is invalid
*
* @since 1.7
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void setMemory(Object o, long offset, long bytes, byte value) {
beforeMemoryAccess();
theInternalUnsafe.setMemory(o, offset, bytes, value);
}
/**
* 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)}.
*
* @deprecated {@link MemorySegment#fill(byte)} fills the contents of a memory
* segment with a given value.
*
* Use {@link MemorySegment} and its bulk copy methods instead.
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void setMemory(long address, long bytes, byte value) {
beforeMemoryAccess();
theInternalUnsafe.setMemory(address, bytes, value);
}
/**
* 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 responsibility 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!
*
* @deprecated Use {@link MemorySegment} and its bulk copy methods instead.
*
* @throws RuntimeException if any of the arguments is invalid
*
* @since 1.7
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void copyMemory(Object srcBase, long srcOffset,
Object destBase, long destOffset,
long bytes) {
beforeMemoryAccess();
theInternalUnsafe.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes);
}
/**
* 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)}.
*
* @deprecated Use {@link MemorySegment} and its bulk copy methods instead.
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void copyMemory(long srcAddress, long destAddress, long bytes) {
beforeMemoryAccess();
theInternalUnsafe.copyMemory(srcAddress, destAddress, 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 responsibility 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!
*
* @deprecated Use {@link java.lang.foreign} to allocate and free off-heap memory.
*
* @throws RuntimeException if any of the arguments is invalid
*
* @see #allocateMemory
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public void freeMemory(long address) {
beforeMemoryAccess();
theInternalUnsafe.freeMemory(address);
}
//| random queries
/**
* This constant differs from all results that will ever be returned from
* {@link #staticFieldOffset}, {@link #objectFieldOffset},
* or {@link #arrayBaseOffset}.
* @deprecated Not needed when using {@link VarHandle} or {@link java.lang.foreign}.
*/
@Deprecated(since="23", forRemoval=true)
public static final int INVALID_FIELD_OFFSET = (int) jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET;
/**
* 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.
*
* @deprecated The guarantee that a field will always have the same offset
* and base may not be true in a future release. The ability to provide an
* offset and object reference to a heap memory accessor will be removed
* in a future release. Use {@link VarHandle} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="18", forRemoval=true)
@ForceInline
public long objectFieldOffset(Field f) {
if (f == null) {
throw new NullPointerException();
}
Class<?> declaringClass = f.getDeclaringClass();
if (declaringClass.isHidden()) {
throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
}
if (declaringClass.isRecord()) {
throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
}
beforeMemoryAccess();
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.
*
* @deprecated The guarantee that a field will always have the same offset
* and base may not be true in a future release. The ability to provide an
* offset and object reference to a heap memory accessor will be removed
* in a future release. Use {@link VarHandle} instead.
*
* @see #getInt(Object, long)
*/
@Deprecated(since="18", forRemoval=true)
@ForceInline
public long staticFieldOffset(Field f) {
if (f == null) {
throw new NullPointerException();
}
Class<?> declaringClass = f.getDeclaringClass();
if (declaringClass.isHidden()) {
throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
}
if (declaringClass.isRecord()) {
throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
}
beforeMemoryAccess();
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.
*
* @deprecated The guarantee that a field will always have the same offset
* and base may not be true in a future release. The ability to provide an
* offset and object reference to a heap memory accessor will be removed
* in a future release. Use {@link VarHandle} instead.
*/
@Deprecated(since="18", forRemoval=true)
@ForceInline
public Object staticFieldBase(Field f) {
if (f == null) {
throw new NullPointerException();
}
Class<?> declaringClass = f.getDeclaringClass();
if (declaringClass.isHidden()) {
throw new UnsupportedOperationException("can't get base address on a hidden class: " + f);
}
if (declaringClass.isRecord()) {
throw new UnsupportedOperationException("can't get base address on a record class: " + f);
}
beforeMemoryAccess();
return theInternalUnsafe.staticFieldBase(f);
}
/**
* Reports the offset of the first element in the storage allocation of a
* given array class. If {@link #arrayIndexScale} returns a non-zero value
* for the same class, you may use that scale factor, together with this
* base offset, to form new offsets to access elements of arrays of the
* given class.
*
* @deprecated Not needed when using {@link VarHandle} or {@link java.lang.foreign}.
*
* @see #getInt(Object, long)
* @see #putInt(Object, long, int)
*/
@Deprecated(since="23", forRemoval=true)
@ForceInline
public int arrayBaseOffset(Class<?> arrayClass) {
beforeMemoryAccess();
return (int) theInternalUnsafe.arrayBaseOffset(arrayClass);
}
/** The value of {@code arrayBaseOffset(boolean[].class)}.