Record Class IntPtr

java.lang.Object
java.lang.Record
club.doki7.ffm.ptr.IntPtr
All Implemented Interfaces:
IPointer, Iterable<Integer>

@ValueBasedCandidate @UnsafeConstructor public record IntPtr(@NotNull MemorySegment segment) extends Record implements IPointer, Iterable<Integer>

Represents a pointer to 32-bit integer(s) in native memory.

The property segment() should always be not-null (segment != NULL && !segment.equals(MemorySegment.NULL)), and properly aligned to MemoryLayout.byteAlignment() bytes. To represent null pointer, you may use a Java null instead. See the documentation of IPointer.segment() for more details.

The constructor of this class is marked as UnsafeConstructor, because it does not perform any runtime check. The constructor can be useful for automatic code generators. For normal users, checked(MemorySegment) is a good safe alternative.

  • Constructor Details

    • IntPtr

      public IntPtr(@NotNull @NotNull MemorySegment segment)
      Creates an instance of a IntPtr record class.
      Parameters:
      segment - the value for the segment record component
  • Method Details

    • size

      public long size()
    • read

      public int read()
    • write

      public void write(int value)
    • read

      public int read(long index)
    • write

      public void write(long index, int value)
    • write

      public void write(int @NotNull [] array)
    • writeV

      public void writeV(int value0, int @NotNull ... values)
    • reinterpret

      @Unsafe @NotNull public @NotNull IntPtr reinterpret(long newSize)

      Assume the IntPtr is capable of holding at least newSize integers, create a new view IntPtr that uses the same backing storage as this IntPtr, but with the new size. Since there is actually no way to really check whether the new size is valid, while buffer overflow is undefined behavior, this method is marked as Unsafe.

      This method could be useful when handling data returned from some C API, where the size of the data is not known in advance.

      If the size of the underlying segment is actually known in advance and correctly set, and you want to create a shrunk view, you may use slice(long) (with validation) instead.

    • offset

      @NotNull public @NotNull IntPtr offset(long offset)
    • slice

      @NotNull public @NotNull IntPtr slice(long start, long end)
      Note that this function uses the List.subList(int, int) semantics (left inclusive, right exclusive interval), not MemorySegment.asSlice(long, long) semantics (offset + newSize). Be careful with the difference.
    • slice

      @NotNull public @NotNull IntPtr slice(long end)
    • iterator

      @NotNull public @NotNull Iterator<Integer> iterator()
      Specified by:
      iterator in interface Iterable<Integer>
    • checked

      @Contract("null -> null") @Nullable public static @Nullable IntPtr checked(@Nullable @Nullable MemorySegment segment)

      Create a new IntPtr using segment as backing storage, with argument validation.

      This function does not ensure segment's size to be a multiple of Integer.BYTES, since that several trailing bytes could be automatically ignored by size() method, and usually these bytes does not interfere with FFI operations. If segment is not big enough to hold at least one integer, that segment is simply considered "empty". See the documentation of IPointer.segment() for more details.

      Parameters:
      segment - the MemorySegment to use as the backing storage
      Returns:
      null if segment is null or MemorySegment.NULL, otherwise a new IntPtr that uses segment as backing storage
      Throws:
      IllegalArgumentException - if segment is not native or not properly aligned
    • checked

      @NotNull public static @NotNull IntPtr checked(@NotNull @NotNull IntBuffer buffer)

      Create a new IntPtr using the same backing storage as buffer, with argument validation.

      The main difference between this static method and the allocate(Arena, IntBuffer) method is that this method does not copy the contents of buffer into a newly allocated MemorySegment. Instead, the newly created IntPtr will use the same backing storage as buffer. Thus, modifications from one side will be visible on the other side.

      Be careful with java.nio buffer types' Buffer.position() property: only the "remaining" (from Buffer.position() to Buffer.limit()) part of buffer will be referred. If you have ever read from buffer, and you want all the contents of buffer to be referred, you may want to call Buffer.rewind().

      When handling data types consisting of multiple bytes, also be careful with endianness and IntBuffer.order() property. IntPtr always uses the native endianness. So if buffer uses a different endianness, you may want to convert it to the native endianness first.

      Parameters:
      buffer - the IntBuffer to use as the backing storage
      Returns:
      a new IntPtr that uses buffer as its backing storage
      Throws:
      IllegalArgumentException - if buffer is not direct, or its backing storage is not properly aligned
    • allocate

      @NotNull public static @NotNull IntPtr allocate(@NotNull @NotNull Arena arena)
    • allocate

      @NotNull public static @NotNull IntPtr allocate(@NotNull @NotNull Arena arena, long size)
    • allocate

      @NotNull public static @NotNull IntPtr allocate(@NotNull @NotNull Arena arena, int @NotNull [] array)
    • allocateV

      @NotNull public static @NotNull IntPtr allocateV(@NotNull @NotNull Arena arena, int value0, int... values)
    • allocate

      @NotNull public static @NotNull IntPtr allocate(@NotNull @NotNull Arena arena, byte @NotNull [] array)

      Allocate a new IntPtr in arena and copy the contents of array into the newly allocated IntPtr.

      Be aware that if the length of array is not a multiple of Integer.BYTES, the residual bytes will be simply discarded.

      Parameters:
      arena - the Arena to allocate the new IntPtr in
      array - the byte array to copy the contents from
      Returns:
      a new IntPtr that contains the contents of array
    • allocate

      @NotNull public static @NotNull IntPtr allocate(@NotNull @NotNull Arena arena, @NotNull @NotNull IntBuffer buffer)

      Allocate a new IntPtr in arena and copy the contents of buffer into the newly allocated IntPtr.

      Be careful with java.nio buffer types' Buffer.position() property: only the "remaining" (from Buffer.position() to Buffer.limit()) part of buffer will be copied. If you have ever read from buffer, and you want all the contents of buffer to be copied, you may want to call Buffer.rewind().

      When handling data types consisting of multiple bytes, also be careful with endianness and IntBuffer.order() property. IntPtr always uses the native endianness. So if buffer uses a different endianness, you may want to convert it to the native endianness first.

      Parameters:
      arena - the Arena to allocate the new IntPtr in
      buffer - the IntBuffer to copy the contents from
      Returns:
      a new IntPtr that contains the contents of buffer
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • segment

      @NotNull public @NotNull MemorySegment segment()
      Returns the value of the segment record component.
      Specified by:
      segment in interface IPointer
      Returns:
      the value of the segment record component