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

public abstract sealed Class AbstractMemorySegmentImpl

extends Object
implements MemorySegment, SegmentAllocator
permits HeapMemorySegmentImpl, NativeMemorySegmentImpl
Class Inheritance
All Implemented Interfaces
java.lang.foreign.SegmentAllocator, java.lang.foreign.MemorySegment
Known Direct Subclasses
jdk.internal.foreign.HeapMemorySegmentImpl, jdk.internal.foreign.NativeMemorySegmentImpl
Imports
jdk.internal.access.JavaNioAccess, .SharedSecrets, jdk.internal.access.foreign.UnmapperProxy, jdk.internal.misc.ScopedMemoryAccess, jdk.internal.reflect.CallerSensitive, .Reflection, jdk.internal.util.ArraysSupport, .Preconditions, jdk.internal.vm.annotation.DontInline, .ForceInline, sun.nio.ch.DirectBuffer, java.lang.foreign.AddressLayout, .Arena, .MemoryLayout, .MemorySegment, .SegmentAllocator, .ValueLayout, java.lang.reflect.Array, java.nio.Buffer, .ByteBuffer, .ByteOrder, java.nio.charset.Charset, java.util.List, .Objects, .Optional, .Spliterator, java.util.function.BiFunction, .Consumer, .Function, .IntFunction, java.util.stream.Stream, .StreamSupport

This abstract class provides an immutable implementation for the MemorySegment interface. This class contains information about the segment's spatial and temporal bounds; each memory segment implementation is associated with an owner thread which is set at creation time. Access to certain sensitive operations on the memory segment will fail with IllegalStateException if the segment is either in an invalid state (e.g. it has already been closed) or if access occurs from a thread other than the owner thread. See MemorySessionImpl for more details on management of temporal bounds. Subclasses are defined for each memory segment kind, see NativeMemorySegmentImpl, HeapMemorySegmentImpl and MappedMemorySegmentImpl.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv final long
pack-priv static final JavaNioAccess
pack-priv final boolean
pack-priv final MemorySessionImpl

Constructor Summary

AccessConstructor and Description
pack-priv
AbstractMemorySegmentImpl(long length, boolean readOnly, MemorySessionImpl scope)

Method Summary

Modifier and TypeMethod and Description
public MemorySegment
allocate(long byteSize, long byteAlignment)

Implements java.lang.foreign.SegmentAllocator.allocate.

Returns a new memory segment with the given byteSize and byteAlignment.

public final ByteBuffer
public final Optional<MemorySegment>
asOverlappingSlice(MemorySegment other)

Implements java.lang.foreign.MemorySegment.asOverlappingSlice.

Returns a slice of this segment that is the overlap between this and the provided segment.

public AbstractMemorySegmentImpl
asReadOnly()

Implements java.lang.foreign.MemorySegment.asReadOnly.

Returns a read-only view of this segment.

public AbstractMemorySegmentImpl
asSlice(long offset, long newSize)

Implements java.lang.foreign.MemorySegment.asSlice.

Returns a slice of this memory segment, at the given offset.

public AbstractMemorySegmentImpl
asSlice(long offset)

Implements java.lang.foreign.MemorySegment.asSlice.

Returns a slice of this memory segment, at the given offset.

public MemorySegment
asSlice(long offset, long newSize, long byteAlignment)

Implements java.lang.foreign.MemorySegment.asSlice.

Returns a slice of this memory segment, at the given offset, with the provided alignment constraint.

public MemorySegment
asSlice(long offset, MemoryLayout layout)

Implements java.lang.foreign.MemorySegment.asSlice.

Returns a slice of this memory segment with the given layout, at the given offset.

private AbstractMemorySegmentImpl
asSliceNoCheck(long offset, long newSize)

private static Object
bufferRef(Buffer buffer)

private static MemorySessionImpl
public final long
byteSize()

Implements java.lang.foreign.MemorySegment.byteSize.

Returns the size (in bytes) of this memory segment.

public void
checkAccess(long offset, long length, boolean readOnly)

pack-priv void
checkAccessBounds(long offset, long length)

private int
checkArraySize(String typeName, int elemSize)

private void
checkBounds(long offset, long length)

public final void
checkEnclosingLayout(long offset, MemoryLayout enclosing, boolean readOnly)

public void
checkReadOnly(boolean readOnly)

pack-priv void
checkSliceBounds(long offset, long length)

private static Runnable
cleanupAction(long address, long newSize, Consumer<MemorySegment> cleanup)

public static void
copy(MemorySegment srcSegment, ValueLayout srcElementLayout, long srcOffset, MemorySegment dstSegment, ValueLayout dstElementLayout, long dstOffset, long elementCount)

Hides java.lang.foreign.MemorySegment.copy.

Performs a bulk copy from source segment to destination segment.

public static void
copy(MemorySegment srcSegment, ValueLayout srcLayout, long srcOffset, Object dstArray, int dstIndex, int elementCount)

Hides java.lang.foreign.MemorySegment.copy.

Copies a number of elements from a source memory segment to a destination array.

public static void
copy(Object srcArray, int srcIndex, MemorySegment dstSegment, ValueLayout dstLayout, long dstOffset, int elementCount)

