diff --git a/src/main/java/org/apposed/appose/shm/ShmLinux.java b/src/main/java/org/apposed/appose/shm/ShmLinux.java index a9dd8f5..087b1f4 100644 --- a/src/main/java/org/apposed/appose/shm/ShmLinux.java +++ b/src/main/java/org/apposed/appose/shm/ShmLinux.java @@ -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()); diff --git a/src/main/java/org/apposed/appose/shm/ShmMacOS.java b/src/main/java/org/apposed/appose/shm/ShmMacOS.java index 5f5115b..7837b33 100644 --- a/src/main/java/org/apposed/appose/shm/ShmMacOS.java +++ b/src/main/java/org/apposed/appose/shm/ShmMacOS.java @@ -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) { diff --git a/src/main/java/org/apposed/appose/shm/ShmUtils.java b/src/main/java/org/apposed/appose/shm/ShmUtils.java index 72fd144..3476fc3 100644 --- a/src/main/java/org/apposed/appose/shm/ShmUtils.java +++ b/src/main/java/org/apposed/appose/shm/ShmUtils.java @@ -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); diff --git a/src/main/java/org/apposed/appose/shm/ShmWindows.java b/src/main/java/org/apposed/appose/shm/ShmWindows.java index 0bcb1d8..9c3e470 100644 --- a/src/main/java/org/apposed/appose/shm/ShmWindows.java +++ b/src/main/java/org/apposed/appose/shm/ShmWindows.java @@ -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