APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.foreign

public final Class Utils

extends Object
Class Inheritance
Imports
jdk.internal.access.SharedSecrets, jdk.internal.foreign.abi.SharedUtils, jdk.internal.misc.Unsafe, jdk.internal.vm.annotation.ForceInline, sun.invoke.util.Wrapper, java.lang.foreign.AddressLayout, .MemoryLayout, .MemorySegment, .StructLayout, .ValueLayout, java.lang.invoke.MethodHandle, .MethodHandles, .MethodType, .VarHandle, java.util.ArrayList, .List, .Map, java.util.concurrent.ConcurrentHashMap, java.util.function.Function, .Supplier

This class contains misc helper functions to support creation of memory segments.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static record

Field Summary

Modifier and TypeField and Description
private static final Class<?>
private static final MethodHandle
private static final MethodHandle
private static final MethodHandle

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static long
alignUp(long n, long alignment)

public static MemorySegment
alignUp(MemorySegment ms, long alignment)

private static byte
booleanToByte(boolean b)

public static boolean
byteToBoolean(byte b)

public static int
public static void
checkAlign(long byteAlignment)

public static void
checkAllocationSizeAndAlign(long byteSize, long byteAlignment)

public static void
public static void
public static void
checkNonNegativeArgument(long value, String name)

public static void
checkNonNegativeIndex(long value, String name)

public static StructLayout

Returns:

return a struct layout constructed from the given elements, with padding computed automatically so that they are naturally aligned
computePaddedStructLayout
(MemoryLayout...
the structs' fields
elements
)

Returns return a struct layout constructed from the given elements, with padding computed automatically so that they are naturally aligned.

private static long
computePadding(long offset, long align)

public static boolean
public static boolean
isAligned(long offset, long align)

public static boolean
public static boolean
isPowerOfTwo(long value)

public static MemorySegment
longToAddress(long addr)

public static MemorySegment
longToAddress(int addr)

public static MemorySegment
longToAddress(long addr, AddressLayout layout)

public static MemorySegment
longToAddress(int addr, AddressLayout layout)

public static MemorySegment
longToAddress(long addr, long size, long align)

public static MemorySegment
longToAddress(long addr, long size, long align, MemorySessionImpl scope)

public static VarHandle

Returns:

a raw memory segment var handle
makeRawSegmentViewVarHandle
(MemoryLayout
the enclosing context of the value layout
enclosing
,
ValueLayout
the value layout for which a raw memory segment var handle is to be created
layout
,
boolean
if the VH carries a constant offset instead of taking a variable offset
constantOffset
,
long
the offset if it is a constant
offset
)

This method returns a var handle that accesses a target layout in an enclosing layout, taking the memory offset and the base offset of the enclosing layout in the segment.

private static VarHandle
makeRawSegmentViewVarHandleInternal(MemoryLayout enclosing, ValueLayout layout, boolean constantOffset, long offset)

public static long
public static long
public static String
toHexString(long value)

public static <L extends MemoryLayout> L
wrapOverflow(Supplier<L> layoutSupplier)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ADDRESS_CARRIER_TYPEback to summary
private static final Class<?> ADDRESS_CARRIER_TYPE
LONG_TO_ADDRESS_NO_TARGETback to summary
private static final MethodHandle LONG_TO_ADDRESS_NO_TARGET
LONG_TO_ADDRESS_TARGETback to summary
private static final MethodHandle LONG_TO_ADDRESS_TARGET
LONG_TO_CARRIERback to summary
private static final MethodHandle LONG_TO_CARRIER

Constructor Detail

Utilsback to summary
private Utils()

Method Detail

alignUpback to summary
public static long alignUp(long n, long alignment)
alignUpback to summary
public static MemorySegment alignUp(MemorySegment ms, long alignment)
booleanToByteback to summary
private static byte booleanToByte(boolean b)
byteToBooleanback to summary
public static boolean byteToBoolean(byte b)
byteWidthOfPrimitiveback to summary
public static int byteWidthOfPrimitive(Class<?> primitive)
checkAlignback to summary
public static void checkAlign(long byteAlignment)
checkAllocationSizeAndAlignback to summary
public static void checkAllocationSizeAndAlign(long byteSize, long byteAlignment)
checkElementAlignmentback to summary
public static void checkElementAlignment(ValueLayout layout, String msg)
Annotations
@ForceInline
checkElementAlignmentback to summary
public static void checkElementAlignment(MemoryLayout layout, String msg)
Annotations
@ForceInline
checkNonNegativeArgumentback to summary
public static void checkNonNegativeArgument(long value, String name)
Annotations
@ForceInline
checkNonNegativeIndexback to summary
public static void checkNonNegativeIndex(long value, String name)
Annotations
@ForceInline
computePaddedStructLayoutback to summary
public static StructLayout computePaddedStructLayout(MemoryLayout... elements)

Returns return a struct layout constructed from the given elements, with padding computed automatically so that they are naturally aligned. .

Parameters
elements:MemoryLayout[]

