Class MappedMemory

java.lang.Object
org.newsclub.net.unix.memory.MappedMemory
All Implemented Interfaces:
Closeable, AutoCloseable

public final class MappedMemory extends Object implements Closeable
Some mapped memory.
Author:
Christian Kohlschütter
  • Method Details

    • getMemorySegment

      public MemorySegment getMemorySegment()
      Returns the MemorySegment backed by this instance.
      Returns:
      The MemorySegment.
    • getMinimumSubAllocationSize

      public long getMinimumSubAllocationSize()
      Returns the minimum sub-allocation size.
      Returns:
      The minimum sub-allocation size.
    • defaultAllocationSize

      public static long defaultAllocationSize()
      Returns the system's default memory page allocation size for shared memory.

      This may be larger than the system's regular page size (e.g., on Windows it's 64k).

      Returns:
      The page size.
    • anonymousReadOnlySegment

      public static MappedMemory anonymousReadOnlySegment(Arena arena, long length) throws IOException
      Returns an anonymous read-only memory segment of the given length.
      Parameters:
      arena - The arena to associate the MappedMemory instance with.
      length - The length of the segment.
      Returns:
      The MappedMemory segment.
      Throws:
      IOException - on error.
    • anonymousSegment

      public static MappedMemory anonymousSegment(Arena arena, long length) throws IOException
      Returns an anonymous read/write memory segment of the given length.
      Parameters:
      arena - The arena to associate the MappedMemory instance with.
      length - The length of the segment.
      Returns:
      The MappedMemory segment.
      Throws:
      IOException - on error.
    • placeholderSegment

      public static MappedMemory placeholderSegment(Arena arena, long length) throws IOException
      Returns an anonymous memory segment of the given length that acts as a placeholder for later, memory-mapped segments in this area.
      Parameters:
      arena - The arena to associate the MappedMemory instance with.
      length - The length of the segment.
      Returns:
      The MappedMemory segment.
      Throws:
      IOException - on error.
    • anonymousCopyOnWriteSegment

      public static MappedMemory anonymousCopyOnWriteSegment(Arena arena, long length) throws IOException
      Returns an anonymous copy-on-write (changes are local to the process running this JVM) memory segment of the given length.
      Parameters:
      arena - The arena to associate the MappedMemory instance with.
      length - The length of the segment.
      Returns:
      The MappedMemory segment.
      Throws:
      IOException - on error.
    • mapRegion

      public MappedMemory mapRegion(long msOffset, long length, FileDescriptor fd, long fdOffset) throws IOException
      Maps some memory to a region of this MappedMemory object. This may fail to work on Windows, unless placeholderSegment(Arena, long) was used to create this MappedMemory instance.
      Parameters:
      msOffset - The target offset in this object.
      length - The number of bytes to map.
      fd - The file descriptor to map from.
      fdOffset - The file descriptor-specific content offset
      Returns:
      A new MappedMemory object, whose MemorySegment is a subrange of this object's MemorySegment.
      Throws:
      IOException - on error.
      See Also:
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException