Skip to content

Commit

Permalink
Add simpler SharedMemory creation methods
Browse files Browse the repository at this point in the history
There are only two cases: create or attach.
  • Loading branch information
ctrueden committed Jul 12, 2024
1 parent 5b04067 commit 1341a2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/apposed/appose/NDArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public NDArray(final SharedMemory sharedMemory, final DType dType, final Shape s
* @param shape array shape
*/
public NDArray(final DType dType, final Shape shape) {
this(SharedMemory.create(null, true,
this(SharedMemory.create(null,
safeInt(shape.numElements() * dType.bytesPerElement())), dType, shape);
}

Expand Down
27 changes: 24 additions & 3 deletions src/main/java/org/apposed/appose/SharedMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@
*/
public interface SharedMemory extends AutoCloseable {

/**
* Creates a new shared memory block.
*
* @param name the unique name for the requested shared memory, specified
* as a string. If {@code null} is supplied for the name, a novel
* name will be generated.
* @param size size in bytes.
*/
static SharedMemory create(String name, int size) {
return createOrAttach(name, true, size);
}

/**
* Attaches to an existing shared memory block.
*
* @param name the unique name for the requested shared memory, specified
* as a string.
*/
static SharedMemory attach(String name, int size) {
return createOrAttach(name, false, size);
}

/**
* Creates a new shared memory block or attaches to an existing shared
* memory block.
Expand All @@ -52,9 +74,9 @@ public interface SharedMemory extends AutoCloseable {
* name will be generated.
* @param create whether a new shared memory block is created ({@code true})
* or an existing one is attached to ({@code false}).
* @param size size in bytes
* @param size size in bytes, or 0 if create==false
*/
static SharedMemory create(String name, boolean create, int size) {
static SharedMemory createOrAttach(String name, boolean create, int size) {
if (size < 0) {
throw new IllegalArgumentException("'size' must be a positive integer");
}
Expand All @@ -64,7 +86,6 @@ static SharedMemory create(String name, boolean create, int size) {
if (!create && name == null) {
throw new IllegalArgumentException("'name' can only be null if create=true");
}

ServiceLoader<ShmFactory> loader = ServiceLoader.load(ShmFactory.class);
for (ShmFactory factory: loader) {
SharedMemory shm = factory.create(name, create, size);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apposed/appose/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private static Object processValue(Object value) {
case "shm":
final String name = (String) map.get("name");
final int size = (int) map.get("size");
return SharedMemory.create(name, false, size);
return SharedMemory.attach(name, size);
case "ndarray":
final SharedMemory shm = (SharedMemory) map.get("shm");
final DType dType = toDType((String) map.get("dtype"));
Expand Down

0 comments on commit 1341a2f

Please sign in to comment.