the structs' fields

Returns:StructLayout

return a struct layout constructed from the given elements, with padding computed automatically so that they are naturally aligned

computePaddingback to summary
private static long computePadding(long offset, long align)
containsNullCharsback to summary
public static boolean containsNullChars(String s)
isAlignedback to summary
public static boolean isAligned(long offset, long align)
Annotations
@ForceInline
isElementAlignedback to summary
public static boolean isElementAligned(ValueLayout layout)
Annotations
@ForceInline
isPowerOfTwoback to summary
public static boolean isPowerOfTwo(long value)
longToAddressback to summary
public static MemorySegment longToAddress(long addr)
Annotations
@ForceInline
longToAddressback to summary
public static MemorySegment longToAddress(int addr)
Annotations
@ForceInline
longToAddressback to summary
public static MemorySegment longToAddress(long addr, AddressLayout layout)
Annotations
@ForceInline
longToAddressback to summary
public static MemorySegment longToAddress(int addr, AddressLayout layout)
Annotations
@ForceInline
longToAddressback to summary
public static MemorySegment longToAddress(long addr, long size, long align)
Annotations
@ForceInline
longToAddressback to summary
public static MemorySegment longToAddress(long addr, long size, long align, MemorySessionImpl scope)
Annotations
@ForceInline
makeRawSegmentViewVarHandleback to summary
public static VarHandle makeRawSegmentViewVarHandle(MemoryLayout enclosing, ValueLayout layout, boolean constantOffset, long offset)

This method returns a var handle that accesses a target layout in an enclosing layout, taking the memory offset and the base offset of the enclosing layout in the segment.

If the offset of the target layout in the enclosing layout is constant, the coordinates are (MS, long). If the offset of the target layout in the enclosing layout is variable, the coordinates are (MS, long, long). The trailing long is a pre-validated, variable extra offset, which the var handle does not perform any size or alignment checks against. Such checks are added (using adaptation) by LayoutPath#dereferenceHandle().

We provide two level of caching of the generated var handles. First, the var handle associated with a ValueLayout#varHandle() call is cached inside a stable field of the value layout implementation. This optimizes common code idioms like JAVA_INT.varHandle().getInt(...). A second layer of caching is then provided by this method, so different value layouts with same effects can reuse var handle instances. (The 2nd layer may be redundant in the long run)

Parameters
enclosing:MemoryLayout

the enclosing context of the value layout

layout:ValueLayout

the value layout for which a raw memory segment var handle is to be created

constantOffset:boolean

if the VH carries a constant offset instead of taking a variable offset

offset:long

the offset if it is a constant

Returns:VarHandle

a raw memory segment var handle

makeRawSegmentViewVarHandleInternalback to summary
private static VarHandle makeRawSegmentViewVarHandleInternal(MemoryLayout enclosing, ValueLayout layout, boolean constantOffset, long offset)
pointeeByteAlignback to summary
public static long pointeeByteAlign(AddressLayout addressLayout)
pointeeByteSizeback to summary
public static long pointeeByteSize(AddressLayout addressLayout)
toHexStringback to summary
public static String toHexString(long value)
wrapOverflowback to summary
public static <L extends MemoryLayout> L wrapOverflow(Supplier<L> layoutSupplier)
jdk.internal.foreign back to summary

public final Record Utils.BaseAndScale

extends Record
Class Inheritance
Record Components
base:long 
scale:long 

Field Summary

Modifier and TypeField and Description
private final long
base

Record Component accessed by base().

public static final Utils.BaseAndScale
public static final Utils.BaseAndScale
public static final Utils.BaseAndScale
public static final Utils.BaseAndScale
public static final Utils.BaseAndScale
public static final Utils.BaseAndScale
private final long
scale

Record Component accessed by scale().

public static final Utils.BaseAndScale

Constructor Summary

AccessConstructor and Description
public
BaseAndScale(long base, long scale)

Method Summary

Modifier and TypeMethod and Description
public long
base()

Record Component getter of base.

public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public static Utils.BaseAndScale
of(Object array)

public long
scale()

Record Component getter of scale.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

baseback to summary
private final long base

Record Component accessed by base().

BYTEback to summary
public static final Utils.BaseAndScale BYTE
CHARback to summary
public static final Utils.BaseAndScale CHAR
DOUBLEback to summary
public static final Utils.BaseAndScale DOUBLE
FLOATback to summary
public static final Utils.BaseAndScale FLOAT
INTback to summary
public static final Utils.BaseAndScale INT
LONGback to summary
public static final Utils.BaseAndScale LONG
scaleback to summary
private final long scale

Record Component accessed by scale().

SHORTback to summary
public static final Utils.BaseAndScale SHORT

Constructor Detail

BaseAndScaleback to summary
public BaseAndScale(long base, long scale)

Method Detail

baseback to summary
public long base()

Record Component getter of base.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

ofback to summary
public static Utils.BaseAndScale of(Object array)
scaleback to summary
public long scale()

Record Component getter of scale.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.