From 5fff306e2b73cb279376ae5e7523ea7cb96fda2e Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Tue, 24 Sep 2024 12:19:38 -0500 Subject: [PATCH] Make ShmInfo track the *requested* size On macOS, the size of the allocated shared memory block might not precisely match the request. But it needs to be at least as large as the requested size. E.g.: sizes less than 16384 round up to 16384. When asking the SharedMemory object for its size, we should remember the requested size and return that value, rather than the rounded up one. --- src/main/java/org/apposed/appose/shm/ShmBase.java | 3 +++ src/main/java/org/apposed/appose/shm/ShmLinux.java | 3 ++- src/main/java/org/apposed/appose/shm/ShmMacOS.java | 3 ++- src/main/java/org/apposed/appose/shm/ShmWindows.java | 3 ++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apposed/appose/shm/ShmBase.java b/src/main/java/org/apposed/appose/shm/ShmBase.java index 83b6349..e3191da 100644 --- a/src/main/java/org/apposed/appose/shm/ShmBase.java +++ b/src/main/java/org/apposed/appose/shm/ShmBase.java @@ -116,6 +116,9 @@ protected static class ShmInfo { /** Size in bytes. */ int size; + /** Size in bytes allocated by the operating system. */ + int totalSize; + /** Pointer referencing the shared memory. */ Pointer pointer; diff --git a/src/main/java/org/apposed/appose/shm/ShmLinux.java b/src/main/java/org/apposed/appose/shm/ShmLinux.java index ac5e06f..9c35c6b 100644 --- a/src/main/java/org/apposed/appose/shm/ShmLinux.java +++ b/src/main/java/org/apposed/appose/shm/ShmLinux.java @@ -119,7 +119,8 @@ private static ShmInfo prepareShm(String name, boolean create, int size } ShmInfo info = new ShmInfo<>(); - info.size = shm_size; + info.size = size; + info.totalSize = shm_size; info.name = withoutLeadingSlash(shm_name); info.pointer = pointer; info.handle = shmFd; diff --git a/src/main/java/org/apposed/appose/shm/ShmMacOS.java b/src/main/java/org/apposed/appose/shm/ShmMacOS.java index 388a2cc..2afeb87 100644 --- a/src/main/java/org/apposed/appose/shm/ShmMacOS.java +++ b/src/main/java/org/apposed/appose/shm/ShmMacOS.java @@ -110,7 +110,8 @@ private static ShmInfo prepareShm(String name, boolean create, int size } ShmInfo info = new ShmInfo<>(); - info.size = shm_size; + info.size = size; + info.totalSize = shm_size; info.name = withoutLeadingSlash(shm_name); info.pointer = pointer; info.handle = shmFd; diff --git a/src/main/java/org/apposed/appose/shm/ShmWindows.java b/src/main/java/org/apposed/appose/shm/ShmWindows.java index c558d4a..8cfbbc3 100644 --- a/src/main/java/org/apposed/appose/shm/ShmWindows.java +++ b/src/main/java/org/apposed/appose/shm/ShmWindows.java @@ -122,7 +122,8 @@ private static ShmInfo prepareShm(String name, boolean create, int } ShmInfo info = new ShmInfo<>(); - info.size = shm_size; + info.size = size; + info.totalSize = shm_size; info.name = shm_name; info.pointer = pointer; info.writePointer = writePointer;