Small fixes
[panamaz] / src / template / Memory.java
index 89b4c6c..b720f99 100644 (file)
@@ -157,6 +157,11 @@ public class Memory {
                ResourceScope scope();
        }
 
+       public interface Array<T> {
+               long length();
+               T getAtIndex(long i);
+       }
+
        public record FunctionPointer<T>(NativeSymbol symbol, T function) {
        }
 
@@ -172,10 +177,18 @@ public class Memory {
                return v != null ? v.symbol().address() : MemoryAddress.NULL;
        }
 
-       public static long size(List<?> list) {
+       public static long length(List<?> list) {
                return list != null ? list.size() : 0;
        }
 
+       public static long length(Array<?> list) {
+               return list != null ? list.length() : 0;
+       }
+
+       public static long size(MemorySegment s) {
+               return s != null ? s.byteSize() : 0;
+       }
+
        // hmm do i want this or not?
        // -> added 'type safety'
        // -> load of crap to be written
@@ -194,6 +207,10 @@ public class Memory {
                        return create(MemorySegment.ofAddress(address, length, scope));
                }
 
+               public static ByteArray createArray(MemoryAddress address, ResourceScope scope) {
+                       return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
+               }
+
                public static ByteArray createArray(long length, SegmentAllocator alloc) {
                        return create(alloc.allocateArray(Memory.BYTE, length));
                }
@@ -256,7 +273,11 @@ public class Memory {
                }
 
                public static ShortArray createArray(MemoryAddress address, long length, ResourceScope scope) {
-                       return create(MemorySegment.ofAddress(address, length, scope));
+                       return create(MemorySegment.ofAddress(address, length * Memory.SHORT.byteSize(), scope));
+               }
+
+               public static ShortArray createArray(MemoryAddress address, ResourceScope scope) {
+                       return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
                }
 
                public static ShortArray createArray(long length, SegmentAllocator alloc) {
@@ -317,7 +338,11 @@ public class Memory {
                }
 
                public static IntArray createArray(MemoryAddress address, long length, ResourceScope scope) {
-                       return create(MemorySegment.ofAddress(address, length, scope));
+                       return create(MemorySegment.ofAddress(address, length * Memory.INT.byteSize(), scope));
+               }
+
+               public static IntArray createArray(MemoryAddress address, ResourceScope scope) {
+                       return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
                }
 
                public static IntArray createArray(long length, SegmentAllocator alloc) {
@@ -378,7 +403,7 @@ public class Memory {
                }
 
                public static LongArray createArray(MemoryAddress address, long length, ResourceScope scope) {
-                       return create(MemorySegment.ofAddress(address, length, scope));
+                       return create(MemorySegment.ofAddress(address, length * Memory.LONG.byteSize(), scope));
                }
 
                public static LongArray createArray(long length, SegmentAllocator alloc) {
@@ -439,7 +464,11 @@ public class Memory {
                }
 
                public static FloatArray createArray(MemoryAddress address, long length, ResourceScope scope) {
-                       return create(MemorySegment.ofAddress(address, length, scope));
+                       return create(MemorySegment.ofAddress(address, length * FLOAT.byteSize(), scope));
+               }
+
+               public static FloatArray createArray(MemoryAddress address, ResourceScope scope) {
+                       return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
                }
 
                public static FloatArray createArray(long length, SegmentAllocator alloc) {
@@ -500,7 +529,11 @@ public class Memory {
                }
 
                public static DoubleArray createArray(MemoryAddress address, long length, ResourceScope scope) {
-                       return create(MemorySegment.ofAddress(address, length, scope));
+                       return create(MemorySegment.ofAddress(address, length * Memory.DOUBLE.byteSize(), scope));
+               }
+
+               public static DoubleArray createArray(MemoryAddress address, ResourceScope scope) {
+                       return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
                }
 
                public static DoubleArray createArray(long length, SegmentAllocator alloc) {
@@ -612,7 +645,7 @@ public class Memory {
 
        // This needs a separate scope from the array itself
        public static class HandleArray<T extends Memory.Addressable> extends AbstractList<T> implements Memory.Addressable {
-               final MemorySegment segment;
+               public final MemorySegment segment;
                final ResourceScope scope;
                BiFunction<MemoryAddress,ResourceScope,T> create;