java.lang.Object
com.github.marschall.nativebytebuffers.Mman

public final class Mman extends Object
Provides access to memory management using sys/mman.h.
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    Returns the number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mapping performed by mmap(int, int).
    static int
    memfd_create(String name, int flags)
    Calls memfd_create() to create an anonymous file and returns a file descriptor that refers to it.
    static ByteBuffer
    mmap(int length)
    Calls mmap() to create an anonymous shared virtual memory region and creates a ByteBuffer around it.
    static ByteBuffer
    mmap(int length, int flags)
    Calls mmap() to create a virtual memory region and creates a ByteBuffer around it.
    static ByteBuffer
    mmap(int length, int flags, int fd)
     
    static void
    Calls munmap() on the contents of the given ByteBuffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • mmap

      public static ByteBuffer mmap(int length)
      Calls mmap() to create an anonymous shared virtual memory region and creates a ByteBuffer around it.

      Protection will be PROT_READ | PROT_WRITE.

      Parameters:
      length - the length of the mapping, must be greater than 0
      Returns:
      a new ByteBuffer around the newly mmap()-ed area, never null, must be released with munmap(ByteBuffer)
      Throws:
      IllegalArgumentException - if length is not positive
      AllocationFailedException - if mmap() fails
      See Also:
    • mmap

      public static ByteBuffer mmap(int length, int flags)
      Calls mmap() to create a virtual memory region and creates a ByteBuffer around it.

      Protection will be PROT_READ | PROT_WRITE.

      Parameters:
      length - the length of the mapping, must be greater than 0
      flags - ORed arguments from MmapFlags that control various aspects of the mapping
      Returns:
      a new ByteBuffer around the newly mmap()-ed area, never null, must be released with munmap(ByteBuffer)
      Throws:
      IllegalArgumentException - if length is not positive
      AllocationFailedException - if mmap() fails
      See Also:
    • mmap

      public static ByteBuffer mmap(int length, int flags, int fd)
    • munmap

      public static void munmap(ByteBuffer buffer)
      Calls munmap() on the contents of the given ByteBuffer.

      This method may crash the JVM the buffer passed has not been created by mmap(int, int), mmap(int) or has already been passed to this method.

      Parameters:
      buffer - the buffer to free, not null, must have been created with mmap(int, int) or mmap(int), must not be accessed afterwards, must not be passed to this method again
      Throws:
      NullPointerException - if buffer is null
      IllegalArgumentException - if buffer is not a direct buffer
      ReleaseFailedException - if unmapping fails
      See Also:
    • memfd_create

      public static int memfd_create(String name, int flags) throws IOException
      Calls memfd_create() to create an anonymous file and returns a file descriptor that refers to it.
      Parameters:
      name - the name supplied in name is used as a filename and will be displayed as the target of the corresponding symbolic link in the directory /proc/self/fd/. The displayed name is always prefixed with memfd: and serves only for debugging purposes. Names do not affect the behavior of the file descriptor, and as such multiple files can have the same name without any side effects. Must not be longer than 249 in the native encoding
      flags - bitwise ORed in flags to change the behavior of memfd_create()
      Returns:
      new file descriptor that can be used to refer to the file. This file descriptor is opened for both reading and writing.
      Throws:
      IOException - if memfd_create() fails
      See Also:
    • getpagesize

      public static long getpagesize()
      Returns the number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mapping performed by mmap(int, int).
      Returns:
      the page size, long because in theory we could have pages larger than 2 GiB
      See Also: