Class Mman
java.lang.Object
com.github.marschall.nativebytebuffers.Mman
Provides access to memory management using
sys/mman.h
.-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 bymmap(int, int)
.static int
memfd_create
(String name, int flags) Callsmemfd_create()
to create an anonymous file and returns a file descriptor that refers to it.static ByteBuffer
mmap
(int length) Callsmmap()
to create an anonymous shared virtual memory region and creates aByteBuffer
around it.static ByteBuffer
mmap
(int length, int flags) Callsmmap()
to create a virtual memory region and creates aByteBuffer
around it.static ByteBuffer
mmap
(int length, int flags, int fd) static void
munmap
(ByteBuffer buffer) Callsmunmap()
on the contents of the givenByteBuffer
.
-
Method Details
-
mmap
Callsmmap()
to create an anonymous shared virtual memory region and creates aByteBuffer
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, nevernull
, must be released withmunmap(ByteBuffer)
- Throws:
IllegalArgumentException
- iflength
is not positiveAllocationFailedException
- ifmmap()
fails- See Also:
-
mmap
Callsmmap()
to create a virtual memory region and creates aByteBuffer
around it.Protection will be
PROT_READ | PROT_WRITE
.- Parameters:
length
- the length of the mapping, must be greater than 0flags
- ORed arguments fromMmapFlags
that control various aspects of the mapping- Returns:
- a new ByteBuffer around the newly
mmap()
-ed area, nevernull
, must be released withmunmap(ByteBuffer)
- Throws:
IllegalArgumentException
- iflength
is not positiveAllocationFailedException
- ifmmap()
fails- See Also:
-
mmap
-
munmap
Callsmunmap()
on the contents of the givenByteBuffer
.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, notnull
, must have been created withmmap(int, int)
ormmap(int)
, must not be accessed afterwards, must not be passed to this method again- Throws:
NullPointerException
- ifbuffer
isnull
IllegalArgumentException
- ifbuffer
is not a direct bufferReleaseFailedException
- if unmapping fails- See Also:
-
memfd_create
Callsmemfd_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 encodingflags
- bitwise ORed in flags to change the behavior ofmemfd_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
- ifmemfd_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 bymmap(int, int)
.- Returns:
- the page size,
long
because in theory we could have pages larger than 2 GiB - See Also:
-