Hides java.lang.foreign.MemorySegment.copy.

Copies a number of elements from a source array to a destination memory segment.

public static long
copy(String src, Charset dstEncoding, int srcIndex, MemorySegment dst, long dstOffset, int numChars)

Hides java.lang.foreign.MemorySegment.copy.

Copies the byte sequence of the given string encoded using the provided charset to the destination segment.

public MemorySegment
copyFrom(MemorySegment src)

Implements java.lang.foreign.MemorySegment.copyFrom.

Performs a bulk copy from the given source segment to this segment.

pack-priv abstract AbstractMemorySegmentImpl
dup(long offset, long size, boolean readOnly, MemorySessionImpl scope)

public Stream<MemorySegment>
elements(MemoryLayout elementLayout)

Implements java.lang.foreign.MemorySegment.elements.

Returns a sequential Stream over disjoint slices (whose size matches that of the specified layout) in this segment.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Implements java.lang.foreign.MemorySegment.equals.

Compares the specified object with this memory segment for equality.

public final MemorySegment
fill(byte value)

Implements java.lang.foreign.MemorySegment.fill.

Fills the contents of this memory segment with the given value.

public void
force()

Implements java.lang.foreign.MemorySegment.force.

Forces any changes made to the contents of this mapped segment to be written to the storage device described by the mapped segment's file descriptor.

public byte
get(ValueLayout.OfByte layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a byte from this segment at the given offset, with the given layout.

public boolean
get(ValueLayout.OfBoolean layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a boolean from this segment at the given offset, with the given layout.

public char
get(ValueLayout.OfChar layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a char from this segment at the given offset, with the given layout.

public short
get(ValueLayout.OfShort layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a short from this segment at the given offset, with the given layout.

public int
get(ValueLayout.OfInt layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads an int from this segment at the given offset, with the given layout.

public float
get(ValueLayout.OfFloat layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a float from this segment at the given offset, with the given layout.

public long
get(ValueLayout.OfLong layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a long from this segment at the given offset, with the given layout.

public double
get(ValueLayout.OfDouble layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads a double from this segment at the given offset, with the given layout.

public MemorySegment
get(AddressLayout layout, long offset)

Implements java.lang.foreign.MemorySegment.get.

Reads an address from this segment at the given offset, with the given layout.

public byte
getAtIndex(ValueLayout.OfByte layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a byte from this segment at the given index, scaled by the given layout size.

public boolean
getAtIndex(ValueLayout.OfBoolean layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a boolean from this segment at the given index, scaled by the given layout size.

public char
getAtIndex(ValueLayout.OfChar layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a char from this segment at the given index, scaled by the given layout size.

public short
getAtIndex(ValueLayout.OfShort layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a short from this segment at the given index, scaled by the given layout size.

public int
getAtIndex(ValueLayout.OfInt layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads an int from this segment at the given index, scaled by the given layout size.

public float
getAtIndex(ValueLayout.OfFloat layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a float from this segment at the given index, scaled by the given layout size.

public long
getAtIndex(ValueLayout.OfLong layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a long from this segment at the given index, scaled by the given layout size.

public double
getAtIndex(ValueLayout.OfDouble layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads a double from this segment at the given index, scaled by the given layout size.

public MemorySegment
getAtIndex(AddressLayout layout, long index)

Implements java.lang.foreign.MemorySegment.getAtIndex.

Reads an address from this segment at the given at the given index, scaled by the given layout size.

public String
getString(long offset, Charset charset, long byteLength)

Implements java.lang.foreign.MemorySegment.getString.

Reads a string from this segment at the given offset, using the provided length and charset.

public String
getString(long offset)

Implements java.lang.foreign.MemorySegment.getString.

Reads a null-terminated string from this segment at the given offset, using the UTF-8 charset.

public String
getString(long offset, Charset charset)

Implements java.lang.foreign.MemorySegment.getString.

Reads a null-terminated string from this segment at the given offset, using the provided charset.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.lang.foreign.MemorySegment.hashCode.

Returns the hash code value for this memory segment.

public boolean
isAccessibleBy(Thread thread)

Implements java.lang.foreign.MemorySegment.isAccessibleBy.

Returns true if this segment can be accessed from the provided thread.

public final boolean
isAlignedForElement(long offset, MemoryLayout layout)

public final boolean
isAlignedForElement(long offset, long byteAlignment)

public boolean
isLoaded()

Implements java.lang.foreign.MemorySegment.isLoaded.

Determines whether all the contents of this mapped segment are resident in physical memory.

public boolean
isMapped()

Implements java.lang.foreign.MemorySegment.isMapped.

Returns true if this segment is a mapped segment.

public boolean
isNative()

Implements java.lang.foreign.MemorySegment.isNative.

Returns true if this segment is a native segment.

public boolean
isReadOnly()

Implements java.lang.foreign.MemorySegment.isReadOnly.

Returns true, if this segment is read-only.

private static long
length(Buffer b, int scaleShifts)

public void
load()

Implements java.lang.foreign.MemorySegment.load.

Loads the contents of this mapped segment into physical memory.

pack-priv abstract ByteBuffer
public abstract long
public long
mismatch(MemorySegment other)

Implements java.lang.foreign.MemorySegment.mismatch.

Finds and returns the offset, in bytes, of the first mismatch between this segment and the given other segment.

private static NativeMemorySegmentImpl
nativeSegment(Buffer b, long offset, long length)

private static UnsupportedOperationException
public static AbstractMemorySegmentImpl
ofBuffer(Buffer b)

Hides java.lang.foreign.MemorySegment.ofBuffer.

Creates a memory segment that is backed by the same region of memory that backs the given Buffer instance.

private static AbstractMemorySegmentImpl
ofBuffer(Buffer b, long offset, long length)

private static long
offset(Buffer b, int scaleShifts)

pack-priv boolean
public final MemorySegment
reinterpret(long newSize, Arena arena, Consumer<MemorySegment> cleanup)

Implements java.lang.foreign.MemorySegment.reinterpret.

Returns a new segment with the same address as this segment, but with the provided size and the provided arena's scope.

public final MemorySegment
reinterpret(long newSize)

Implements java.lang.foreign.MemorySegment.reinterpret.

Returns a new memory segment that has the same address and scope as this segment, but with the provided size.

public final MemorySegment
reinterpret(Arena arena, Consumer<MemorySegment> cleanup)

Implements java.lang.foreign.MemorySegment.reinterpret.

Returns a new memory segment with the same address and size as this segment, but with the provided arena's scope.

private NativeMemorySegmentImpl
reinterpretInternal(Class<?> callerClass, long newSize, MemorySessionImpl scope, Consumer<MemorySegment> cleanup)

public MemorySessionImpl
scope()

Implements java.lang.foreign.MemorySegment.scope.

Returns the scope associated with this memory segment.

public final MemorySessionImpl
public void
set(ValueLayout.OfByte layout, long offset, byte value)

Implements java.lang.foreign.MemorySegment.set.

Writes a byte into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfBoolean layout, long offset, boolean value)

Implements java.lang.foreign.MemorySegment.set.

Writes a boolean into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfChar layout, long offset, char value)

Implements java.lang.foreign.MemorySegment.set.

Writes a char into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfShort layout, long offset, short value)

Implements java.lang.foreign.MemorySegment.set.

Writes a short into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfInt layout, long offset, int value)

Implements java.lang.foreign.MemorySegment.set.

Writes an int into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfFloat layout, long offset, float value)

Implements java.lang.foreign.MemorySegment.set.

Writes a float into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfLong layout, long offset, long value)

Implements java.lang.foreign.MemorySegment.set.

Writes a long into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfDouble layout, long offset, double value)

Implements java.lang.foreign.MemorySegment.set.

Writes a double into this segment at the given offset, with the given layout.

public void
set(AddressLayout layout, long offset, MemorySegment value)

Implements java.lang.foreign.MemorySegment.set.

Writes an address into this segment at the given offset, with the given layout.

public void
setAtIndex(ValueLayout.OfChar layout, long index, char value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a char into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfByte layout, long index, byte value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a byte into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfBoolean layout, long index, boolean value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a boolean into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfShort layout, long index, short value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a short into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfInt layout, long index, int value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes an int into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfFloat layout, long index, float value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a float into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfLong layout, long index, long value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a long into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfDouble layout, long index, double value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes a double into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(AddressLayout layout, long index, MemorySegment value)

Implements java.lang.foreign.MemorySegment.setAtIndex.

Writes an address into this segment at the given index, scaled by the given layout size.

public void
setString(long offset, String str)

Implements java.lang.foreign.MemorySegment.setString.

Writes the given string into this segment at the given offset, converting it to a null-terminated byte sequence using the UTF-8 charset.

public void
setString(long offset, String str, Charset charset)

Implements java.lang.foreign.MemorySegment.setString.

Writes the given string into this segment at the given offset, converting it to a null-terminated byte sequence using the provided charset.

public Spliterator<MemorySegment>
spliterator(MemoryLayout elementLayout)

Implements java.lang.foreign.MemorySegment.spliterator.

Returns a spliterator for this memory segment.

private void
throwOutOfBounds(long offset, long length, boolean isSlice)

public final byte[]
toArray(ValueLayout.OfByte elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new byte array.

public final short[]
toArray(ValueLayout.OfShort elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new short array.

public final char[]
toArray(ValueLayout.OfChar elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new char array.

public final int[]
toArray(ValueLayout.OfInt elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new int array.

public final float[]
toArray(ValueLayout.OfFloat elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new float array.

public final long[]
toArray(ValueLayout.OfLong elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new long array.

public final double[]
toArray(ValueLayout.OfDouble elementLayout)

Implements java.lang.foreign.MemorySegment.toArray.

Copy the contents of this memory segment into a new double array.

private <Z> Z
toArray(Class<Z> arrayClass, ValueLayout elemLayout, IntFunction<Z> arrayFactory, Function<Z, MemorySegment> segmentFactory)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
unload()

Implements java.lang.foreign.MemorySegment.unload.

Unloads the contents of this mapped segment from physical memory.

public abstract Object
public abstract long
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait