Class SharedMemory
java.lang.Object
org.newsclub.net.unix.memory.SharedMemory
- All Implemented Interfaces:
Closeable, AutoCloseable
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe exact size (in bytes) required for aMemorySegmentused inmutex(MemorySegment). -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSeals(Set<MemorySeal> seals) Adds the givenMemorySeals, preventing certain operations on shared memory.asMappedMemorySegment(FileChannel.MapMode mapMode) Return aMemorySegmentinstance corresponding to this shared memory object, using the givenFileChannel.MapMode, and a custom sharedArenathat will be closed uponclose().asMappedMemorySegment(FileChannel.MapMode mapMode, Arena arena) Return aMemorySegmentinstance corresponding to this shared memory object, using the givenFileChannel.MapMode, and the given arena.asMappedMemorySegment(FileChannel.MapMode mapMode, Arena arena, int duplicates) Return aMemorySegmentinstance corresponding to this shared memory object -- repeated multiple times after each other (aligned with page size) -- using the givenFileChannel.MapMode, and the given arena, as well as the duplication count.asMappedMemorySegment(FileChannel.MapMode mapMode, Arena arena, long offset, long length, int duplicates) Return aMemorySegmentinstance corresponding to a range of this shared memory object -- repeated multiple times after each other (aligned with page size) -- using the givenFileChannel.MapMode, and the given arena, as well as the duplication count.longbyteSize()Returns the aligned size of this shared memory instance.voidclose()Closes thisSharedMemoryresource, potentially unlinking the corresponding underlying resource from the kernel if the object has been instantiated withSharedMemoryOption.UNLINK_UPON_CLOSE.static SharedMemorycreateAnonymous(long minimumLength) Creates a newSharedMemoryinstance using an anonymous identifier.static SharedMemorycreateAnonymous(long minimumLength, SharedMemoryOption... options) Creates a newSharedMemoryinstance using an anonymous identifier.static SharedMemorycreateExclusively(String name, long minimumLength, Set<PosixFilePermission> perms, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given name.static SharedMemorycreateExclusively(String name, long minimumLength, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given name, using default permissions (read-write for all users, where applicable).static SharedMemorycreateOrOpenExisting(String name, long minimumLength, Set<PosixFilePermission> perms, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given namex.static SharedMemorycreateOrOpenExisting(String name, long minimumLength, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given name, using default permissions (read-write for all users, where applicable).static SharedMemorycreateOrReuseExisting(String name, long minimumLength, Set<PosixFilePermission> perms, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given name.static SharedMemorycreateOrReuseExisting(String name, long minimumLength, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given name, using default permissions (read-write for all users, where applicable).static longReturns the system's default memory page allocation size for shared memory.Returns the file descriptor associated with this instance.getSeals()Returns the currentMemorySeals for this shared memory instance.static voidinit(MemoryImplUtilInternal util) Internal initializer used by junixsocket-common; do not use.mutex(MemorySegment addr) Returns aSharedMutexinstance working with the givenMemorySegment, which has to be exactlyMUTEX_SEGMENT_SIZEbytes long.static SharedMemoryopenExisting(String name, SharedMemoryOption... options) Creates a newSharedMemoryinstance under the given name, using the object under the given name.static voidunlinkShared(String name) Asks to explicitly unlink/remove a shared memory object identified by the given name.static SharedMemoryusing(FileDescriptor fd) Creates a newSharedMemoryinstance using the given file descriptor, which can be associated with a regular file that is to be memory-mapped, or a shared memory region.
-
Field Details
-
MUTEX_SEGMENT_SIZE
public static final int MUTEX_SEGMENT_SIZEThe exact size (in bytes) required for aMemorySegmentused inmutex(MemorySegment).- See Also:
-
-
Method Details
-
init
Internal initializer used by junixsocket-common; do not use.- Parameters:
util- The MemoryImplUtil instance.
-
using
Creates a newSharedMemoryinstance using the given file descriptor, which can be associated with a regular file that is to be memory-mapped, or a shared memory region.- Parameters:
fd- The file descriptor.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
createAnonymous
Creates a newSharedMemoryinstance using an anonymous identifier.- Parameters:
minimumLength- The requested length (the actual object can be larger).- Returns:
- The new instance.
- Throws:
IOException- on error.
-
getFileDescriptor
Returns the file descriptor associated with this instance.- Returns:
- The file descriptor.
-
asMappedMemorySegment
Return aMemorySegmentinstance corresponding to this shared memory object, using the givenFileChannel.MapMode, and a custom sharedArenathat will be closed uponclose().- Parameters:
mapMode- The map mode.- Returns:
- The memory segment.
- Throws:
IOException- on error.
-
asMappedMemorySegment
public MemorySegment asMappedMemorySegment(FileChannel.MapMode mapMode, Arena arena) throws IOException Return aMemorySegmentinstance corresponding to this shared memory object, using the givenFileChannel.MapMode, and the given arena.If the given arena is
null, a custom sharedArenais used that will be closed uponclose().- Parameters:
mapMode- The map mode.arena- The arena to use, ornull.- Returns:
- The memory segment.
- Throws:
IOException- on error.
-
asMappedMemorySegment
public MemorySegment asMappedMemorySegment(FileChannel.MapMode mapMode, Arena arena, int duplicates) throws IOException Return aMemorySegmentinstance corresponding to this shared memory object -- repeated multiple times after each other (aligned with page size) -- using the givenFileChannel.MapMode, and the given arena, as well as the duplication count.This method is particularly useful to simplify building circular buffers ("magic RingBuffer").
If the given arena is
null, a custom sharedArenais used that will be closed uponclose().- Parameters:
mapMode- The map mode.arena- The arena to use, ornull.duplicates- The number of times the shared memory should be repeated (0 = no repetitions, just 1 copy).- Returns:
- The memory segment.
- Throws:
IOException- on error.
-
asMappedMemorySegment
public MemorySegment asMappedMemorySegment(FileChannel.MapMode mapMode, Arena arena, long offset, long length, int duplicates) throws IOException Return aMemorySegmentinstance corresponding to a range of this shared memory object -- repeated multiple times after each other (aligned with page size) -- using the givenFileChannel.MapMode, and the given arena, as well as the duplication count.This method is particularly useful to simplify building circular buffers ("magic RingBuffer").
If the given arena is
null, a custom sharedArenais used that will be closed uponclose().- Parameters:
mapMode- The map mode.arena- The arena to use, ornull.offset- The offset from the beginning of this segment, in bytes.length- The length of the mapped region, in bytes.duplicates- The number of times the shared memory should be repeated (0 = no repetitions, just 1 copy).- Returns:
- The memory segment.
- Throws:
IOException- on error.
-
addSeals
Adds the givenMemorySeals, preventing certain operations on shared memory.- Parameters:
seals- The seals.- Throws:
IOException- on error (e.g., if unsupported).
-
getSeals
Returns the currentMemorySeals for this shared memory instance.- Returns:
- The seals, or empty if none or unsupported.
- Throws:
IOException- on error (e.g., if a system call fails unexpectedly).
-
close
Closes thisSharedMemoryresource, potentially unlinking the corresponding underlying resource from the kernel if the object has been instantiated withSharedMemoryOption.UNLINK_UPON_CLOSE.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
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.
-
mutex
Returns aSharedMutexinstance working with the givenMemorySegment, which has to be exactlyMUTEX_SEGMENT_SIZEbytes long.- Parameters:
addr- The address.- Returns:
- The instance.
- Throws:
IOException- on error.
-
byteSize
public long byteSize()Returns the aligned size of this shared memory instance.- Returns:
- The aligned size, in bytes.
-