Skip to content

Commit

Permalink
Migrate common shm size checking logic to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Jul 15, 2024
1 parent 2b9064a commit 2c0866d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
7 changes: 1 addition & 6 deletions src/main/java/org/apposed/appose/shm/ShmLinux.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,8 @@ private SharedMemoryLinux(final String name, final boolean create, final int siz
shm_name = withLeadingSlash(name);
prevSize = getSHMSize(shm_name);
}
ShmUtils.checkSize(shm_name, prevSize, size);

final boolean alreadyExists = prevSize >= 0;
if (alreadyExists && prevSize < size) {
throw new RuntimeException("Shared memory segment already exists with smaller dimensions, data type or format. "
+ "Size of the existing shared memory segment cannot be smaller than the size of the proposed object. "
+ "Size of existing shared memory segment: " + prevSize + ", size of proposed object: " + size);
}
final int shmFd = LibRtOrC.shm_open(shm_name, O_CREAT | O_RDWR, 0666);
if (shmFd < 0) {
throw new RuntimeException("shm_open failed, errno: " + Native.getLastError());
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/apposed/appose/shm/ShmMacOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ private SharedMemoryMacOS(final String name, final boolean create, final int siz
shm_name = withLeadingSlash(name);
prevSize = getSHMSize(shm_name);
}
ShmUtils.checkSize(shm_name, prevSize, size);

final boolean alreadyExists = prevSize >= 0;
if (alreadyExists && prevSize < size) {
throw new RuntimeException("Shared memory segment already exists with smaller dimensions, data type or format. "
+ "Size of the existing shared memory segment cannot be smaller than the size of the proposed object. "
+ "Size of existing shared memory segment: " + prevSize + ", size of proposed object: " + size);
}
// shmFd = INSTANCE.shm_open(this.memoryName, O_RDWR, 0666);
final int shmFd = MacosHelpers.INSTANCE.create_shared_memory(shm_name, size);
if (shmFd < 0) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/apposed/appose/shm/ShmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ static String make_filename(int maxLength, String prefix) {
return name;
}

static void checkSize(String shmName, long prevSize, int size) {
final boolean alreadyExists = prevSize >= 0;
if (alreadyExists && prevSize < size) {
throw new RuntimeException("Shared memory segment '" + shmName + "' already exists with smaller size. "
+ "Size of the existing shared memory segment cannot be smaller than the size of the proposed object. "
+ "Size of existing shared memory segment: " + prevSize + ", size of proposed object: " + size);
}
}

private static String token_hex(int nbytes) {
final byte[] bytes = new byte[nbytes];
new Random().nextBytes(bytes);
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/org/apposed/appose/shm/ShmWindows.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,7 @@ private SharedMemoryWindows(final String name, final boolean create, final int s
shm_name = nameMangle_TODO(name);
prevSize = getSHMSize(shm_name);
}

final boolean alreadyExists = prevSize >= 0;
if(alreadyExists &&prevSize<size)

{
throw new RuntimeException("Shared memory segment already exists with smaller dimensions, data type or format. "
+ "Size of the existing shared memory segment cannot be smaller than the size of the proposed object. "
+ "Size of existing shared memory segment: " + prevSize + ", size of proposed object: " + size);
}
ShmUtils.checkSize(shm_name, prevSize, size);

final WinNT.HANDLE shm_hMapFile = Kernel32.INSTANCE.CreateFileMapping(
WinBase.INVALID_HANDLE_VALUE,
Expand Down

0 comments on commit 2c0866d

Please sign in to comment.